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 @ 40559
History | View | Annotate | Download (3.87 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 | |||
29 | import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
||
30 | import org.gvsig.fmap.dal.exception.DataException; |
||
31 | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
||
32 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
33 | import org.gvsig.tools.evaluator.Evaluator; |
||
34 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
35 | import org.gvsig.tools.exception.BaseException; |
||
36 | |||
37 | public class FilteredIterator extends DefaultIterator { |
||
38 | |||
39 | protected boolean nextChecked; |
||
40 | protected DefaultFeature current;
|
||
41 | protected Evaluator filter;
|
||
42 | |||
43 | |||
44 | protected FilteredIterator(DefaultFeatureSet featureSet) {
|
||
45 | super(featureSet);
|
||
46 | this.current = null; |
||
47 | this.nextChecked = false; |
||
48 | this.filter = featureSet.query.getFilter();
|
||
49 | } |
||
50 | |||
51 | FilteredIterator(DefaultFeatureSet featureSet, long index)
|
||
52 | throws DataException {
|
||
53 | super(featureSet);
|
||
54 | this.iterator = featureSet.provider.iterator();
|
||
55 | if (index > 0) { |
||
56 | this.skypto(index);
|
||
57 | } |
||
58 | this.current = null; |
||
59 | this.nextChecked = false; |
||
60 | this.filter = featureSet.query.getFilter();
|
||
61 | } |
||
62 | |||
63 | protected void skypto(long index) { |
||
64 | // TODO: Comprobar si esta bien la condicion de n<=
|
||
65 | for (long n = 0; n <= index && this.hasNext(); n++, this.next()) { |
||
66 | ; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | protected void doNext() throws DataException { |
||
71 | nextChecked = true;
|
||
72 | DefaultFeature feature; |
||
73 | FeatureProvider data; |
||
74 | Object obj;
|
||
75 | while (this.getIterator().hasNext()) { |
||
76 | obj =this.getIterator().next();
|
||
77 | if (obj instanceof FeatureProvider){ |
||
78 | data = (FeatureProvider)obj; |
||
79 | if (isDeletedOrHasToSkip(data)) {
|
||
80 | continue;
|
||
81 | } |
||
82 | feature = this.createFeature(data);
|
||
83 | } else {
|
||
84 | feature = (DefaultFeature)obj; |
||
85 | if (isDeletedOrHasToSkip(feature.getData())) {
|
||
86 | continue;
|
||
87 | } |
||
88 | } |
||
89 | if (this.match(feature)) { |
||
90 | this.current = feature;
|
||
91 | return;
|
||
92 | } |
||
93 | } |
||
94 | this.current = null; |
||
95 | } |
||
96 | |||
97 | protected Iterator getIterator() { |
||
98 | return this.iterator; |
||
99 | } |
||
100 | |||
101 | public boolean hasNext() { |
||
102 | fset.checkSourceStoreModified(); |
||
103 | if (nextChecked) {
|
||
104 | return this.current != null; |
||
105 | } |
||
106 | try {
|
||
107 | doNext(); |
||
108 | } catch( DataException e) {
|
||
109 | NullPointerException ex = new NullPointerException(); |
||
110 | ex.initCause(e); |
||
111 | throw ex;
|
||
112 | } |
||
113 | return this.current != null; |
||
114 | } |
||
115 | |||
116 | public boolean match(DefaultFeature feature) throws DataException { |
||
117 | try {
|
||
118 | if (filter==null) { |
||
119 | return true; |
||
120 | } |
||
121 | return ((Boolean) this.filter.evaluate(feature)).booleanValue(); |
||
122 | } catch (EvaluatorException e) {
|
||
123 | throw new DataEvaluatorException(e); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public Object next() { |
||
128 | fset.checkSourceStoreModified(); |
||
129 | if (!nextChecked) {
|
||
130 | hasNext(); |
||
131 | } |
||
132 | if (this.current == null) { |
||
133 | throw new NoSuchElementException(); |
||
134 | } |
||
135 | this.lastFeature = null; |
||
136 | nextChecked = false;
|
||
137 | DefaultFeature feature = this.current;
|
||
138 | this.current = null; |
||
139 | this.lastFeature = feature;
|
||
140 | return feature;
|
||
141 | } |
||
142 | |||
143 | protected void doDispose() throws BaseException { |
||
144 | super.doDispose();
|
||
145 | current = null;
|
||
146 | filter = null;
|
||
147 | } |
||
148 | |||
149 | } |