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

History | View | Annotate | Download (6.93 KB)

1
/* 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
*
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 2
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
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

    
28

    
29
package org.gvsig.fmap.dal.feature.impl;
30

    
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.Iterator;
34
import java.util.List;
35

    
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureReference;
42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.impl.featureset.DynObjectSetFeatureSetFacade;
45
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
46
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
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
50
import org.gvsig.fmap.dal.feature.spi.LongList;
51
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices;
52
import org.gvsig.tools.dispose.DisposableIterator;
53
import org.gvsig.tools.dynobject.DynObjectSet;
54
import org.gvsig.tools.exception.BaseException;
55
import org.gvsig.tools.visitor.Visitor;
56

    
57

    
58
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
59

    
60
        LongList featureReferences = null;
61
        FeatureStoreProvider storeProvider = null;
62
        FeatureStoreProviderServices store = null;
63
        FeatureIndexProviderServices index = null;
64
        List featureTypes = null;
65

    
66
        public class IndexIterator implements DisposableIterator {
67
                Iterator it = null;
68

    
69
                public IndexIterator(Iterator it) {
70
                        this.it = it;
71
                }
72

    
73
                public boolean hasNext() {
74
                        return it.hasNext();
75
                }
76

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

    
88
                public void remove() {
89
                        throw new UnsupportedOperationException();
90
                }
91

    
92
                public void dispose() {
93
                        this.it = null;
94
                }
95
        }
96

    
97
        public class FastIndexIterator implements DisposableIterator {
98
                Iterator it = null;
99
                DefaultFeature feature = null;
100

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

    
107
                public boolean hasNext() {
108
                        return it.hasNext();
109
                }
110

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

    
120
                                feature.setData(data);
121

    
122
                                return feature;
123
                        } catch (DataException e) {
124
                                throw new ReadRuntimeException(store.getName(), e);
125
                        }
126
                }
127

    
128
                public void remove() {
129
                        throw new UnsupportedOperationException();
130
                }
131

    
132
                public void dispose() {
133
                        this.it = null;
134
                        this.feature = null;
135

    
136
                }
137
        }
138

    
139
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
140
                this.featureReferences = featureReferences;
141
                this.store = index.getFeatureStoreProviderServices();
142
                this.storeProvider = store.getProvider();
143
                this.index = index;
144
        }
145

    
146
        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
        public DisposableIterator fastIterator(long index) throws DataException {
159
                if (store.getFeatureStore().isEditing()) {
160
                        return this.iterator(index);
161
                }
162
                return new FastIndexIterator(this.featureReferences.iterator(index));
163
        }
164

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

    
172
        public long getSize() throws DataException {
173
                return featureReferences.getSize();
174
        }
175

    
176
        public boolean isEmpty() throws DataException {
177
                return featureReferences.isEmpty();
178
        }
179

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

    
184
        public DisposableIterator iterator(long index) throws DataException {
185
                return new IndexIterator(this.featureReferences.iterator(index));
186
        }
187

    
188
        public void delete(Feature feature) throws DataException {
189
                index.delete(feature);
190
                store.getFeatureStore().delete(feature);
191
        }
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
                store.getFeatureStore().insert(feature);
206
        }
207

    
208
        public void update(EditableFeature feature) throws DataException {
209
                store.getFeatureStore().update(feature);
210
                // 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

    
217
        }
218

    
219
        public boolean isFromStore(DataStore store) {
220
                return this.store.equals(store);
221
        }
222

    
223
        public void accept(Visitor visitor) throws BaseException {
224
                accept(visitor, 0);
225
        }
226

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

    
245
    public DynObjectSet getDynObjectSet() {
246
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore());
247
    }
248

    
249
    public DynObjectSet getDynObjectSet(boolean fast) {
250
        return new DynObjectSetFeatureSetFacade(this, store.getFeatureStore(),
251
            fast);
252
    }
253

    
254
}