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

History | View | Annotate | Download (2.93 KB)

1
package org.gvsig.fmap.dal.feature.impl.featureset;
2

    
3
import java.util.Iterator;
4
import java.util.NoSuchElementException;
5

    
6
import org.gvsig.fmap.dal.exception.DataEvaluatorException;
7
import org.gvsig.fmap.dal.exception.DataException;
8
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
9
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
10
import org.gvsig.tools.evaluator.Evaluator;
11
import org.gvsig.tools.evaluator.EvaluatorException;
12
import org.gvsig.tools.exception.BaseException;
13

    
14
public class FilteredIterator extends DefaultIterator {
15

    
16
        protected boolean nextChecked;
17
        protected DefaultFeature current;
18
        protected Evaluator filter;
19

    
20

    
21
        protected FilteredIterator(DefaultFeatureSet featureSet) {
22
                super(featureSet);
23
                this.current = null;
24
                this.nextChecked = false;
25
                this.filter = featureSet.query.getFilter();
26
        }
27

    
28
        FilteredIterator(DefaultFeatureSet featureSet, long index)
29
                        throws DataException {
30
                super(featureSet);
31
                this.iterator = featureSet.provider.iterator();
32
                if (index > 0) {
33
                        this.skypto(index);
34
                }
35
                this.current = null;
36
                this.nextChecked = false;
37
                this.filter = featureSet.query.getFilter();
38
        }
39

    
40
        protected void skypto(long index) {
41
                // TODO: Comprobar si esta bien la condicion de n<=
42
                for (long n = 0; n <= index && this.hasNext(); n++, this.next()) {
43
                        ;
44
                }
45
        }
46

    
47
        protected void doNext() throws DataException {
48
                nextChecked = true;
49
                DefaultFeature feature;
50
                FeatureProvider data;
51
                Object obj;
52
                while (this.getIterator().hasNext()) {
53
                        obj =this.getIterator().next();
54
                        if (obj instanceof FeatureProvider){
55
                                data = (FeatureProvider)obj;
56
                                if (isDeletedOrHasToSkip(data)) {
57
                                        continue;
58
                                }
59
                                feature = this.createFeature(data);
60
                        } else {
61
                                feature = (DefaultFeature)obj;
62
                                if (isDeletedOrHasToSkip(feature.getData())) {
63
                                        continue;
64
                                }
65
                        }
66
                        if (this.match(feature)) {
67
                                this.current = feature;
68
                                return;
69
                        }
70
                }
71
                this.current = null;
72
        }
73

    
74
        protected Iterator getIterator() {
75
                return this.iterator;
76
        }
77

    
78
        public boolean hasNext() {
79
                fset.checkSourceStoreModified();
80
                if (nextChecked) {
81
                        return this.current != null;
82
                }
83
                try {
84
                        doNext();
85
                } catch( DataException e) {
86
                        NullPointerException ex = new NullPointerException();
87
                        ex.initCause(e);
88
                        throw ex;
89
                }
90
                return this.current != null;
91
        }
92

    
93
        public boolean match(DefaultFeature feature) throws DataException {
94
                try {
95
                        if (filter==null) {
96
                                return true;
97
                        }
98
                        return ((Boolean) this.filter.evaluate(feature)).booleanValue();
99
                } catch (EvaluatorException e) {
100
                        throw new DataEvaluatorException(e);
101
                }
102
        }
103

    
104
        public Object next() {
105
                fset.checkSourceStoreModified();
106
                if (!nextChecked) {
107
                        hasNext();
108
                }
109
                if (this.current == null) {
110
                        throw new NoSuchElementException();
111
                }
112
                this.lastFeature = null;
113
                nextChecked = false;
114
                DefaultFeature feature = this.current;
115
                this.current = null;
116
                this.lastFeature = feature;
117
                return feature;
118
        }
119

    
120
        protected void doDispose() throws BaseException {
121
                super.doDispose();
122
                current = null;
123
                filter = null;
124
        }
125

    
126
}