Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / selection / SelectionIterator.java @ 26898

History | View | Annotate | Download (2.25 KB)

1
package com.iver.cit.gvsig.fmap.selection;
2

    
3
import java.util.BitSet;
4
import java.util.NoSuchElementException;
5

    
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.values.Value;
9
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
10

    
11
/**
12
 * <p>Convenience class which allows to access a selection of rows in a DataSource,
13
 * using an Iterator interface (instead of the painful BitSet interface).</p>
14
 * 
15
 * <p>This class is intended to smooth transition to 2.0 version, in which
16
 * random access has been replaced by iterators.</p>
17
 * 
18
 * <p>Warning: This object is not thread-safe!!! Use external synchronization
19
 * if you plan to use it from different threads, otherwise it is warranted
20
 * to fail!!</p>
21
 * 
22
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 18/02/2009
23
 * @author IVER T.I. <http://www.iver.es> 18/02/2009
24
 *
25
 */
26
public class SelectionIterator
27
                implements ResettableIterator<Value[]>, Cloneable {
28
        DataSource ds = null;
29
        BitSet selectedRows = null;
30
        int currBit;
31
        boolean hasNext;
32
        boolean usedNext = true;
33

    
34
        /**
35
         * <p>Creates a new Iterator, which is able to access the provided
36
         * <code>selection</code> from <code>ds</code>.</p>
37
         * 
38
         * @param ds
39
         * @param selection The selection of rows to iterate through
40
         */
41
        public SelectionIterator(DataSource ds, BitSet selection) {
42
                this.selectedRows = (BitSet) selection.clone();
43
                this.ds = ds;
44
                reset();
45
        }
46

    
47
        public boolean hasNext() {
48
                if (usedNext==true) {
49
                        currBit = selectedRows.nextSetBit(currBit+1);
50
                        if (currBit<0) {
51
                                hasNext = false;
52
                        }
53
                        else {
54
                                hasNext = true; 
55
                        }
56
                        usedNext = false;
57
                }
58
                return hasNext;
59
        }
60

    
61
        public Value[] next() {
62
                if (!hasNext()) {
63
                        throw new NoSuchElementException();
64
                }
65
                usedNext = true;
66
                try {
67
                        return ds.getRow(currBit);        
68
                } catch (ReadDriverException e) {
69
                        NoSuchElementException e1 = new NoSuchElementException();
70
                        e1.initCause(e);
71
                        throw e1;
72
                }
73
        }
74

    
75
        public void remove() {
76
                throw new UnsupportedOperationException();
77
        }
78

    
79
        public void reset() {
80
                currBit = -1;
81
                usedNext = true;
82
        }
83

    
84
        public Object clone() throws CloneNotSupportedException {
85
                SelectionFieldIterator obj = (SelectionFieldIterator) super.clone();
86
                obj.reset();
87
                return obj;
88
        }
89
}