svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / featureset / FilteredIterator.java @ 47248
History | View | Annotate | Download (4.73 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.impl.featureset; |
25 |
|
26 |
import java.util.Iterator; |
27 |
import java.util.NoSuchElementException; |
28 |
import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
31 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
32 |
import org.gvsig.tools.evaluator.Evaluator; |
33 |
import org.gvsig.tools.exception.BaseException; |
34 |
|
35 |
@SuppressWarnings("UseSpecificCatch") |
36 |
public class FilteredIterator extends DefaultIterator { |
37 |
|
38 |
protected boolean nextChecked; |
39 |
protected DefaultFeature current;
|
40 |
protected Evaluator filter;
|
41 |
|
42 |
|
43 |
protected FilteredIterator(DefaultFeatureSet featureSet) {
|
44 |
super(featureSet);
|
45 |
this.current = null; |
46 |
this.nextChecked = false; |
47 |
this.filter = featureSet.query.getFilter();
|
48 |
} |
49 |
|
50 |
FilteredIterator(DefaultFeatureSet featureSet, long index, long elements) |
51 |
throws DataException {
|
52 |
this(featureSet);
|
53 |
if(featureSet.provider.canFilter() && featureSet.provider.canIterateFromIndex()){
|
54 |
this.iterator = featureSet.provider.iterator(index, elements);
|
55 |
} else {
|
56 |
this.iterator = featureSet.provider.iterator();
|
57 |
if (index > 0) { |
58 |
this.skypto(index);
|
59 |
} |
60 |
} |
61 |
} |
62 |
|
63 |
@Override
|
64 |
@SuppressWarnings("empty-statement") |
65 |
protected void skypto(long index) { |
66 |
// TODO: Comprobar si esta bien la condicion de n<=
|
67 |
for (long n = 0; n < index && this.hasNext(); n++, this.next()) { |
68 |
; |
69 |
} |
70 |
} |
71 |
|
72 |
@Override
|
73 |
protected void doNext() throws DataException { |
74 |
nextChecked = true;
|
75 |
DefaultFeature feature; |
76 |
FeatureProvider data; |
77 |
Object obj;
|
78 |
while (this.getIterator().hasNext()) { |
79 |
obj =this.getIterator().next();
|
80 |
if(obj == null){ |
81 |
continue;
|
82 |
} |
83 |
if (obj instanceof FeatureProvider){ |
84 |
data = (FeatureProvider)obj; |
85 |
if (skipFeature(data)) {
|
86 |
continue;
|
87 |
} |
88 |
feature = this.createFeature(data);
|
89 |
} else {
|
90 |
feature = (DefaultFeature)obj; |
91 |
if (skipFeature(feature.getData())) {
|
92 |
continue;
|
93 |
} |
94 |
} |
95 |
if (this.match(feature)) { |
96 |
this.current = feature;
|
97 |
return;
|
98 |
} |
99 |
} |
100 |
this.current = null; |
101 |
} |
102 |
|
103 |
@Override
|
104 |
protected Iterator getIterator() { |
105 |
return this.iterator; |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean hasNext() { |
110 |
if( fset == null ) { |
111 |
return false; |
112 |
} |
113 |
fset.checkSourceStoreModified(); |
114 |
if (nextChecked) {
|
115 |
return this.current != null; |
116 |
} |
117 |
try {
|
118 |
doNext(); |
119 |
} catch( DataException e) {
|
120 |
NullPointerException ex = new NullPointerException(); |
121 |
ex.initCause(e); |
122 |
throw ex;
|
123 |
} |
124 |
return this.current != null; |
125 |
} |
126 |
|
127 |
protected boolean toBooleanMatch(Object x) { |
128 |
if( x == null ) { |
129 |
return false; |
130 |
} else if( x instanceof Boolean ) { |
131 |
return ((Boolean) x); |
132 |
} else {
|
133 |
return true; |
134 |
} |
135 |
} |
136 |
|
137 |
public boolean match(DefaultFeature feature) throws DataException { |
138 |
try {
|
139 |
if (filter==null) { |
140 |
return true; |
141 |
} |
142 |
Object x = this.filter.evaluate(feature); |
143 |
return toBooleanMatch(x);
|
144 |
} catch (Exception e) { |
145 |
throw new DataEvaluatorException(e); |
146 |
} |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public Object next() { |
151 |
fset.checkSourceStoreModified(); |
152 |
if (!nextChecked) {
|
153 |
hasNext(); |
154 |
} |
155 |
if (this.current == null) { |
156 |
throw new NoSuchElementException(); |
157 |
} |
158 |
this.lastFeature = null; |
159 |
nextChecked = false;
|
160 |
DefaultFeature feature = this.current;
|
161 |
this.current = null; |
162 |
this.lastFeature = feature;
|
163 |
return feature;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected void doDispose() throws BaseException { |
168 |
super.doDispose();
|
169 |
current = null;
|
170 |
filter = null;
|
171 |
} |
172 |
|
173 |
} |