Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / selection / SelectionIterator.java @ 27637

History | View | Annotate | Download (2.31 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
                        ds.start();
68
                        Value[] result = ds.getRow(currBit);
69
                        ds.stop();
70
                        return result;
71
                } catch (ReadDriverException e) {
72
                        NoSuchElementException e1 = new NoSuchElementException();
73
                        e1.initCause(e);
74
                        throw e1;
75
                }
76
        }
77

    
78
        public void remove() {
79
                throw new UnsupportedOperationException();
80
        }
81

    
82
        public void reset() {
83
                currBit = -1;
84
                usedNext = true;
85
        }
86

    
87
        public Object clone() throws CloneNotSupportedException {
88
                SelectionFieldIterator obj = (SelectionFieldIterator) super.clone();
89
                obj.reset();
90
                return obj;
91
        }
92
}