Revision 2345

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dispose/DisposableIterable.java
1 1
package org.gvsig.tools.dispose;
2 2

  
3
import java.util.Iterator;
4 3

  
5

  
6 4
public interface DisposableIterable<T>
7
        extends Iterator<T>, Iterable<T>, Disposable {
5
        extends Iterable<T>, Disposable {
8 6
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/MapBuilder.java
1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

  
23
package org.gvsig.tools.util;
24

  
25
import java.util.HashMap;
26
import java.util.Map;
27

  
28
/**
29
 *
30
 * @author gvSIG Team
31
 * 
32
 * @param <K> the type of keys maintained by this map
33
 * @param <V> the type of mapped values
34
 */
35
public class MapBuilder<K,V> {
36

  
37
    private final Map<K,V> map;
38
    
39
    public MapBuilder() {
40
        this.map = new HashMap<>();
41
    }
42
    
43
    public MapBuilder add(K key, V value) {
44
        this.map.put(key, value);
45
        return this;
46
    }
47
    
48
    public Map<K,V> build() {
49
        return this.map;
50
    }
51
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/IterableBuilder.java
1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

  
23
package org.gvsig.tools.util;
24

  
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.Iterator;
28
import java.util.List;
29

  
30
/**
31
 *
32
 * @author gvSIG Team
33
 */
34
public class IterableBuilder<T> {
35

  
36
    public List<T> l;
37
    public Iterator<T> it;
38
    
39
    public IterableBuilder() {
40
        this.l = null;
41
        this.it = null;
42
    }
43
    
44
    public IterableBuilder(Iterator<T> iterator) {
45
        this.l = null;
46
        this.it = iterator;
47
    }
48
    
49
    public IterableBuilder add(Iterator<T> iterator) {
50
        this.it = iterator;
51
        return this;
52
    }
53
    
54
    public IterableBuilder add(T value) {
55
        if( this.l==null ) {
56
            this.l = new ArrayList<>();    
57
        }
58
        this.l.add(value);
59
        return this;
60
    }
61
    
62
    public Iterable<T> build(Iterator<T> it) {
63
        return () -> it;
64
    }
65
    
66
    public Iterable<T> build() {
67
        if( this.l!=null ) {
68
            return this.l;
69
        }
70
        if( this.it!=null ) {
71
            return () -> it;
72
        }
73
        return Collections.EMPTY_LIST;
74
    }
75
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/ChainedIterable.java
1 1
package org.gvsig.tools.util;
2 2

  
3 3
import java.util.Iterator;
4
import org.gvsig.tools.dispose.Disposable;
5
import org.gvsig.tools.dispose.DisposableIterable;
4 6

  
5
public class ChainedIterable<T> implements Iterable<T> {
7
public class ChainedIterable<T> implements DisposableIterable<T> {
6 8

  
7
    private final Iterator<T>[] iterators;
9
    private final Iterable<T>[] iterables;
8 10

  
9 11
    public ChainedIterable(Iterable<T>... iterables) {
10
        this.iterators = new Iterator[iterables.length];
11
        for (int i = 0; i < iterables.length; i++) {
12
        this.iterables = new Iterable[iterables.length];
13
        System.arraycopy(iterables, 0, this.iterables, 0, iterables.length);
14
    }
15

  
16
    @Override
17
    public Iterator<T> iterator() {
18
        Iterator[] iterators = new Iterator[this.iterables.length];
19
        for (int i = 0; i < this.iterables.length; i++) {
12 20
            if( iterables[i]==null ) {
13 21
                iterators[i] = null;
14 22
            } else {
15 23
                iterators[i] = iterables[i].iterator();
16 24
            }
17 25
        }
26
        return new ChainedIterator(iterators);
18 27
    }
19 28

  
20 29
    @Override
21
    public Iterator<T> iterator() {
22
        return new ChainedIterator(this.iterators);
30
    public void dispose() {
31
        for (Iterable<T> iterable : this.iterables) {
32
            if (iterable instanceof Disposable) {
33
                ((Disposable) iterable).dispose();
34
            }
35
        }
23 36
    }
24 37

  
25 38
}

Also available in: Unified diff