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

History | View | Annotate | Download (6.27 KB)

1 40559 jjdelcerro
/**
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 40435 jjdelcerro
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.exception.DataException;
34
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureReference;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39 43089 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
40 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
41 43089 jjdelcerro
import org.gvsig.fmap.dal.feature.impl.featureset.AbstractFeatureSet;
42 40435 jjdelcerro
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
51 43089 jjdelcerro
public class IndexFeatureSet
52
    extends AbstractFeatureSet
53
    implements FeatureSet, FeatureSetProvider, Iterable<Feature> {
54 40435 jjdelcerro
55
        LongList featureReferences = null;
56
        FeatureStoreProvider storeProvider = null;
57
        FeatureStoreProviderServices store = null;
58
        FeatureIndexProviderServices index = null;
59
        List featureTypes = null;
60
61
        public class IndexIterator implements DisposableIterator {
62
                Iterator it = null;
63
64
                public IndexIterator(Iterator it) {
65
                        this.it = it;
66
                }
67
68
                public boolean hasNext() {
69
                        return it.hasNext();
70
                }
71
72
                public Object next() {
73
                        Object oid = it.next();
74
                        FeatureReference ref = new DefaultFeatureReference(store
75
                                        .getFeatureStore(), oid);
76
                        try {
77
                                return store.getFeatureStore().getFeatureByReference(ref);
78
                        } catch (DataException e) {
79 42811 jjdelcerro
                                throw new ReadRuntimeException(store.getName(),oid, e);
80 40435 jjdelcerro
                        }
81
                }
82
83
                public void remove() {
84
                        throw new UnsupportedOperationException();
85
                }
86
87
                public void dispose() {
88
                        this.it = null;
89
                }
90
        }
91
92
        public class FastIndexIterator implements DisposableIterator {
93
                Iterator it = null;
94
                DefaultFeature feature = null;
95
96
                public FastIndexIterator(Iterator it) throws DataException {
97
                        this.it = it;
98
                        feature = (DefaultFeature) store.createFeature(storeProvider
99
                                        .createFeatureProvider(index.getFeatureType()));
100
                }
101
102
                public boolean hasNext() {
103
                        return it.hasNext();
104
                }
105
106
                public Object next() {
107
                        Object oid = it.next();
108
                        try {
109
                                //                                Long longer=new Long(((Integer)oid).longValue());
110
                                FeatureReference ref = new DefaultFeatureReference(store
111
                                                .getFeatureStore(), oid);
112
                                FeatureProvider data = storeProvider
113
                                                .getFeatureProviderByReference((FeatureReferenceProviderServices) ref);
114
115
                                feature.setData(data);
116
117
                                return feature;
118
                        } catch (DataException e) {
119 42811 jjdelcerro
                                throw new ReadRuntimeException(store.getName(),oid, e);
120 40435 jjdelcerro
                        }
121
                }
122
123
                public void remove() {
124
                        throw new UnsupportedOperationException();
125
                }
126
127
                public void dispose() {
128
                        this.it = null;
129
                        this.feature = null;
130
131
                }
132
        }
133
134
        public IndexFeatureSet(FeatureIndexProviderServices index, LongList featureReferences) {
135
                this.featureReferences = featureReferences;
136
                this.store = index.getFeatureStoreProviderServices();
137
                this.storeProvider = store.getProvider();
138
                this.index = index;
139
        }
140
141
        public boolean canFilter() {
142
                return false;
143
        }
144
145
        public boolean canIterateFromIndex() {
146
                return true;
147
        }
148
149
        public boolean canOrder() {
150
                return false;
151
        }
152
153 43358 jjdelcerro
    public DisposableIterator fastIterator(long index) throws DataException {
154
        return fastIterator(index, 0);
155
    }
156 40435 jjdelcerro
157 43358 jjdelcerro
    public DisposableIterator fastIterator(long index, long elements) throws DataException {
158
        if (store.getFeatureStore().isEditing()) {
159
            return this.iterator(index, elements);
160
        }
161
        return new FastIndexIterator(this.featureReferences.iterator(index));
162
    }
163
164
165 40435 jjdelcerro
        public long getSize() throws DataException {
166
                return featureReferences.getSize();
167
        }
168
169
        public boolean isEmpty() throws DataException {
170
                return featureReferences.isEmpty();
171
        }
172
173
        public DisposableIterator iterator(long index) throws DataException {
174
                return new IndexIterator(this.featureReferences.iterator(index));
175
        }
176
177 43358 jjdelcerro
        public DisposableIterator iterator(long index, long elements) throws DataException {
178
                return new IndexIterator(this.featureReferences.iterator(index));
179
        }
180
181 40435 jjdelcerro
        public void delete(Feature feature) throws DataException {
182
                index.delete(feature);
183
                store.getFeatureStore().delete(feature);
184
        }
185
186 44097 omartinez
        @Override
187
        public void commitChanges() throws DataException {
188
            store.getFeatureStore().commitChanges();
189
        }
190
191 40435 jjdelcerro
        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 43089 jjdelcerro
    @Override
218
    public FeatureStore getFeatureStore() {
219
        return this.store.getFeatureStore();
220 40435 jjdelcerro
    }
221
222 43089 jjdelcerro
223
224 40435 jjdelcerro
225 40767 jjdelcerro
}