svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / featureset / FilteredIterator.java @ 29289
History | View | Annotate | Download (2.84 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.exception.DataEvaluatorException; |
7 |
import org.gvsig.fmap.dal.exception.DataException; |
8 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
9 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
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 |
FeatureProvider data; |
50 |
Object obj;
|
51 |
while (this.getIterator().hasNext()) { |
52 |
obj =this.getIterator().next();
|
53 |
if (obj instanceof FeatureProvider){ |
54 |
data = (FeatureProvider)obj; |
55 |
if (isDeletedOrHasToSkip(data)) {
|
56 |
continue;
|
57 |
} |
58 |
feature = this.createFeature(data);
|
59 |
} else {
|
60 |
feature = (DefaultFeature)obj; |
61 |
if (isDeletedOrHasToSkip(feature.getData())) {
|
62 |
continue;
|
63 |
} |
64 |
} |
65 |
if (this.match(feature)) { |
66 |
this.current = feature;
|
67 |
return;
|
68 |
} |
69 |
} |
70 |
this.current = null; |
71 |
} |
72 |
|
73 |
protected Iterator getIterator() { |
74 |
return this.iterator; |
75 |
} |
76 |
|
77 |
public boolean hasNext() { |
78 |
fset.checkModified(); |
79 |
if (nextChecked) {
|
80 |
return this.current != null; |
81 |
} |
82 |
try {
|
83 |
doNext(); |
84 |
} catch( DataException e) {
|
85 |
NullPointerException ex = new NullPointerException(); |
86 |
ex.initCause(e); |
87 |
throw ex;
|
88 |
} |
89 |
return this.current != null; |
90 |
} |
91 |
|
92 |
public boolean match(DefaultFeature feature) throws DataException { |
93 |
try {
|
94 |
if (filter==null) { |
95 |
return true; |
96 |
} |
97 |
return ((Boolean) this.filter.evaluate(feature)).booleanValue(); |
98 |
} catch (EvaluatorException e) {
|
99 |
throw new DataEvaluatorException(e); |
100 |
} |
101 |
} |
102 |
|
103 |
public Object next() { |
104 |
fset.checkModified(); |
105 |
if (!nextChecked) {
|
106 |
hasNext(); |
107 |
} |
108 |
if (this.current == null) { |
109 |
throw new NoSuchElementException(); |
110 |
} |
111 |
this.lastFeature = null; |
112 |
nextChecked = false;
|
113 |
DefaultFeature feature = this.current;
|
114 |
this.current = null; |
115 |
this.lastFeature = feature;
|
116 |
return feature;
|
117 |
} |
118 |
|
119 |
public void dispose() { |
120 |
super.dispose();
|
121 |
current = null;
|
122 |
filter = null;
|
123 |
} |
124 |
|
125 |
} |