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 @ 47652
History | View | Annotate | Download (5.02 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 java.util.Objects; |
29 |
import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
32 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
33 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
34 |
import org.gvsig.tools.evaluator.Evaluator; |
35 |
import org.gvsig.tools.exception.BaseException; |
36 |
|
37 |
@SuppressWarnings("UseSpecificCatch") |
38 |
public class FilteredIterator extends DefaultIterator { |
39 |
|
40 |
protected boolean nextChecked; |
41 |
protected DefaultFeature current;
|
42 |
protected Evaluator filter;
|
43 |
|
44 |
|
45 |
protected FilteredIterator(DefaultFeatureSet featureSet) {
|
46 |
super(featureSet);
|
47 |
this.current = null; |
48 |
this.nextChecked = false; |
49 |
this.filter = featureSet.query.getFilter();
|
50 |
} |
51 |
|
52 |
FilteredIterator(DefaultFeatureSet featureSet, long index, long elements) |
53 |
throws DataException {
|
54 |
this(featureSet);
|
55 |
if(featureSet.provider.canFilter() && featureSet.provider.canIterateFromIndex()){
|
56 |
this.iterator = featureSet.provider.iterator(index, elements);
|
57 |
} else {
|
58 |
this.iterator = featureSet.provider.iterator();
|
59 |
if (index > 0) { |
60 |
this.skypto(index);
|
61 |
} |
62 |
} |
63 |
} |
64 |
|
65 |
@Override
|
66 |
@SuppressWarnings("empty-statement") |
67 |
protected void skypto(long index) { |
68 |
// TODO: Comprobar si esta bien la condicion de n<=
|
69 |
for (long n = 0; n < index && this.hasNext(); n++, this.next()) { |
70 |
; |
71 |
} |
72 |
} |
73 |
|
74 |
@Override
|
75 |
protected void doNext() throws DataException { |
76 |
nextChecked = true;
|
77 |
DefaultFeature feature; |
78 |
FeatureProvider data; |
79 |
Object obj;
|
80 |
while (this.getIterator().hasNext()) { |
81 |
obj =this.getIterator().next();
|
82 |
if(obj == null){ |
83 |
continue;
|
84 |
} |
85 |
if (obj instanceof FeatureProvider){ |
86 |
data = (FeatureProvider)obj; |
87 |
if (skipFeature(data)) {
|
88 |
continue;
|
89 |
} |
90 |
feature = this.createFeature(data);
|
91 |
} else {
|
92 |
feature = (DefaultFeature)obj; |
93 |
if (skipFeature(feature.getData())) {
|
94 |
continue;
|
95 |
} |
96 |
} |
97 |
if (this.match(feature)) { |
98 |
this.current = feature;
|
99 |
return;
|
100 |
} |
101 |
} |
102 |
this.current = null; |
103 |
} |
104 |
|
105 |
@Override
|
106 |
protected Iterator getIterator() { |
107 |
return this.iterator; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public boolean hasNext() { |
112 |
if( fset == null ) { |
113 |
return false; |
114 |
} |
115 |
fset.checkSourceStoreModified(); |
116 |
if (nextChecked) {
|
117 |
return this.current != null; |
118 |
} |
119 |
try {
|
120 |
doNext(); |
121 |
} catch( DataException e) {
|
122 |
NullPointerException ex = new NullPointerException(); |
123 |
ex.initCause(e); |
124 |
throw ex;
|
125 |
} |
126 |
return this.current != null; |
127 |
} |
128 |
|
129 |
protected boolean toBooleanMatch(Object x) { |
130 |
if( x == null ) { |
131 |
return false; |
132 |
} else if( x instanceof Boolean ) { |
133 |
return ((Boolean) x); |
134 |
} else {
|
135 |
return true; |
136 |
} |
137 |
} |
138 |
|
139 |
public boolean match(DefaultFeature feature) throws DataException { |
140 |
try {
|
141 |
if (filter == null) { |
142 |
return true; |
143 |
} |
144 |
Object x = this.filter.evaluate(feature); |
145 |
return toBooleanMatch(x);
|
146 |
} catch (Exception e) { |
147 |
FeatureReference ref = null;
|
148 |
try {
|
149 |
ref = feature.getReference(); |
150 |
} catch (Throwable th) { |
151 |
//Do nothing
|
152 |
} |
153 |
throw new DataEvaluatorException(e, Objects.toString(ref)); |
154 |
} |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public Object next() { |
159 |
fset.checkSourceStoreModified(); |
160 |
if (!nextChecked) {
|
161 |
hasNext(); |
162 |
} |
163 |
if (this.current == null) { |
164 |
throw new NoSuchElementException(); |
165 |
} |
166 |
this.lastFeature = null; |
167 |
nextChecked = false;
|
168 |
DefaultFeature feature = this.current;
|
169 |
this.current = null; |
170 |
this.lastFeature = feature;
|
171 |
return feature;
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
protected void doDispose() throws BaseException { |
176 |
super.doDispose();
|
177 |
current = null;
|
178 |
filter = null;
|
179 |
} |
180 |
|
181 |
} |