svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / selection / SelectionFieldIterator.java @ 26898
History | View | Annotate | Download (2.82 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 |
* <p>Allows to access a field from a selection of rows in a DataSource,
|
12 |
* using an Iterator interface (instead of the painful BitSet interface).</p>
|
13 |
*
|
14 |
* <p>This class is intended to smooth transition to 2.0 version, in which
|
15 |
* random access has been replaced by iterators.</p>
|
16 |
*
|
17 |
* <p>Warning: This object is not thread-safe!!! Use external synchronization
|
18 |
* if you plan to use it from different threads, otherwise it is warranted
|
19 |
* to fail!!</p>
|
20 |
*
|
21 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 18/02/2009
|
22 |
* @author IVER T.I. <http://www.iver.es> 18/02/2009
|
23 |
*/
|
24 |
public class SelectionFieldIterator |
25 |
implements ResettableIterator<Value>, Cloneable { |
26 |
DataSource ds = null; |
27 |
BitSet selectedRows = null; |
28 |
int fieldIndex;
|
29 |
int currBit;
|
30 |
int nextSet;
|
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 |
* @param fieldIndex The index of the field to iterate on
|
41 |
*/
|
42 |
public SelectionFieldIterator(DataSource ds, BitSet selection, int fieldIndex) { |
43 |
this.selectedRows = selection;
|
44 |
this.ds = ds;
|
45 |
this.fieldIndex = fieldIndex;
|
46 |
reset(); |
47 |
} |
48 |
|
49 |
/**
|
50 |
* <p>Creates a new Iterator, which is able to access the provided
|
51 |
* <code>selection</code> from <code>ds</code>.</p>
|
52 |
*
|
53 |
* @param ds
|
54 |
* @param selection The selection of rows to iterate through
|
55 |
* @param fieldIndex The name of the field to iterate on
|
56 |
*/
|
57 |
public SelectionFieldIterator(SelectableDataSource ds, BitSet selection, String fieldName) throws ReadDriverException { |
58 |
this(ds, selection, ds.getFieldIndexByName(fieldName));
|
59 |
} |
60 |
|
61 |
public boolean hasNext() { |
62 |
if (usedNext==true) { |
63 |
currBit = selectedRows.nextSetBit(currBit+1);
|
64 |
if (currBit<0) { |
65 |
hasNext = false;
|
66 |
} |
67 |
else {
|
68 |
hasNext = true;
|
69 |
} |
70 |
usedNext = false;
|
71 |
} |
72 |
return hasNext;
|
73 |
} |
74 |
|
75 |
public Value next() {
|
76 |
if (!hasNext()) {
|
77 |
throw new NoSuchElementException(); |
78 |
} |
79 |
usedNext = true;
|
80 |
try {
|
81 |
return ds.getFieldValue(currBit, fieldIndex);
|
82 |
} catch (ReadDriverException e) {
|
83 |
NoSuchElementException e1 = new NoSuchElementException(); |
84 |
e1.initCause(e); |
85 |
throw e1;
|
86 |
} |
87 |
} |
88 |
|
89 |
public void remove() { |
90 |
throw new UnsupportedOperationException(); |
91 |
} |
92 |
|
93 |
public void reset() { |
94 |
currBit = -1;
|
95 |
usedNext = true;
|
96 |
} |
97 |
|
98 |
public Object clone() throws CloneNotSupportedException { |
99 |
SelectionFieldIterator obj = (SelectionFieldIterator) super.clone();
|
100 |
obj.reset(); |
101 |
return obj;
|
102 |
} |
103 |
} |