Statistics
| Revision:

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 @ 45522

History | View | Annotate | Download (4.47 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 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
@SuppressWarnings("UseSpecificCatch")
36
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
        FilteredIterator(DefaultFeatureSet featureSet, long index)
51
                        throws DataException {
52
                super(featureSet);
53
                this.iterator = featureSet.provider.iterator();
54
                if (index > 0) {
55
                        this.skypto(index);
56
                }
57
                this.current = null;
58
                this.nextChecked = false;
59
                this.filter = featureSet.query.getFilter();
60
        }
61

    
62
        @Override
63
        @SuppressWarnings("empty-statement")
64
        protected void skypto(long index) {
65
                // TODO: Comprobar si esta bien la condicion de n<=
66
                for (long n = 0; n < index && this.hasNext(); n++, this.next()) {
67
                        ;
68
                }
69
        }
70

    
71
        @Override
72
        protected void doNext() throws DataException {
73
                nextChecked = true;
74
                DefaultFeature feature;
75
                FeatureProvider data;
76
                Object obj;
77
                while (this.getIterator().hasNext()) {
78
                        obj =this.getIterator().next();
79
                        if(obj == null){
80
                            continue;
81
                        }
82
                        if (obj instanceof FeatureProvider){
83
                                data = (FeatureProvider)obj;
84
                                if (skipFeature(data)) {
85
                                        continue;
86
                                }
87
                                feature = this.createFeature(data);
88
                        } else {
89
                                feature = (DefaultFeature)obj;
90
                                if (skipFeature(feature.getData())) {
91
                                        continue;
92
                                }
93
                        }
94
                        if (this.match(feature)) {
95
                                this.current = feature;
96
                                return;
97
                        }
98
                }
99
                this.current = null;
100
        }
101

    
102
        @Override
103
        protected Iterator getIterator() {
104
                return this.iterator;
105
        }
106

    
107
        @Override
108
        public boolean hasNext() {
109
                if( fset == null ) {
110
                    return false;
111
                }
112
                fset.checkSourceStoreModified();
113
                if (nextChecked) {
114
                        return this.current != null;
115
                }
116
                try {
117
                        doNext();
118
                } catch( DataException e) {
119
                        NullPointerException ex = new NullPointerException();
120
                        ex.initCause(e);
121
                        throw ex;
122
                }
123
                return this.current != null;
124
        }
125

    
126
        public boolean match(DefaultFeature feature) throws DataException {
127
                try {
128
                        if (filter==null) {
129
                            return true;
130
                        }
131
                        Object x = this.filter.evaluate(feature);
132
                        if( x == null ) {
133
                            return false;
134
                        } else if( x instanceof Boolean ) {
135
                            return ((Boolean) x);
136
                        } else {
137
                            return true;
138
                        }
139
                } catch (Exception e) {
140
                        throw new DataEvaluatorException(e);
141
                }
142
        }
143

    
144
        @Override
145
        public Object next() {
146
                fset.checkSourceStoreModified();
147
                if (!nextChecked) {
148
                        hasNext();
149
                }
150
                if (this.current == null) {
151
                        throw new NoSuchElementException();
152
                }
153
                this.lastFeature = null;
154
                nextChecked = false;
155
                DefaultFeature feature = this.current;
156
                this.current = null;
157
                this.lastFeature = feature;
158
                return feature;
159
        }
160

    
161
        @Override
162
        protected void doDispose() throws BaseException {
163
                super.doDispose();
164
                current = null;
165
                filter = null;
166
        }
167

    
168
}