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 @ 44207
History | View | Annotate | Download (4.33 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 | 44207 | jjdelcerro | import org.apache.commons.lang3.BooleanUtils; |
29 | 40435 | jjdelcerro | |
30 | import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
||
31 | import org.gvsig.fmap.dal.exception.DataException; |
||
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.evaluator.EvaluatorException; |
||
36 | import org.gvsig.tools.exception.BaseException; |
||
37 | |||
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)
|
||
53 | throws DataException {
|
||
54 | super(featureSet);
|
||
55 | this.iterator = featureSet.provider.iterator();
|
||
56 | if (index > 0) { |
||
57 | this.skypto(index);
|
||
58 | } |
||
59 | this.current = null; |
||
60 | this.nextChecked = false; |
||
61 | this.filter = featureSet.query.getFilter();
|
||
62 | } |
||
63 | |||
64 | protected void skypto(long index) { |
||
65 | // TODO: Comprobar si esta bien la condicion de n<=
|
||
66 | 40629 | jldominguez | for (long n = 0; n < index && this.hasNext(); n++, this.next()) { |
67 | 40435 | jjdelcerro | ; |
68 | } |
||
69 | } |
||
70 | |||
71 | protected void doNext() throws DataException { |
||
72 | nextChecked = true;
|
||
73 | DefaultFeature feature; |
||
74 | FeatureProvider data; |
||
75 | Object obj;
|
||
76 | while (this.getIterator().hasNext()) { |
||
77 | obj =this.getIterator().next();
|
||
78 | if (obj instanceof FeatureProvider){ |
||
79 | data = (FeatureProvider)obj; |
||
80 | if (isDeletedOrHasToSkip(data)) {
|
||
81 | continue;
|
||
82 | } |
||
83 | feature = this.createFeature(data);
|
||
84 | } else {
|
||
85 | feature = (DefaultFeature)obj; |
||
86 | if (isDeletedOrHasToSkip(feature.getData())) {
|
||
87 | continue;
|
||
88 | } |
||
89 | } |
||
90 | if (this.match(feature)) { |
||
91 | this.current = feature;
|
||
92 | return;
|
||
93 | } |
||
94 | } |
||
95 | this.current = null; |
||
96 | } |
||
97 | |||
98 | protected Iterator getIterator() { |
||
99 | return this.iterator; |
||
100 | } |
||
101 | |||
102 | 42488 | jjdelcerro | @Override
|
103 | 40435 | jjdelcerro | public boolean hasNext() { |
104 | 42488 | jjdelcerro | if( fset == null ) { |
105 | return false; |
||
106 | } |
||
107 | 40435 | jjdelcerro | fset.checkSourceStoreModified(); |
108 | if (nextChecked) {
|
||
109 | return this.current != null; |
||
110 | } |
||
111 | try {
|
||
112 | doNext(); |
||
113 | } catch( DataException e) {
|
||
114 | NullPointerException ex = new NullPointerException(); |
||
115 | ex.initCause(e); |
||
116 | throw ex;
|
||
117 | } |
||
118 | return this.current != null; |
||
119 | } |
||
120 | |||
121 | public boolean match(DefaultFeature feature) throws DataException { |
||
122 | try {
|
||
123 | if (filter==null) { |
||
124 | 44207 | jjdelcerro | return true; |
125 | 40435 | jjdelcerro | } |
126 | 44207 | jjdelcerro | Object x = this.filter.evaluate(feature); |
127 | if( x == null ) { |
||
128 | return false; |
||
129 | } else if( x instanceof Boolean ) { |
||
130 | return ((Boolean) x); |
||
131 | } else {
|
||
132 | return true; |
||
133 | } |
||
134 | 40435 | jjdelcerro | } catch (EvaluatorException e) {
|
135 | throw new DataEvaluatorException(e); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public Object next() { |
||
140 | fset.checkSourceStoreModified(); |
||
141 | if (!nextChecked) {
|
||
142 | hasNext(); |
||
143 | } |
||
144 | if (this.current == null) { |
||
145 | throw new NoSuchElementException(); |
||
146 | } |
||
147 | this.lastFeature = null; |
||
148 | nextChecked = false;
|
||
149 | DefaultFeature feature = this.current;
|
||
150 | this.current = null; |
||
151 | this.lastFeature = feature;
|
||
152 | return feature;
|
||
153 | } |
||
154 | |||
155 | protected void doDispose() throws BaseException { |
||
156 | super.doDispose();
|
||
157 | current = null;
|
||
158 | filter = null;
|
||
159 | } |
||
160 | |||
161 | } |