Statistics
| Revision:

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

History | View | Annotate | Download (5.46 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

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

    
41
        }
42

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

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

    
65
        public boolean isEmpty() {
66
                return (size()==0);
67
        }
68

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

    
80
        public Iterator iterator() {
81
                DBFIterator dbfIter=new DBFIterator();
82
                return dbfIter;
83
        }
84

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

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

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

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

    
113
        public boolean containsAll(Collection c) {
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

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

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

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

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

    
140

    
141
        protected class DBFIterator implements Iterator{
142
                protected long position=0;
143
                private boolean nextChecked=false;
144
                private IFeature feature;
145

    
146
                public DBFIterator(){
147
                        position=0;
148
                }
149

    
150
                protected void checkModified(){
151
                        if (modified)
152
                                throw new ConcurrentModificationException("FeatureCollection modified");
153
                }
154

    
155
                protected IFeatureID createCurrectDriverFeatureID(long pos){
156
                        if (pos<driverFeatureCount){
157
                                return new DBFFeatureID((DBFDriver)driver,pos);
158
                        } else {
159
                                return null;
160
                        }
161
                }
162

    
163
                public boolean hasNext(){
164
                        checkModified();
165

    
166
                        IFeature feature=null;
167
                        if (nextChecked){
168
                                return feature != null;
169
                        }
170
                        nextChecked=true;
171
                        while (true){
172
                                if (position<driverFeatureCount){
173
                                        IFeatureID featureID = this.createCurrectDriverFeatureID(position);
174
                                        feature=featureID.getFeature(featureType);
175
                                }else if ((position-driverFeatureCount)<featureManager.getNum()){
176
                                        int pos=(int)(position-driverFeatureCount);
177
                                        feature=featureManager.getFeature(pos);
178
                                }else{
179
                                        this.feature = null;
180
                                        return false;
181
                                }
182

    
183
                                position++;
184

    
185
                                if(featureManager.isDeleted(feature))
186
                                        continue;
187

    
188
                                if (filter == null) {
189
                                        this.feature=feature;
190
                                        return true;
191

    
192
                                } else {
193
                                        try {
194
                                                if (parser.match(feature)){
195
                                                        this.feature=feature;
196
                                                        return true;
197
                                                }else{
198
                                                        continue;
199
                                                }
200
                                        } catch (Exception e) {
201
                                                // TODO Auto-generated catch block
202
                                                e.printStackTrace();
203
                                        }
204
                                }
205
                        }
206
                }
207

    
208
                public Object next() {
209
                        checkModified();
210
                        if (!nextChecked){
211
                                hasNext();
212
                        }
213
                        if (feature == null)
214
                                throw new NoSuchElementException();
215
                        nextChecked=false;
216
                        return feature;
217
                }
218

    
219
                public void remove() {
220
                        throw new UnsupportedOperationException();
221
                }
222

    
223
        }
224

    
225
}