Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeatureCollectionBitSet.java @ 21571

History | View | Annotate | Download (5.28 KB)

1
package org.gvsig.data.datastores.vectorial.file.dbf;
2

    
3
import java.lang.reflect.Method;
4
import java.util.ArrayList;
5
import java.util.BitSet;
6
import java.util.Collection;
7
import java.util.Iterator;
8
import java.util.NoSuchElementException;
9

    
10
import org.gvsig.data.ReadException;
11
import org.gvsig.data.vectorial.AbstractFeatureCollection;
12
import org.gvsig.data.vectorial.Feature;
13
import org.gvsig.data.vectorial.FeatureID;
14
import org.gvsig.data.vectorial.FeatureType;
15
//import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
16
import org.gvsig.util.observer.DefaultObservable;
17
import org.opengis.filter.BinaryComparisonOperator;
18
import org.opengis.filter.Filter;
19
import org.opengis.filter.PropertyIsGreaterThan;
20

    
21

    
22
//FIXME: OJO Excepciones
23
public class DBFFeatureCollectionBitSet extends AbstractFeatureCollection {
24
        protected BitSet bitSet=new BitSet();
25
        protected DefaultObservable observable = new DefaultObservable();
26
        protected FeatureType featureType;
27
        protected DBFStore store;
28
        protected Filter filter;
29
        //protected FeatureFilter parser;
30
        protected long driverFeatureCount;
31

    
32
        protected DBFFeatureCollectionBitSet(DBFStore store,FeatureType type, Filter filter) throws ReadException {
33
                this.featureType=type;
34
                this.filter=filter;
35
                /*if (this.filter!=null)
36
                        parser = new FeatureFilter(filter,this.featureType);*/
37
                driverFeatureCount=store.getFeatureCount();
38
                this.store=store;
39
                intilizeFeatureIDs();
40

    
41
        }
42
        
43
        public void showMEthods(Class class1){
44
                System.out.println("--------------");
45
                System.out.println(class1.getName());
46
                Method[] metods = class1.getMethods();
47
                for (int kk=0;kk<metods.length;kk++)
48
                        System.out.println(metods[kk].toString());
49
        }
50

    
51
        protected void intilizeFeatureIDs() throws ReadException {
52
                try{
53
                        bitSet.clear();
54
                        if (filter==null){
55
                                for (int i = 0; i < driverFeatureCount; i++) {
56
                                        bitSet.set(i);
57
                                }
58
                        }else{
59
                                for (int i = 0; i < driverFeatureCount; i++) {
60
                                        FeatureID featureID=(FeatureID)createCurrentFeatureID(i);
61
                                        //TODO:filter previously if possible
62
                                        /*
63
                                        showMEthods(Filter.class);
64
                                        showMEthods(BinaryComparisonOperator.class);
65
                                        showMEthods(PropertyIsGreaterThan.class);
66
                                        showMEthods(filter.getClass());
67
                                        */
68
                                        //filter.evaluate(null);
69
                                        if (filter.evaluate(featureID.getFeature(featureType))){
70
                                                bitSet.set(i);
71
                                        }
72
                                }
73
                        }
74
                }catch (Exception e) {
75
                        throw new ReadException("DBFFeatureCollectionBitSet",e);
76
                }
77
        }
78

    
79
        protected FeatureID createCurrentFeatureID(long pos){
80
                if (pos<driverFeatureCount){
81
                        return new DBFFeatureID((DBFStore)store,pos);
82
                } else {
83
                        return null;
84
                }
85
        }
86

    
87
        public int size() {
88
                checkModified();
89
                return bitSet.cardinality();
90
        }
91

    
92
        public boolean isEmpty() {
93
                checkModified();
94
                return bitSet.isEmpty();
95
        }
96

    
97
        public boolean contains(Object o) {
98
                checkModified();
99
                if (o instanceof Feature){
100
                        return bitSet.get((int)((DBFFeatureID)((Feature)o).getID()).getIndex());
101
                }
102
                return false;
103
        }
104

    
105
        public Iterator iterator() {
106
                checkModified();
107
                DBFBitSetIterator dbfIter=new DBFBitSetIterator(bitSet);
108
                return dbfIter;
109
        }
110

    
111
        public Object[] toArray() {
112
                checkModified();
113
                ArrayList features= new ArrayList();
114
                Iterator iterator= this.iterator();
115
                while(iterator.hasNext()){
116
                        Feature feature=(Feature)iterator.next();
117
                        features.add(feature);
118
                }
119
                return features.toArray();
120
        }
121

    
122
        public Object[] toArray(Object[] a) {
123
                checkModified();
124
                ArrayList features= new ArrayList();
125
                Iterator iterator= this.iterator();
126
                while(iterator.hasNext()){
127
                        Feature feature=(Feature)iterator.next();
128
                        features.add(feature);
129
                }
130
                return features.toArray(a);
131
        }
132

    
133
        public boolean add(Object o) {
134
                throw new UnsupportedOperationException();
135
        }
136

    
137
        public boolean remove(Object o) {
138
                throw new UnsupportedOperationException();
139
        }
140

    
141
        public boolean containsAll(Collection c) {
142
                checkModified();
143
                Iterator iter = c.iterator();
144
                while (iter.hasNext()){
145
                        if (!this.contains(iter.next())){
146
                                return false;
147
                        }
148
                }
149
                return true;
150
        }
151

    
152
        public boolean addAll(Collection c) {
153
                throw new UnsupportedOperationException();
154
        }
155

    
156
        public boolean removeAll(Collection c) {
157
                throw new UnsupportedOperationException();
158
        }
159

    
160
        public boolean retainAll(Collection c) {
161
                throw new UnsupportedOperationException();
162
        }
163

    
164
        public void clear() {
165
                throw new UnsupportedOperationException();
166
        }
167

    
168
        private class DBFBitSetIterator implements Iterator{
169
                private BitSet bs;
170
                private int current=-1;
171
                public DBFBitSetIterator(BitSet bitSet){
172
                        this.bs=bitSet;
173
                }
174
                public boolean hasNext() {
175
                        checkModified();
176
                        return (bs.nextSetBit(current+1) >= 0);
177
                }
178

    
179

    
180
                public Object next() {
181
                        checkModified();
182
                        Feature feature=null;
183
                        if (!this.hasNext()){
184
                                throw new NoSuchElementException();
185
                        }
186

    
187
                        current = bs.nextSetBit(current+1);
188

    
189
                        try {
190
                                feature = createCurrentFeatureID(current).getFeature(featureType);//new DBFFeature(featureType,store,current);
191
                        } catch (ReadException e) {
192
                                throw new RuntimeException(e);
193
                        }
194
                        return feature;
195
                }
196

    
197
                public void remove() {
198
                        throw new UnsupportedOperationException();
199
                }
200
        }
201

    
202
        public void dispose() {
203
                observable.deleteObservers();
204
                this.observable = null;
205
                this.bitSet.clear();
206
                this.featureType = null;
207

    
208
        }
209

    
210
}