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 20437 vcaballero
package org.gvsig.data.datastores.vectorial.file.dbf;
2
3 21571 jcarrasco
import java.lang.reflect.Method;
4 20437 vcaballero
import java.util.ArrayList;
5
import java.util.BitSet;
6
import java.util.Collection;
7
import java.util.Iterator;
8 20467 jmvivo
import java.util.NoSuchElementException;
9 20437 vcaballero
10 20971 vcaballero
import org.gvsig.data.ReadException;
11 21045 jmvivo
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 21571 jcarrasco
//import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
16 21045 jmvivo
import org.gvsig.util.observer.DefaultObservable;
17 21571 jcarrasco
import org.opengis.filter.BinaryComparisonOperator;
18
import org.opengis.filter.Filter;
19
import org.opengis.filter.PropertyIsGreaterThan;
20 20437 vcaballero
21
22
//FIXME: OJO Excepciones
23 21045 jmvivo
public class DBFFeatureCollectionBitSet extends AbstractFeatureCollection {
24 20437 vcaballero
        protected BitSet bitSet=new BitSet();
25 21045 jmvivo
        protected DefaultObservable observable = new DefaultObservable();
26
        protected FeatureType featureType;
27 20600 vcaballero
        protected DBFStore store;
28 21571 jcarrasco
        protected Filter filter;
29
        //protected FeatureFilter parser;
30 20600 vcaballero
        protected long driverFeatureCount;
31 20437 vcaballero
32 21571 jcarrasco
        protected DBFFeatureCollectionBitSet(DBFStore store,FeatureType type, Filter filter) throws ReadException {
33 20437 vcaballero
                this.featureType=type;
34
                this.filter=filter;
35 21571 jcarrasco
                /*if (this.filter!=null)
36
                        parser = new FeatureFilter(filter,this.featureType);*/
37 20437 vcaballero
                driverFeatureCount=store.getFeatureCount();
38
                this.store=store;
39
                intilizeFeatureIDs();
40
41
        }
42 21571 jcarrasco
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 20437 vcaballero
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 21045 jmvivo
                                        FeatureID featureID=(FeatureID)createCurrentFeatureID(i);
61 21571 jcarrasco
                                        //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 20437 vcaballero
                                                bitSet.set(i);
71
                                        }
72
                                }
73
                        }
74
                }catch (Exception e) {
75
                        throw new ReadException("DBFFeatureCollectionBitSet",e);
76
                }
77
        }
78
79 21045 jmvivo
        protected FeatureID createCurrentFeatureID(long pos){
80 20437 vcaballero
                if (pos<driverFeatureCount){
81
                        return new DBFFeatureID((DBFStore)store,pos);
82
                } else {
83
                        return null;
84
                }
85
        }
86
87
        public int size() {
88 20692 jmvivo
                checkModified();
89 20437 vcaballero
                return bitSet.cardinality();
90
        }
91
92
        public boolean isEmpty() {
93 20692 jmvivo
                checkModified();
94 20437 vcaballero
                return bitSet.isEmpty();
95
        }
96
97
        public boolean contains(Object o) {
98 20692 jmvivo
                checkModified();
99 21045 jmvivo
                if (o instanceof Feature){
100
                        return bitSet.get((int)((DBFFeatureID)((Feature)o).getID()).getIndex());
101 20437 vcaballero
                }
102
                return false;
103
        }
104
105
        public Iterator iterator() {
106 20692 jmvivo
                checkModified();
107 20437 vcaballero
                DBFBitSetIterator dbfIter=new DBFBitSetIterator(bitSet);
108
                return dbfIter;
109
        }
110
111
        public Object[] toArray() {
112 20692 jmvivo
                checkModified();
113 20437 vcaballero
                ArrayList features= new ArrayList();
114
                Iterator iterator= this.iterator();
115
                while(iterator.hasNext()){
116 21045 jmvivo
                        Feature feature=(Feature)iterator.next();
117 20437 vcaballero
                        features.add(feature);
118
                }
119
                return features.toArray();
120
        }
121
122
        public Object[] toArray(Object[] a) {
123 20692 jmvivo
                checkModified();
124 20437 vcaballero
                ArrayList features= new ArrayList();
125
                Iterator iterator= this.iterator();
126
                while(iterator.hasNext()){
127 21045 jmvivo
                        Feature feature=(Feature)iterator.next();
128 20437 vcaballero
                        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 20692 jmvivo
                checkModified();
143 20437 vcaballero
                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 20467 jmvivo
                private int current=-1;
171 20437 vcaballero
                public DBFBitSetIterator(BitSet bitSet){
172
                        this.bs=bitSet;
173
                }
174
                public boolean hasNext() {
175 20692 jmvivo
                        checkModified();
176 20467 jmvivo
                        return (bs.nextSetBit(current+1) >= 0);
177 20437 vcaballero
                }
178
179 20467 jmvivo
180 20437 vcaballero
                public Object next() {
181 20692 jmvivo
                        checkModified();
182 21045 jmvivo
                        Feature feature=null;
183 20467 jmvivo
                        if (!this.hasNext()){
184
                                throw new NoSuchElementException();
185
                        }
186
187
                        current = bs.nextSetBit(current+1);
188
189 20437 vcaballero
                        try {
190 20600 vcaballero
                                feature = createCurrentFeatureID(current).getFeature(featureType);//new DBFFeature(featureType,store,current);
191 20437 vcaballero
                        } 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
}