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 @ 42488

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

    
25

    
26
package org.gvsig.fmap.dal.feature.impl;
27

    
28
import java.util.ArrayList;
29
import java.util.Collections;
30
import java.util.Iterator;
31
import java.util.List;
32

    
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
36
import org.gvsig.fmap.dal.feature.EditableFeature;
37
import org.gvsig.fmap.dal.feature.Feature;
38
import org.gvsig.fmap.dal.feature.FeatureReference;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureType;
41
import org.gvsig.fmap.dal.feature.impl.featureset.DynObjectSetFeatureSetFacade;
42
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
43
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
44
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
45
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
46
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
47
import org.gvsig.fmap.dal.feature.spi.LongList;
48
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
49
import org.gvsig.tools.dispose.DisposableIterator;
50
import org.gvsig.tools.dynobject.DynObjectSet;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.visitor.Visitor;
53

    
54

    
55
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider, Iterable<Feature> {
56

    
57
        LongList featureReferences = null;
58
        FeatureStoreProvider storeProvider = null;
59
        FeatureStoreProviderServices store = null;
60
        FeatureIndexProviderServices index = null;
61
        List featureTypes = null;
62

    
63
        public class IndexIterator implements DisposableIterator {
64
                Iterator it = null;
65

    
66
                public IndexIterator(Iterator it) {
67
                        this.it = it;
68
                }
69

    
70
                public boolean hasNext() {
71
                        return it.hasNext();
72
                }
73

    
74
                public Object next() {
75
                        Object oid = it.next();
76
                        FeatureReference ref = new DefaultFeatureReference(store
77
                                        .getFeatureStore(), oid);
78
                        try {
79
                                return store.getFeatureStore().getFeatureByReference(ref);
80
                        } catch (DataException e) {
81
                                throw new ReadRuntimeException(store.getName(), e);
82
                        }
83
                }
84

    
85
                public void remove() {
86
                        throw new UnsupportedOperationException();
87
                }
88

    
89
                public void dispose() {
90
                        this.it = null;
91
                }
92
        }
93

    
94
        public class FastIndexIterator implements DisposableIterator {
95
                Iterator it = null;
96
                DefaultFeature feature = null;
97

    
98
                public FastIndexIterator(Iterator it) throws DataException {
99
                        this.it = it;
100
                        feature = (DefaultFeature) store.createFeature(storeProvider
101
                                        .createFeatureProvider(index.getFeatureType()));
102
                }
103

    
104
                public boolean hasNext() {
105
                        return it.hasNext();
106
                }
107

    
108
                public Object next() {
109
                        Object oid = it.next();
110
                        try {
111
                                //                                Long longer=new Long(((Integer)oid).longValue());
112
                                FeatureReference ref = new DefaultFeatureReference(store
113
                                                .getFeatureStore(), oid);
114
                                FeatureProvider data = storeProvider
115
                                                .getFeatureProviderByReference((FeatureReferenceProviderServices) ref);
116

    
117
                                feature.setData(data);
118

    
119
                                return feature;
120
                        } catch (DataException e) {
121
                                throw new ReadRuntimeException(store.getName(), e);
122
                        }
123
                }
124

    
125
                public void remove() {
126
                        throw new UnsupportedOperationException();
127
                }
128

    
129
                public void dispose() {
130
                        this.it = null;
131
                        this.feature = null;
132

    
133
                }
134
        }
135

    
136
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
137
                this.featureReferences = featureReferences;
138
                this.store = index.getFeatureStoreProviderServices();
139
                this.storeProvider = store.getProvider();
140
                this.index = index;
141
        }
142

    
143
        public boolean canFilter() {
144
                return false;
145
        }
146

    
147
        public boolean canIterateFromIndex() {
148
                return true;
149
        }
150

    
151
        public boolean canOrder() {
152
                return false;
153
        }
154

    
155
        public DisposableIterator fastIterator(long index) throws DataException {
156
                if (store.getFeatureStore().isEditing()) {
157
                        return this.iterator(index);
158
                }
159
                return new FastIndexIterator(this.featureReferences.iterator(index));
160
        }
161

    
162
        public DisposableIterator fastIterator() throws DataException {
163
                if (store.getFeatureStore().isEditing()) {
164
                        return this.iterator();
165
                }
166
                return new FastIndexIterator(this.featureReferences.iterator());
167
        }
168

    
169
        public long getSize() throws DataException {
170
                return featureReferences.getSize();
171
        }
172

    
173
        public boolean isEmpty() throws DataException {
174
                return featureReferences.isEmpty();
175
        }
176

    
177
        public DisposableIterator iterator() {
178
                return new IndexIterator(this.featureReferences.iterator());
179
        }
180

    
181
        public DisposableIterator iterator(long index) throws DataException {
182
                return new IndexIterator(this.featureReferences.iterator(index));
183
        }
184

    
185
        public void delete(Feature feature) throws DataException {
186
                index.delete(feature);
187
                store.getFeatureStore().delete(feature);
188
        }
189

    
190
        public FeatureType getDefaultFeatureType() {
191
                return index.getFeatureType();
192
        }
193

    
194
        public List getFeatureTypes() {
195
                List types = new ArrayList();
196
                types.add(index.getFeatureType());
197
                return Collections.unmodifiableList(types);
198
        }
199

    
200
        public void insert(EditableFeature feature) throws DataException {
201
                index.insert(feature);
202
                store.getFeatureStore().insert(feature);
203
        }
204

    
205
        public void update(EditableFeature feature) throws DataException {
206
                store.getFeatureStore().update(feature);
207
                // we need to re-index the feature, since its shape might have changed
208
                index.delete(feature);
209
                index.insert(feature);
210
        }
211

    
212
        public void dispose() {
213

    
214
        }
215

    
216
        public boolean isFromStore(DataStore store) {
217
                return this.store.equals(store);
218
        }
219

    
220
        public void accept(Visitor visitor) throws BaseException {
221
                accept(visitor, 0);
222
        }
223

    
224
        public void accept(Visitor visitor, long firstValueIndex)
225
                        throws BaseException {
226
                synchronized (store.getFeatureStore()) {
227
                        DisposableIterator iterator = fastIterator(firstValueIndex);
228
                        
229
                        if (iterator != null) {
230
                                try {
231
                                        while (iterator.hasNext()) {
232
                                                Feature feature = (Feature) iterator.next();
233
                                                visitor.visit(feature);
234
                                        }
235
                                } finally {
236
                                        iterator.dispose();
237
                                }
238
                        }
239
                }
240
        }
241

    
242
    public DynObjectSet getDynObjectSet() {
243
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore());
244
    }
245

    
246
    public DynObjectSet getDynObjectSet(boolean fast) {
247
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore(),
248
            fast);
249
    }
250

    
251
}