Statistics
| Revision:

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

History | View | Annotate | Download (5.65 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.datastores.vectorial.driver.AbstractDriver;
11
import org.gvsig.data.exception.ReadException;
12
import org.gvsig.data.vectorial.AbstractFeatureCollection;
13
import org.gvsig.data.vectorial.IFeature;
14
import org.gvsig.data.vectorial.IFeatureID;
15
import org.gvsig.data.vectorial.IFeatureType;
16
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
17
import org.gvsig.data.vectorial.filter.FeatureFilterParser;
18

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

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

    
39
        }
40

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

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

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

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

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

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

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

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

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

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

    
126
        }
127

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

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

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

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

    
144

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

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

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

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

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

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

    
191
                                position++;
192

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

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

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

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

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

    
232
        }
233

    
234
}