Statistics
| Revision:

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

History | View | Annotate | Download (6.87 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 25716 jiyarza
import org.gvsig.fmap.dal.feature.EditableFeature;
40 24496 jmvivo
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureReference;
42 25716 jiyarza
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44 33657 cordinyana
import org.gvsig.fmap.dal.feature.impl.featureset.DynObjectSetFeatureSetFacade;
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 33205 cordinyana
import org.gvsig.tools.dispose.DisposableIterator;
53 33657 cordinyana
import org.gvsig.tools.dynobject.DynObjectSet;
54 25716 jiyarza
import org.gvsig.tools.exception.BaseException;
55
import org.gvsig.tools.visitor.Visitor;
56 23950 jiyarza
57
58 25716 jiyarza
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
59 23950 jiyarza
60
        LongList featureReferences = null;
61 25917 jmvivo
        FeatureStoreProvider storeProvider = null;
62
        FeatureStoreProviderServices store = null;
63 23950 jiyarza
        FeatureIndexProviderServices index = null;
64 25716 jiyarza
        List featureTypes = null;
65 24017 jjdelcerro
66 27525 jmvivo
        public class IndexIterator implements DisposableIterator {
67 23950 jiyarza
                Iterator it = null;
68 24017 jjdelcerro
69 23950 jiyarza
                public IndexIterator(Iterator it) {
70 24017 jjdelcerro
                        this.it = it;
71 23950 jiyarza
                }
72
73
                public boolean hasNext() {
74
                        return it.hasNext();
75
                }
76
77
                public Object next() {
78 24248 jjdelcerro
                        Object oid = it.next();
79 29326 jmvivo
                        FeatureReference ref = new DefaultFeatureReference(store
80
                                        .getFeatureStore(), oid);
81 23950 jiyarza
                        try {
82 29326 jmvivo
                                return store.getFeatureStore().getFeatureByReference(ref);
83 23950 jiyarza
                        } catch (DataException e) {
84 25917 jmvivo
                                throw new ReadRuntimeException(store.getName(), e);
85 23950 jiyarza
                        }
86
                }
87
88
                public void remove() {
89
                        throw new UnsupportedOperationException();
90
                }
91 27525 jmvivo
92
                public void dispose() {
93
                        this.it = null;
94
                }
95 23950 jiyarza
        }
96 24017 jjdelcerro
97 27525 jmvivo
        public class FastIndexIterator implements DisposableIterator {
98 23950 jiyarza
                Iterator it = null;
99 25917 jmvivo
                DefaultFeature feature = null;
100 24017 jjdelcerro
101 23950 jiyarza
                public FastIndexIterator(Iterator it) throws DataException {
102 24017 jjdelcerro
                        this.it = it;
103 25917 jmvivo
                        feature = (DefaultFeature) store.createFeature(storeProvider
104 29292 jmvivo
                                        .createFeatureProvider(index.getFeatureType()));
105 23950 jiyarza
                }
106
107
                public boolean hasNext() {
108
                        return it.hasNext();
109
                }
110
111
                public Object next() {
112 24248 jjdelcerro
                        Object oid = it.next();
113 23950 jiyarza
                        try {
114 25917 jmvivo
                                //                                Long longer=new Long(((Integer)oid).longValue());
115 29326 jmvivo
                                FeatureReference ref = new DefaultFeatureReference(store
116
                                                .getFeatureStore(), oid);
117 29289 jmvivo
                                FeatureProvider data = storeProvider
118 29292 jmvivo
                                                .getFeatureProviderByReference((FeatureReferenceProviderServices) ref);
119 25917 jmvivo
120
                                feature.setData(data);
121
122
                                return feature;
123 23950 jiyarza
                        } catch (DataException e) {
124 25917 jmvivo
                                throw new ReadRuntimeException(store.getName(), e);
125 23950 jiyarza
                        }
126
                }
127
128
                public void remove() {
129
                        throw new UnsupportedOperationException();
130
                }
131 27525 jmvivo
132
                public void dispose() {
133
                        this.it = null;
134
                        this.feature = null;
135
136
                }
137 24017 jjdelcerro
        }
138
139 23950 jiyarza
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
140
                this.featureReferences = featureReferences;
141 29326 jmvivo
                this.store = index.getFeatureStoreProviderServices();
142 25917 jmvivo
                this.storeProvider = store.getProvider();
143 23950 jiyarza
                this.index = index;
144
        }
