Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / IndexFeatureSet.java @ 29326

History | View | Annotate | Download (6.27 KB)

1 23950 jiyarza
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5 24017 jjdelcerro
*
6 23950 jiyarza
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10 24017 jjdelcerro
*
11 23950 jiyarza
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15 24017 jjdelcerro
*
16 23950 jiyarza
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18 24017 jjdelcerro
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 23950 jiyarza
* MA  02110-1301, USA.
20 24017 jjdelcerro
*
21 23950 jiyarza
*/
22
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27
28 24017 jjdelcerro
29 24496 jmvivo
package org.gvsig.fmap.dal.feature.impl;
30 23950 jiyarza
31 25716 jiyarza
import java.util.ArrayList;
32
import java.util.Collections;
33 23950 jiyarza
import java.util.Iterator;
34 25716 jiyarza
import java.util.List;
35 23950 jiyarza
36 25716 jiyarza
import org.gvsig.fmap.dal.DataStore;
37 24505 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
39 27525 jmvivo
import org.gvsig.fmap.dal.feature.DisposableIterator;
40 25716 jiyarza
import org.gvsig.fmap.dal.feature.EditableFeature;
41 24496 jmvivo
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureReference;
43 25716 jiyarza
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45 29289 jmvivo
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
46 24496 jmvivo
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
47
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
48
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
49 25917 jmvivo
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
50 24496 jmvivo
import org.gvsig.fmap.dal.feature.spi.LongList;
51
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
52 25716 jiyarza
import org.gvsig.tools.exception.BaseException;
53
import org.gvsig.tools.visitor.Visitor;
54 23950 jiyarza
55
56 25716 jiyarza
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
57 23950 jiyarza
58
        LongList featureReferences = null;
59 25917 jmvivo
        FeatureStoreProvider storeProvider = null;
60
        FeatureStoreProviderServices store = null;
61 23950 jiyarza
        FeatureIndexProviderServices index = null;
62 25716 jiyarza
        List featureTypes = null;
63 24017 jjdelcerro
64 27525 jmvivo
        public class IndexIterator implements DisposableIterator {
65 23950 jiyarza
                Iterator it = null;
66 24017 jjdelcerro
67 23950 jiyarza
                public IndexIterator(Iterator it) {
68 24017 jjdelcerro
                        this.it = it;
69 23950 jiyarza
                }
70
71
                public boolean hasNext() {
72
                        return it.hasNext();
73
                }
74
75
                public Object next() {
76 24248 jjdelcerro
                        Object oid = it.next();
77 29326 jmvivo
                        FeatureReference ref = new DefaultFeatureReference(store
78
                                        .getFeatureStore(), oid);
79 23950 jiyarza
                        try {
80 29326 jmvivo
                                return store.getFeatureStore().getFeatureByReference(ref);
81 23950 jiyarza
                        } catch (DataException e) {
82 25917 jmvivo
                                throw new ReadRuntimeException(store.getName(), e);
83 23950 jiyarza
                        }
84
                }
85
86
                public void remove() {
87
                        throw new UnsupportedOperationException();
88
                }
89 27525 jmvivo
90
                public void dispose() {
91
                        this.it = null;
92
                }
93 23950 jiyarza
        }
94 24017 jjdelcerro
95 27525 jmvivo
        public class FastIndexIterator implements DisposableIterator {
96 23950 jiyarza
                Iterator it = null;
97 25917 jmvivo
                DefaultFeature feature = null;
98 24017 jjdelcerro
99 23950 jiyarza
                public FastIndexIterator(Iterator it) throws DataException {
100 24017 jjdelcerro
                        this.it = it;
101 25917 jmvivo
                        feature = (DefaultFeature) store.createFeature(storeProvider
102 29292 jmvivo
                                        .createFeatureProvider(index.getFeatureType()));
103 23950 jiyarza
                }
104
105
                public boolean hasNext() {
106
                        return it.hasNext();
107
                }
108
109
                public Object next() {
110 24248 jjdelcerro
                        Object oid = it.next();
111 23950 jiyarza
                        try {
112 25917 jmvivo
                                //                                Long longer=new Long(((Integer)oid).longValue());
113 29326 jmvivo
                                FeatureReference ref = new DefaultFeatureReference(store
114
                                                .getFeatureStore(), oid);
115 29289 jmvivo
                                FeatureProvider data = storeProvider
116 29292 jmvivo
                                                .getFeatureProviderByReference((FeatureReferenceProviderServices) ref);
117 25917 jmvivo
118
                                feature.setData(data);
119
120
                                return feature;
121 23950 jiyarza
                        } catch (DataException e) {
122 25917 jmvivo
                                throw new ReadRuntimeException(store.getName(), e);
123 23950 jiyarza
                        }
124
                }
125
126
                public void remove() {
127
                        throw new UnsupportedOperationException();
128
                }
129 27525 jmvivo
130
                public void dispose() {
131
                        this.it = null;
132
                        this.feature = null;
133
134
                }
135 24017 jjdelcerro
        }
