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
History | View | Annotate | Download (5.02 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl.featureset; |
25 | |||
26 | import java.util.Iterator; |
||
27 | import java.util.NoSuchElementException; |
||
28 | 47652 | fdiaz | import java.util.Objects; |
29 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
30 | import org.gvsig.fmap.dal.exception.DataException; |
||
31 | 47652 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureReference; |
32 | 40435 | jjdelcerro | 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 | 45425 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
38 | 40435 | jjdelcerro | 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 | 45989 | fdiaz | FilteredIterator(DefaultFeatureSet featureSet, long index, long elements) |
53 | 40435 | jjdelcerro | throws DataException {
|
54 | 45989 | fdiaz | 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 | 40435 | jjdelcerro | } |
64 | |||
65 | 45425 | jjdelcerro | @Override
|
66 | @SuppressWarnings("empty-statement") |
||
67 | 40435 | jjdelcerro | protected void skypto(long index) { |
68 | // TODO: Comprobar si esta bien la condicion de n<=
|
||
69 | 40629 | jldominguez | for (long n = 0; n < index && this.hasNext(); n++, this.next()) { |
70 | 40435 | jjdelcerro | ; |
71 | } |
||
72 | } |
||
73 | |||
74 | 45425 | jjdelcerro | @Override
|
75 | 40435 | jjdelcerro | 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 | 45522 | fdiaz | if(obj == null){ |
83 | continue;
|
||
84 | } |
||
85 | 40435 | jjdelcerro | if (obj instanceof FeatureProvider){ |
86 | data = (FeatureProvider)obj; |
||
87 | 45522 | fdiaz | if (skipFeature(data)) {
|
88 | 40435 | jjdelcerro | continue;
|
89 | } |
||
90 | feature = this.createFeature(data);
|
||
91 | } else {
|
||
92 | feature = (DefaultFeature)obj; |
||
93 | 45522 | fdiaz | if (skipFeature(feature.getData())) {
|
94 | 40435 | jjdelcerro | continue;
|
95 | } |
||
96 | } |
||
97 | if (this.match(feature)) { |
||
98 | this.current = feature;
|
||
99 | return;
|
||
100 | } |
||
101 | } |
||
102 | this.current = null; |
||
103 | } |
||
104 | |||
105 | 45425 | jjdelcerro | @Override
|
106 | 40435 | jjdelcerro | protected Iterator getIterator() { |
107 | return this.iterator; |
||
108 | } |
||
109 | |||
110 | 42488 | jjdelcerro | @Override
|
111 | 40435 | jjdelcerro | public boolean hasNext() { |
112 | 42488 | jjdelcerro | if( fset == null ) { |
113 | return false; |
||
114 | } |
||
115 | 40435 | jjdelcerro | 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 | 46696 | jjdelcerro | 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 | 47652 | fdiaz | 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 | 40435 | jjdelcerro | |
157 | 45425 | jjdelcerro | @Override
|
158 | 40435 | jjdelcerro | 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 | 45425 | jjdelcerro | @Override
|
175 | 40435 | jjdelcerro | protected void doDispose() throws BaseException { |
176 | super.doDispose();
|
||
177 | current = null;
|
||
178 | filter = null;
|
||
179 | } |
||
180 | |||
181 | } |