145 24017 jjdelcerro
146 23950 jiyarza
        public boolean canFilter() {
147
                return false;
148
        }
149
150
        public boolean canIterateFromIndex() {
151
                return true;
152
        }
153
154
        public boolean canOrder() {
155
                return false;
156
        }
157
158 27525 jmvivo
        public DisposableIterator fastIterator(long index) throws DataException {
159 29326 jmvivo
                if (store.getFeatureStore().isEditing()) {
160 25917 jmvivo
                        return this.iterator(index);
161
                }
162 25721 jiyarza
                return new FastIndexIterator(this.featureReferences.iterator(index));
163 23950 jiyarza
        }
164
165 27525 jmvivo
        public DisposableIterator fastIterator() throws DataException {
166 29326 jmvivo
                if (store.getFeatureStore().isEditing()) {
167 25917 jmvivo
                        return this.iterator();
168
                }
169 23950 jiyarza
                return new FastIndexIterator(this.featureReferences.iterator());
170
        }
171
172
        public long getSize() throws DataException {
173
                return featureReferences.getSize();
174
        }
175
176 24017 jjdelcerro
        public boolean isEmpty() throws DataException {
177 23950 jiyarza
                return featureReferences.isEmpty();
178
        }
179
180 27525 jmvivo
        public DisposableIterator iterator() throws DataException {
181 23950 jiyarza
                return new IndexIterator(this.featureReferences.iterator());
182
        }
183
184 27525 jmvivo
        public DisposableIterator iterator(long index) throws DataException {
185 25917 jmvivo
                return new IndexIterator(this.featureReferences.iterator(index));
186 23950 jiyarza
        }
187
188 25716 jiyarza
        public void delete(Feature feature) throws DataException {
189
                index.delete(feature);
190 29326 jmvivo
                store.getFeatureStore().delete(feature);
191 25716 jiyarza
        }
192
193
        public FeatureType getDefaultFeatureType() {
194
                return index.getFeatureType();
195
        }
196
197
        public List getFeatureTypes() {
198
                List types = new ArrayList();
199
                types.add(index.getFeatureType());
200
                return Collections.unmodifiableList(types);
201
        }
202
203
        public void insert(EditableFeature feature) throws DataException {
204
                index.insert(feature);
205 29326 jmvivo
                store.getFeatureStore().insert(feature);
206 25716 jiyarza
        }
207
208
        public void update(EditableFeature feature) throws DataException {
209 29326 jmvivo
                store.getFeatureStore().update(feature);
210 25716 jiyarza
                // we need to re-index the feature, since its shape might have changed
211
                index.delete(feature);
212
                index.insert(feature);
213
        }
214
215
        public void dispose() {
216 25917 jmvivo
217 25716 jiyarza
        }
218
219
        public boolean isFromStore(DataStore store) {
220
                return this.store.equals(store);
221
        }
222
223
        public void accept(Visitor visitor) throws BaseException {
224 31110 cordinyana
                accept(visitor, 0);
225
        }
226 25716 jiyarza
227 31110 cordinyana
        public void accept(Visitor visitor, long firstValueIndex)
228
                        throws BaseException {
229
                DisposableIterator iterator = fastIterator(firstValueIndex);
230
231 31074 cordinyana
                if (iterator != null) {
232
                        try {
233
                                while (iterator.hasNext()) {
234
                                        Feature feature = (Feature) iterator.next();
235
                                        visitor.visit(feature);
236
                                }
237
                        } finally {
238
                                iterator.dispose();
239
                        }
240 25716 jiyarza
                }
241
        }
242
243 33657 cordinyana
    public DynObjectSet getDynObjectSet() {
244
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore());
245
    }
246
247
    public DynObjectSet getDynObjectSet(boolean fast) {
248
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore(),
249
            fast);
250
    }
251
252 31110 cordinyana
}