Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / IndexFeatureSet.java @ 40559

History | View | Annotate | Download (7.87 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * 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 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * 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
 *
16
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
* MA  02110-1301, USA.
43
*
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2008 {{Company}}   {{Task}}
49
*/
50

    
51

    
52
package org.gvsig.fmap.dal.feature.impl;
53

    
54
import java.util.ArrayList;
55
import java.util.Collections;
56
import java.util.Iterator;
57
import java.util.List;
58

    
59
import org.gvsig.fmap.dal.DataStore;
60
import org.gvsig.fmap.dal.exception.DataException;
61
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
62
import org.gvsig.fmap.dal.feature.EditableFeature;
63
import org.gvsig.fmap.dal.feature.Feature;
64
import org.gvsig.fmap.dal.feature.FeatureReference;
65
import org.gvsig.fmap.dal.feature.FeatureSet;
66
import org.gvsig.fmap.dal.feature.FeatureType;
67
import org.gvsig.fmap.dal.feature.impl.featureset.DynObjectSetFeatureSetFacade;
68
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
69
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
70
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
71
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
72
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
73
import org.gvsig.fmap.dal.feature.spi.LongList;
74
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
75
import org.gvsig.tools.dispose.DisposableIterator;
76
import org.gvsig.tools.dynobject.DynObjectSet;
77
import org.gvsig.tools.exception.BaseException;
78
import org.gvsig.tools.visitor.Visitor;
79

    
80

    
81
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
82

    
83
        LongList featureReferences = null;
84
        FeatureStoreProvider storeProvider = null;
85
        FeatureStoreProviderServices store = null;
86
        FeatureIndexProviderServices index = null;
87
        List featureTypes = null;
88

    
89
        public class IndexIterator implements DisposableIterator {
90
                Iterator it = null;
91

    
92
                public IndexIterator(Iterator it) {
93
                        this.it = it;
94
                }
95

    
96
                public boolean hasNext() {
97
                        return it.hasNext();
98
                }
99

    
100
                public Object next() {
101
                        Object oid = it.next();
102
                        FeatureReference ref = new DefaultFeatureReference(store
103
                                        .getFeatureStore(), oid);
104
                        try {
105
                                return store.getFeatureStore().getFeatureByReference(ref);
106
                        } catch (DataException e) {
107
                                throw new ReadRuntimeException(store.getName(), e);
108
                        }
109
                }
110

    
111
                public void remove() {
112
                        throw new UnsupportedOperationException();
113
                }
114

    
115
                public void dispose() {
116
                        this.it = null;
117
                }
118
        }
119

    
120
        public class FastIndexIterator implements DisposableIterator {
121
                Iterator it = null;
122
                DefaultFeature feature = null;
123

    
124
                public FastIndexIterator(Iterator it) throws DataException {
125
                        this.it = it;
126
                        feature = (DefaultFeature) store.createFeature(storeProvider
127
                                        .createFeatureProvider(index.getFeatureType()));
128
                }
129

    
130
                public boolean hasNext() {
131
                        return it.hasNext();
132
                }
133

    
134
                public Object next() {
135
                        Object oid = it.next();
136
                        try {
137
                                //                                Long longer=new Long(((Integer)oid).longValue());
138
                                FeatureReference ref = new DefaultFeatureReference(store
139
                                                .getFeatureStore(), oid);
140
                                FeatureProvider data = storeProvider
141
                                                .getFeatureProviderByReference((FeatureReferenceProviderServices) ref);
142

    
143
                                feature.setData(data);
144

    
145
                                return feature;
146
                        } catch (DataException e) {
147
                                throw new ReadRuntimeException(store.getName(), e);
148
                        }
149
                }
150

    
151
                public void remove() {
152
                        throw new UnsupportedOperationException();
153
                }
154

    
155
                public void dispose() {
156
                        this.it = null;
157
                        this.feature = null;
158

    
159
                }
160
        }
161

    
162
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
163
                this.featureReferences = featureReferences;
164
                this.store = index.getFeatureStoreProviderServices();
165
                this.storeProvider = store.getProvider();
166
                this.index = index;
167
        }
168

    
169
        public boolean canFilter() {
170
                return false;
171
        }
172

    
173
        public boolean canIterateFromIndex() {
174
                return true;
175
        }
176

    
177
        public boolean canOrder() {
178
                return false;
179
        }
180

    
181
        public DisposableIterator fastIterator(long index) throws DataException {
182
                if (store.getFeatureStore().isEditing()) {
183
                        return this.iterator(index);
184
                }
185
                return new FastIndexIterator(this.featureReferences.iterator(index));
186
        }
187

    
188
        public DisposableIterator fastIterator() throws DataException {
189
                if (store.getFeatureStore().isEditing()) {
190
                        return this.iterator();
191
                }
192
                return new FastIndexIterator(this.featureReferences.iterator());
193
        }
194

    
195
        public long getSize() throws DataException {
196
                return featureReferences.getSize();
197
        }
198

    
199
        public boolean isEmpty() throws DataException {
200
                return featureReferences.isEmpty();
201
        }
202

    
203
        public DisposableIterator iterator() throws DataException {
204
                return new IndexIterator(this.featureReferences.iterator());
205
        }
206

    
207
        public DisposableIterator iterator(long index) throws DataException {
208
                return new IndexIterator(this.featureReferences.iterator(index));
209
        }
210

    
211
        public void delete(Feature feature) throws DataException {
212
                index.delete(feature);
213
                store.getFeatureStore().delete(feature);
214
        }
215

    
216
        public FeatureType getDefaultFeatureType() {
217
                return index.getFeatureType();
218
        }
219

    
220
        public List getFeatureTypes() {
221
                List types = new ArrayList();
222
                types.add(index.getFeatureType());
223
                return Collections.unmodifiableList(types);
224
        }
225

    
226
        public void insert(EditableFeature feature) throws DataException {
227
                index.insert(feature);
228
                store.getFeatureStore().insert(feature);
229
        }
230

    
231
        public void update(EditableFeature feature) throws DataException {
232
                store.getFeatureStore().update(feature);
233
                // we need to re-index the feature, since its shape might have changed
234
                index.delete(feature);
235
                index.insert(feature);
236
        }
237

    
238
        public void dispose() {
239

    
240
        }
241

    
242
        public boolean isFromStore(DataStore store) {
243
                return this.store.equals(store);
244
        }
245

    
246
        public void accept(Visitor visitor) throws BaseException {
247
                accept(visitor, 0);
248
        }
249

    
250
        public void accept(Visitor visitor, long firstValueIndex)
251
                        throws BaseException {
252
                synchronized (store.getFeatureStore()) {
253
                        DisposableIterator iterator = fastIterator(firstValueIndex);
254
                        
255
                        if (iterator != null) {
256
                                try {
257
                                        while (iterator.hasNext()) {
258
                                                Feature feature = (Feature) iterator.next();
259
                                                visitor.visit(feature);
260
                                        }
261
                                } finally {
262
                                        iterator.dispose();
263
                                }
264
                        }
265
                }
266
        }
267

    
268
    public DynObjectSet getDynObjectSet() {
269
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore());
270
    }
271

    
272
    public DynObjectSet getDynObjectSet(boolean fast) {
273
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore(),
274
            fast);
275
    }
276

    
277
}