Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / UnmodifiableBasicSetMapped.java @ 2335

History | View | Annotate | Download (1.35 KB)

1 1886 jjdelcerro
package org.gvsig.tools.util;
2
3
import java.util.Collection;
4
import java.util.Iterator;
5
import java.util.function.Function;
6
7
/**
8
 *
9
 * @author jjdelcerro
10
 */
11
public class UnmodifiableBasicSetMapped<S, T> implements UnmodifiableBasicSet<T> {
12
13
    private final Collection<S> source1;
14
    private final UnmodifiableBasicCollection<S> source2;
15
    private final Function<S, T> mapping;
16
17
    public UnmodifiableBasicSetMapped(Collection<S> source, Function<S, T> mapping) {
18
        this.source1 = source;
19
        this.source2 = null;
20
        this.mapping = mapping;
21
    }
22
23
    public UnmodifiableBasicSetMapped(UnmodifiableBasicCollection<S> source, Function<S, T> mapping) {
24
        this.source1 = null;
25
        this.source2 = source;
26
        this.mapping = mapping;
27
    }
28
29
    @Override
30
    public boolean isEmpty() {
31
        if (this.source1 != null) {
32
            return this.source1.isEmpty();
33
        }
34
        return this.source2.isEmpty();
35
    }
36
37
    @Override
38
    public int size() {
39
        if (this.source1 != null) {
40
            return this.source1.size();
41
        }
42
        return this.source2.size();
43
    }
44
45
    @Override
46
    public Iterator<T> iterator() {
47
        if (this.source1 != null) {
48
            return new MappedIterator<>(this.source1.iterator(), this.mapping);
49
        }
50
        return new MappedIterator<>(this.source2.iterator(), this.mapping);
51
    }
52
53
}