Statistics
| Revision:

root / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / driver / dbf / DBFFeatureCollection.java @ 19606

History | View | Annotate | Download (5.66 KB)

1
package org.gvsig.data.datastores.vectorial.driver.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.IObservable;
11
import org.gvsig.data.datastores.vectorial.driver.AbstractDriver;
12
import org.gvsig.data.vectorial.AbstractFeatureCollection;
13
import org.gvsig.data.vectorial.IFeature;
14
import org.gvsig.data.vectorial.IFeatureCollection;
15
import org.gvsig.data.vectorial.IFeatureID;
16
import org.gvsig.data.vectorial.IFeatureStoreNotification;
17
import org.gvsig.data.vectorial.IFeatureType;
18
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
19
import org.gvsig.data.vectorial.filter.FeatureFilterParser;
20
import org.gvsig.data.visitor.IVisitor;
21

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

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

    
42
        }
43

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

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

    
72
        public boolean isEmpty() {
73
                return (size()==0);
74
        }
75

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

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

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

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

    
112
        public boolean add(Object o) {
113
                throw new UnsupportedOperationException();
114
        }
115

    
116
        public boolean remove(Object o) {
117
                throw new UnsupportedOperationException();
118
        }
119

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

    
129
        }
130

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

    
135
        public boolean removeAll(Collection c) {
136
                throw new UnsupportedOperationException();
137
        }
138

    
139
        public boolean retainAll(Collection c) {
140
                throw new UnsupportedOperationException();
141
        }
142

    
143
        public void clear() {
144
                throw new UnsupportedOperationException();
145
        }
146

    
147

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

    
153
                public DBFIterator(){
154
                        position=0;
155
                }
156

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

    
162
                protected IFeatureID createCurrectDriverFeatureID(long pos){
163
                        if (pos<driverFeatureCount){
164
                                return new DBFFeatureID((DBFDriver)driver,pos);
165
                        } else {
166
                                return null;
167
                        }
168
                }
169

    
170
                public boolean hasNext(){
171
                        checkModified();
172

    
173
                        IFeature feature=null;
174
                        if (nextChecked){
175
                                return feature != null;
176
                        }
177
                        nextChecked=true;
178
                        while (true){
179
                                if (position<driverFeatureCount){
180
                                        IFeatureID featureID = this.createCurrectDriverFeatureID(position);
181
                                        feature=featureID.getFeature(featureType);
182
                                }else if (featureManager!=null && (position-driverFeatureCount)<featureManager.getNum()){
183
                                        int pos=(int)(position-driverFeatureCount);
184
                                        feature=featureManager.getFeature(pos);
185
                                }else{
186
                                        this.feature = null;
187
                                        return false;
188
                                }
189

    
190
                                position++;
191

    
192
                                if(featureManager!=null && featureManager.isDeleted(feature))
193
                                        continue;
194

    
195
                                if (filter == null) {
196
                                        this.feature=feature;
197
                                        return true;
198

    
199
                                } else {
200
                                        try {
201
                                                if (parser.match(feature)){
202
                                                        this.feature=feature;
203
                                                        return true;
204
                                                }else{
205
                                                        continue;
206
                                                }
207
                                        } catch (Exception e) {
208
                                                // TODO Auto-generated catch block
209
                                                e.printStackTrace();
210
                                        }
211
                                }
212
                        }
213
                }
214

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

    
226
                public void remove() {
227
                        throw new UnsupportedOperationException();
228
                }
229

    
230
        }
231

    
232
}