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 @ 46696
History | View | Annotate | Download (4.73 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 | 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 | 45425 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
36 | 40435 | jjdelcerro | 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 | 45989 | fdiaz | FilteredIterator(DefaultFeatureSet featureSet, long index, long elements) |
51 | 40435 | jjdelcerro | throws DataException {
|
52 | 45989 | fdiaz | 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 | 40435 | jjdelcerro | } |
62 | |||
63 | 45425 | jjdelcerro | @Override
|
64 | @SuppressWarnings("empty-statement") |
||
65 | 40435 | jjdelcerro | protected void skypto(long index) { |
66 | // TODO: Comprobar si esta bien la condicion de n<=
|
||
67 | 40629 | jldominguez | for (long n = 0; n < index && this.hasNext(); n++, this.next()) { |
68 | 40435 | jjdelcerro | ; |
69 | } |
||
70 | } |
||
71 | |||
72 | 45425 | jjdelcerro | @Override
|
73 | 40435 | jjdelcerro | 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 | 45522 | fdiaz | if(obj == null){ |
81 | continue;
|
||
82 | } |
||
83 | 40435 | jjdelcerro | if (obj instanceof FeatureProvider){ |
84 | data = (FeatureProvider)obj; |
||
85 | 45522 | fdiaz | if (skipFeature(data)) {
|
86 | 40435 | jjdelcerro | continue;
|
87 | } |
||
88 | feature = this.createFeature(data);
|
||
89 | } else {
|
||
90 | feature = (DefaultFeature)obj; |
||
91 | 45522 | fdiaz | if (skipFeature(feature.getData())) {
|
92 | 40435 | jjdelcerro | continue;
|
93 | } |
||
94 | } |
||
95 | if (this.match(feature)) { |
||
96 | this.current = feature;
|
||
97 | return;
|
||
98 | } |
||
99 | } |
||
100 | this.current = null; |
||
101 | } |
||
102 | |||
103 | 45425 | jjdelcerro | @Override
|
104 | 40435 | jjdelcerro | protected Iterator getIterator() { |
105 | return this.iterator; |
||
106 | } |
||
107 | |||
108 | 42488 | jjdelcerro | @Override
|
109 | 40435 | jjdelcerro | public boolean hasNext() { |
110 | 42488 | jjdelcerro | if( fset == null ) { |
111 | return false; |
||
112 | } |
||
113 | 40435 | jjdelcerro | 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 | 46696 | jjdelcerro | 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 | 40435 | jjdelcerro | public boolean match(DefaultFeature feature) throws DataException { |
138 | try {
|
||
139 | if (filter==null) { |
||
140 | 44207 | jjdelcerro | return true; |
141 | 40435 | jjdelcerro | } |
142 | 44207 | jjdelcerro | Object x = this.filter.evaluate(feature); |
143 | 46696 | jjdelcerro | return toBooleanMatch(x);
|
144 | 45425 | jjdelcerro | } catch (Exception e) { |
145 | 40435 | jjdelcerro | throw new DataEvaluatorException(e); |
146 | } |
||
147 | } |
||
148 | |||
149 | 45425 | jjdelcerro | @Override
|
150 | 40435 | jjdelcerro | 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 | 45425 | jjdelcerro | @Override
|
167 | 40435 | jjdelcerro | protected void doDispose() throws BaseException { |
168 | super.doDispose();
|
||
169 | current = null;
|
||
170 | filter = null;
|
||
171 | } |
||
172 | |||
173 | } |