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

History | View | Annotate | Download (3.97 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

    
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
        @Override
102
        public boolean hasNext() {
103
                if( fset == null ) {
104
                    return false;
105
                }
106
                fset.checkSourceStoreModified();
107
                if (nextChecked) {
108
                        return this.current != null;
109
                }
110
                try {
111
                        doNext();
112
                } catch( DataException e) {
113
                        NullPointerException ex = new NullPointerException();
114
                        ex.initCause(e);
115
                        throw ex;
116
                }
117
                return this.current != null;
118
        }
119

    
120
        public boolean match(DefaultFeature feature) throws DataException {
121
                try {
122
                        if (filter==null) {
123
                                return true;
124
                        }
125
                        return ((Boolean) this.filter.evaluate(feature)).booleanValue();
126
                } catch (EvaluatorException e) {
127
                        throw new DataEvaluatorException(e);
128
                }
129
        }
130

    
131
        public Object next() {
132
                fset.checkSourceStoreModified();
133
                if (!nextChecked) {
134
                        hasNext();
135
                }
136
                if (this.current == null) {
137
                        throw new NoSuchElementException();
138
                }
139
                this.lastFeature = null;
140
                nextChecked = false;
141
                DefaultFeature feature = this.current;
142
                this.current = null;
143
                this.lastFeature = feature;
144
                return feature;
145
        }
146

    
147
        protected void doDispose() throws BaseException {
148
                super.doDispose();
149
                current = null;
150
                filter = null;
151
        }
152

    
153
}