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
/* 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.DisposableIterator;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureReference;
43
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureType;
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.exception.BaseException;
53
import org.gvsig.tools.visitor.Visitor;
54

    
55

    
56
public class IndexFeatureSet implements FeatureSet, FeatureSetProvider {
57

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

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

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

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

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

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

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

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

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

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

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

    
118
                                feature.setData(data);
119

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

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

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

    
134
                }
135
        }
136

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

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

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

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

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

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

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

    
186
        public void delete(Feature feature) throws DataException {
187
                index.delete(feature);
188
                store.getFeatureStore().delete(feature);
189
        }
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
                store.getFeatureStore().insert(feature);
204
        }
205

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

    
215
        }
216

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

    
221
        public void accept(Visitor visitor) throws BaseException {
222
                DisposableIterator iterator = iterator();
223

    
224
                while (iterator.hasNext()) {
225
                        Feature feature = (Feature) iterator.next();
226
                        visitor.visit(feature);
227
                }
228
                iterator.dispose();
229
        }
230

    
231
}
232