root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / featureSet / FilteredIterator.java @ 24496
History | View | Annotate | Download (2.45 KB)
1 |
package org.gvsig.fmap.dal.feature.impl.featureSet; |
---|---|
2 |
|
3 |
import java.util.Iterator; |
4 |
import java.util.NoSuchElementException; |
5 |
|
6 |
import org.gvsig.fmap.dal.exceptions.DataEvaluatorException; |
7 |
import org.gvsig.fmap.dal.exceptions.DataException; |
8 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
9 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
10 |
import org.gvsig.tools.evaluator.Evaluator; |
11 |
import org.gvsig.tools.evaluator.EvaluatorException; |
12 |
|
13 |
public class FilteredIterator extends DefaultIterator { |
14 |
|
15 |
protected boolean nextChecked; |
16 |
protected DefaultFeature current;
|
17 |
protected Evaluator filter;
|
18 |
|
19 |
|
20 |
protected FilteredIterator(DefaultFeatureSet featureSet) {
|
21 |
super(featureSet);
|
22 |
this.current = null; |
23 |
this.nextChecked = false; |
24 |
this.filter = featureSet.query.getFilter();
|
25 |
} |
26 |
|
27 |
FilteredIterator(DefaultFeatureSet featureSet, long index)
|
28 |
throws DataException {
|
29 |
super(featureSet);
|
30 |
this.iterator = featureSet.provider.iterator();
|
31 |
if (index > 0) { |
32 |
this.skypto(index);
|
33 |
} |
34 |
this.current = null; |
35 |
this.nextChecked = false; |
36 |
this.filter = featureSet.query.getFilter();
|
37 |
} |
38 |
|
39 |
protected void skypto(long index) { |
40 |
// TODO: Comprobar si esta bien la condicion de n<=
|
41 |
for (long n = 0; n <= index && this.hasNext(); n++, this.next()) { |
42 |
; |
43 |
} |
44 |
} |
45 |
|
46 |
protected void doNext() throws DataException { |
47 |
nextChecked = true;
|
48 |
DefaultFeature feature; |
49 |
FeatureData data; |
50 |
while (this.getIterator().hasNext()) { |
51 |
data = (FeatureData) this.getIterator().next();
|
52 |
if (isDeleted(data)) {
|
53 |
continue;
|
54 |
} |
55 |
feature = this.createFeature(data);
|
56 |
if (this.match(feature)) { |
57 |
this.current = feature;
|
58 |
return;
|
59 |
} |
60 |
} |
61 |
this.current = null; |
62 |
} |
63 |
|
64 |
protected Iterator getIterator() { |
65 |
return this.iterator; |
66 |
} |
67 |
|
68 |
public boolean hasNext() { |
69 |
fset.checkModified(); |
70 |
if (nextChecked) {
|
71 |
return this.current != null; |
72 |
} |
73 |
try {
|
74 |
doNext(); |
75 |
} catch( DataException e) {
|
76 |
NullPointerException ex = new NullPointerException(); |
77 |
ex.initCause(e); |
78 |
throw ex;
|
79 |
} |
80 |
return this.current != null; |
81 |
} |
82 |
|
83 |
public boolean match(DefaultFeature feature) throws DataException { |
84 |
try {
|
85 |
return ((Boolean) this.filter.evaluate(feature)).booleanValue(); |
86 |
} catch (EvaluatorException e) {
|
87 |
throw new DataEvaluatorException(e); |
88 |
} |
89 |
} |
90 |
|
91 |
public Object next() { |
92 |
fset.checkModified(); |
93 |
if (!nextChecked) {
|
94 |
hasNext(); |
95 |
} |
96 |
if (this.current == null) { |
97 |
throw new NoSuchElementException(); |
98 |
} |
99 |
nextChecked = false;
|
100 |
DefaultFeature feature = this.current;
|
101 |
this.current = null; |
102 |
return feature;
|
103 |
} |
104 |
|
105 |
} |