136
137 23950 jiyarza
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
138
                this.featureReferences = featureReferences;
139 29326 jmvivo
                this.store = index.getFeatureStoreProviderServices();
140 25917 jmvivo
                this.storeProvider = store.getProvider();
141 23950 jiyarza
                this.index = index;
142
        }
143 24017 jjdelcerro
144 23950 jiyarza
        public boolean canFilter() {
145
                return false;
146
        }
147
148
        public boolean canIterateFromIndex() {
149
                return true;
150
        }
151
152
        public boolean canOrder() {
153
                return false;
154
        }
155
156 27525 jmvivo
        public DisposableIterator fastIterator(long index) throws DataException {
157 29326 jmvivo
                if (store.getFeatureStore().isEditing()) {
158 25917 jmvivo
                        return this.iterator(index);
159
                }
160 25721 jiyarza
                return new FastIndexIterator(this.featureReferences.iterator(index));
161 23950 jiyarza
        }
162
163 27525 jmvivo
        public DisposableIterator fastIterator() throws DataException {
164 29326 jmvivo
                if (store.getFeatureStore().isEditing()) {
165 25917 jmvivo
                        return this.iterator();
166
                }
167 23950 jiyarza
                return new FastIndexIterator(this.featureReferences.iterator());
168
        }
169
170
        public long getSize() throws DataException {
171
                return featureReferences.getSize();
172
        }
173
174 24017 jjdelcerro
        public boolean isEmpty() throws DataException {
175 23950 jiyarza
                return featureReferences.isEmpty();
176
        }
177
178 27525 jmvivo
        public DisposableIterator iterator() throws DataException {
179 23950 jiyarza
                return new IndexIterator(this.featureReferences.iterator());
180
        }
181
182 27525 jmvivo
        public DisposableIterator iterator(long index) throws DataException {
183 25917 jmvivo
                return new IndexIterator(this.featureReferences.iterator(index));
184 23950 jiyarza
        }
185
186 25716 jiyarza
        public void delete(Feature feature) throws DataException {
187
                index.delete(feature);
188 29326 jmvivo
                store.getFeatureStore().delete(feature);
189 25716 jiyarza
        }
190
191
        public FeatureType getDefaultFeatureType() {
192
                return index.getFeatureType();
193
        }
194
195
        public List getFeatureTypes() {
196
                List types = new ArrayList();
197
                types.add(index.getFeatureType());
198
                return Collections.unmodifiableList(types);
199
        }
200
201
        public void insert(EditableFeature feature) throws DataException {
202
                index.insert(feature);
203 29326 jmvivo
                store.getFeatureStore().insert(feature);
204 25716 jiyarza
        }
205
206
        public void update(EditableFeature feature) throws DataException {
207 29326 jmvivo
                store.getFeatureStore().update(feature);
208 25716 jiyarza
                // we need to re-index the feature, since its shape might have changed
209
                index.delete(feature);
210
                index.insert(feature);
211
        }
212
213
        public void dispose() {
214 25917 jmvivo
215 25716 jiyarza
        }
216
217
        public boolean isFromStore(DataStore store) {
218
                return this.store.equals(store);
219
        }
220
221
        public void accept(Visitor visitor) throws BaseException {
222 27525 jmvivo
                DisposableIterator iterator = iterator();
223 25716 jiyarza
224
                while (iterator.hasNext()) {
225
                        Feature feature = (Feature) iterator.next();
226
                        visitor.visit(feature);
227
                }
228 27525 jmvivo
                iterator.dispose();
229 25716 jiyarza
        }
230
231 23950 jiyarza
}