Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeatureCollection.java @ 20421

History | View | Annotate | Download (5.93 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.ConcurrentModificationException;
6
import java.util.Iterator;
7
import java.util.NoSuchElementException;
8

    
9
import org.gvsig.data.ComplexObservable;
10
import org.gvsig.data.exception.ReadException;
11
import org.gvsig.data.vectorial.AbstractFeatureCollection;
12
import org.gvsig.data.vectorial.IFeature;
13
import org.gvsig.data.vectorial.IFeatureID;
14
import org.gvsig.data.vectorial.IFeatureStore;
15
import org.gvsig.data.vectorial.IFeatureType;
16
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
17
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
18
import org.gvsig.data.vectorial.filter.FeatureFilterParser;
19

    
20
public class DBFFeatureCollection extends AbstractFeatureCollection {
21
        protected ComplexObservable observable = new ComplexObservable();
22
        protected ArrayList featureIDs=new ArrayList();//<IFeatureID>
23
        protected IFeatureType featureType;
24
        protected String filter;
25
        protected IFeatureStore store;
26
        private FeatureFilterParser parser = null;
27
        protected FeatureManager featureManager;
28
        protected long driverFeatureCount=0;
29
        protected long size=-1;
30

    
31
        public DBFFeatureCollection(FeatureManager fm,DBFStore store,IFeatureType type, String filter) {
32
                this.featureManager=fm;
33
                this.store=store;
34
                driverFeatureCount=store.getFeatureCount();
35
                this.featureType=type;
36
                this.filter=filter;
37
                if (this.filter!=null)
38
                        parser = new FeatureFilterParser(filter,this.featureType);
39

    
40
        }
41

    
42
        protected void checkModified(){
43
                if (modified)
44
                        throw new ConcurrentModificationException("FeatureCollection modified");
45
        }
46

    
47
        public int size() {
48
                if (this.size < 0){
49
                        if (filter==null){
50
                                this.size = driverFeatureCount;
51
                                if (featureManager!=null){
52
                                        this.size += featureManager.getNum();
53
                                }
54
                        } else {
55
                                Iterator iterator= this.iterator();
56
                                this.size=0;
57
                                try {
58
                                        while(true){
59
                                                iterator.next();
60
                                                size++;
61
                                        }
62
                                } catch (NoSuchElementException e) {
63
                                        //End
64
                                }
65
                        }
66
                }
67
                return (int)this.size;
68
        }
69

    
70
        public boolean isEmpty() {
71
                return (size()==0);
72
        }
73

    
74
        public boolean contains(Object o) {
75
                Iterator iterator= this.iterator();
76
                while(iterator.hasNext()){
77
                        IFeature feature=(IFeature)iterator.next();
78
                        if (feature.getID().equals(((IFeature)o).getID())){
79
                                return true;
80
                        }
81
                }
82
                return false;
83
        }
84

    
85
        public Iterator iterator() {
86
                DBFIterator dbfIter=new DBFIterator();
87
                return dbfIter;
88
        }
89

    
90
        public Object[] toArray() {
91
                ArrayList features= new ArrayList();
92
                Iterator iterator= this.iterator();
93
                while(iterator.hasNext()){
94
                        IFeature feature=(IFeature)iterator.next();
95
                        features.add(feature);
96
                }
97
                return features.toArray();
98
        }
99

    
100
        public Object[] toArray(Object[] a) {
101
                ArrayList features= new ArrayList();
102
                Iterator iterator= this.iterator();
103
                while(iterator.hasNext()){
104
                        IFeature feature=(IFeature)iterator.next();
105
                        features.add(feature);
106
                }
107
                return features.toArray(a);
108
        }
109

    
110
        public boolean add(Object o) {
111
                throw new UnsupportedOperationException();
112
        }
113

    
114
        public boolean remove(Object o) {
115
                throw new UnsupportedOperationException();
116
        }
117

    
118
        public boolean containsAll(Collection c) {
119
                Iterator iter = c.iterator();
120
                while (iter.hasNext()){
121
                        if (!this.contains(iter.next())){
122
                                return false;
123
                        }
124
                }
125
                return true;
126

    
127
        }
128

    
129
        public boolean addAll(Collection c) {
130
                throw new UnsupportedOperationException();
131
        }
132

    
133
        public boolean removeAll(Collection c) {
134
                throw new UnsupportedOperationException();
135
        }
136

    
137
        public boolean retainAll(Collection c) {
138
                throw new UnsupportedOperationException();
139
        }
140

    
141
        public void clear() {
142
                throw new UnsupportedOperationException();
143
        }
144

    
145

    
146
        protected class DBFIterator implements Iterator{
147
                protected long position=0;
148
                private boolean nextChecked=false;
149
                private IFeature feature;
150

    
151
                public DBFIterator(){
152
                        position=0;
153
                }
154

    
155
                protected void checkModified(){
156
                        if (modified)
157
                                throw new ConcurrentModificationException("FeatureCollection modified");
158
                }
159

    
160
                protected IFeatureID createCurrentFeatureID(long pos){
161
                        if (pos<driverFeatureCount){
162
                                return new DBFFeatureID((DBFStore)store,pos);
163
                        } else {
164
                                return null;
165
                        }
166
                }
167

    
168
                public boolean hasNext(){
169
                        checkModified();
170

    
171
                        IFeature feature=null;
172
                        if (nextChecked){
173
                                return this.feature != null;
174
                        }
175
                        nextChecked=true;
176
                        while (true){
177
                                if (position<driverFeatureCount){
178
                                        IFeatureID featureID = this.createCurrentFeatureID(position);
179
                                        try {
180
                                                feature=featureID.getFeature(featureType);
181
                                        } catch (ReadException e) {
182
                                                throw new RuntimeException(e);
183
                                        }
184
                                }else if (featureManager!=null && (position-driverFeatureCount)<featureManager.getNum()){
185
                                        int pos=(int)(position-driverFeatureCount);
186
                                        feature=featureManager.getFeature(pos);
187
                                }else{
188
                                        this.feature = null;
189
                                        return false;
190
                                }
191

    
192
                                position++;
193

    
194
                                if(featureManager!=null && featureManager.isDeleted(feature))
195
                                        continue;
196

    
197
                                if (filter == null) {
198
                                        this.feature=feature;
199
                                        return true;
200

    
201
                                } else {
202
                                        try {
203
                                                if (parser.match(feature)){
204
                                                        this.feature=feature;
205
                                                        return true;
206
                                                }else{
207
                                                        continue;
208
                                                }
209
                                        } catch (Exception e) {
210
                                                throw new RuntimeException(e);
211
                                        }
212
                                }
213
                        }
214
                }
215

    
216
                public Object next() {
217
                        checkModified();
218
                        if (!nextChecked){
219
                                hasNext();
220
                        }
221
                        if (this.feature == null)
222
                                throw new NoSuchElementException();
223
                        nextChecked=false;
224
                        IFeature feature = this.feature;
225
                        this.feature = null;
226
                        return feature;
227
                }
228

    
229
                public void remove() {
230
                        throw new UnsupportedOperationException();
231
                }
232

    
233
        }
234

    
235

    
236
        public void dispose() {
237
                this.observable.deleteObservers();
238
                this.observable = null;
239
                this.featureIDs.clear();
240
                this.featureIDs = null;
241
                this.featureType = null;
242
                this.store = null;
243
                this.parser = null;
244
                this.featureManager = null;
245
                ;
246
        }
247

    
248
}