Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dbf / DBFFeatureCollectionBitSet.java @ 23677

History | View | Annotate | Download (5.09 KB)

1
package org.gvsig.fmap.data.feature.file.dbf;
2

    
3
import java.util.*;
4

    
5
import org.gvsig.fmap.data.DataManager;
6
import org.gvsig.fmap.data.DataStore;
7
import org.gvsig.fmap.data.ReadException;
8
import org.gvsig.fmap.data.feature.AbstractFeatureCollection;
9
import org.gvsig.fmap.data.feature.Feature;
10
import org.gvsig.fmap.data.feature.FeatureType;
11
import org.gvsig.fmap.data.feature.expressionevaluator.Filter;
12
import org.gvsig.tools.observer.DefaultObservable;
13

    
14

    
15
//FIXME: OJO Excepciones
16
public class DBFFeatureCollectionBitSet extends AbstractFeatureCollection {
17
        protected BitSet bitSet=new BitSet();
18
        protected DefaultObservable observable = new DefaultObservable();
19
        protected FeatureType featureType;
20
        protected DBFStore store;
21
        protected String filter;
22
        protected Filter parser;
23
        protected long driverFeatureCount;
24

    
25
        protected DBFFeatureCollectionBitSet(DBFStore store,FeatureType type, String filter) throws ReadException {
26
                DataManager manager = DataManager.getManager();
27
                this.featureType=type;
28
                this.filter=filter;
29
                if (this.filter!=null) {
30
                        parser = manager.getExpressionParser().parseFilter(filter);
31
                }
32
                driverFeatureCount=store.getFeatureCount();
33
                this.store=store;
34
                intilizeBitSet();
35

    
36
        }
37

    
38
        protected void intilizeBitSet() throws ReadException {
39
                try{
40
                        bitSet.clear();
41
                        if (filter==null){
42
                                for (int i = 0; i < driverFeatureCount; i++) {
43
                                        bitSet.set(i);
44
                                }
45
                        }else{
46
                                for (int i = 0; i < driverFeatureCount; i++) {
47
                                        Feature feature = getFeature(i);
48
                                        if (parser.evaluate(feature)) {
49
                                                bitSet.set(i);
50
                                        }
51
                                }
52
                        }
53
                }catch (Exception e) {
54
                        throw new ReadException("DBFFeatureCollectionBitSet",e);
55
                }
56
        }
57

    
58
        public int size() {
59
                checkModified();
60
                return bitSet.cardinality();
61
        }
62

    
63
        public boolean isEmpty() {
64
                checkModified();
65
                return bitSet.isEmpty();
66
        }
67

    
68
        public boolean contains(Object o) {
69
                checkModified();
70
                if (o instanceof Feature){
71
                        return bitSet.get((int)((DBFFeatureID)((Feature)o).getID()).getIndex());
72
                }
73
                return false;
74
        }
75
        
76
        protected Iterator internalIterator(int index) {
77
        checkModified();
78
        DBFBitSetIterator dbfIter = new DBFBitSetIterator(bitSet, index);
79
        return dbfIter;
80
    }
81

    
82
        public Object[] toArray() {
83
                checkModified();
84
                ArrayList features= new ArrayList();
85
                Iterator iterator= this.iterator();
86
                while(iterator.hasNext()){
87
                        Feature feature=(Feature)iterator.next();
88
                        features.add(feature);
89
                }
90
                return features.toArray();
91
        }
92

    
93
        public Object[] toArray(Object[] a) {
94
                checkModified();
95
                ArrayList features= new ArrayList();
96
                Iterator iterator= this.iterator();
97
                while(iterator.hasNext()){
98
                        Feature feature=(Feature)iterator.next();
99
                        features.add(feature);
100
                }
101
                return features.toArray(a);
102
        }
103

    
104
        public boolean add(Object o) {
105
                throw new UnsupportedOperationException();
106
        }
107

    
108
        public boolean remove(Object o) {
109
                throw new UnsupportedOperationException();
110
        }
111

    
112
        public boolean containsAll(Collection c) {
113
                checkModified();
114
                Iterator iter = c.iterator();
115
                while (iter.hasNext()){
116
                        if (!this.contains(iter.next())){
117
                                return false;
118
                        }
119
                }
120
                return true;
121
        }
122

    
123
        public boolean addAll(Collection c) {
124
                throw new UnsupportedOperationException();
125
        }
126

    
127
        public boolean removeAll(Collection c) {
128
                throw new UnsupportedOperationException();
129
        }
130

    
131
        public boolean retainAll(Collection c) {
132
                throw new UnsupportedOperationException();
133
        }
134

    
135
        public void clear() {
136
                throw new UnsupportedOperationException();
137
        }
138

    
139
        private class DBFBitSetIterator implements Iterator{
140
                private BitSet bs;
141
                private int current=-1;
142
                
143
                public DBFBitSetIterator(BitSet bitSet){
144
                        this(bitSet, 0);
145
                }
146

    
147
        /**
148
         * Creates a new DBFBitSetIterator starting at the given position.
149
         * 
150
         * @param bitSet
151
         *            to iterate on
152
         * @param position
153
         *            to start the iteration
154
         */
155
        public DBFBitSetIterator(BitSet bitSet, int position) {
156
            this.bs = bitSet;
157
            this.current = position - 1;
158
        }
159

    
160
                public boolean hasNext() {
161
                        checkModified();
162
                        return (bs.nextSetBit(current+1) >= 0);
163
                }
164

    
165

    
166
                public Object next() {
167
                        checkModified();
168
                        Feature feature=null;
169
                        if (!this.hasNext()){
170
                                throw new NoSuchElementException();
171
                        }
172

    
173
                        current = bs.nextSetBit(current+1);
174

    
175
                        try {
176
                                feature = getFeature(current);
177
                        } catch (ReadException e) {
178
                                throw new RuntimeException(e);
179
                        }
180
                        return feature;
181
                }
182

    
183
                public void remove() {
184
                        throw new UnsupportedOperationException();
185
                }
186
        }
187

    
188
        public void dispose() {
189
                observable.deleteObservers();
190
                this.observable = null;
191
                this.bitSet.clear();
192
                this.featureType = null;
193

    
194
        }
195

    
196
        public Feature getFeature(int index) throws ReadException {
197
                Feature feature=null;
198
                if (index<driverFeatureCount){
199
                        return store.getFeatureByIndex(index, featureType);
200
                }else{
201
                        feature = null;
202
                }
203
                return feature;
204
        }
205

    
206
        public boolean isFromStore(DataStore store) {
207
                return this.store.equals(store);
208
        }
209

    
210
        public FeatureType getFeatureType() {
211
                return this.featureType;
212
        }
213
}