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 @ 46426
History | View | Annotate | Download (6.35 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.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 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.dal.feature.impl.featurereference.FeatureReferenceFactory; |
42 |
import org.gvsig.fmap.dal.feature.impl.featureset.AbstractFeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
48 |
import org.gvsig.fmap.dal.feature.spi.LongList; |
49 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices; |
50 |
import org.gvsig.tools.dispose.DisposableIterator; |
51 |
|
52 |
public class IndexFeatureSet |
53 |
extends AbstractFeatureSet
|
54 |
implements FeatureSet, FeatureSetProvider, Iterable<Feature> { |
55 |
|
56 |
LongList featureReferences = null;
|
57 |
FeatureStoreProvider storeProvider = null;
|
58 |
FeatureStoreProviderServices store = null;
|
59 |
FeatureIndexProviderServices index = null;
|
60 |
List featureTypes = null; |
61 |
|
62 |
public class IndexIterator implements DisposableIterator { |
63 |
Iterator it = null; |
64 |
|
65 |
public IndexIterator(Iterator it) { |
66 |
this.it = it;
|
67 |
} |
68 |
|
69 |
public boolean hasNext() { |
70 |
return it.hasNext();
|
71 |
} |
72 |
|
73 |
public Object next() { |
74 |
Object oid = it.next();
|
75 |
FeatureReference ref = FeatureReferenceFactory.createFromOID( |
76 |
store.getFeatureStore(), oid); |
77 |
try {
|
78 |
return store.getFeatureStore().getFeatureByReference(ref);
|
79 |
} catch (DataException e) {
|
80 |
throw new ReadRuntimeException(store.getName(),oid, e); |
81 |
} |
82 |
} |
83 |
|
84 |
public void remove() { |
85 |
throw new UnsupportedOperationException(); |
86 |
} |
87 |
|
88 |
public void dispose() { |
89 |
this.it = null; |
90 |
} |
91 |
} |
92 |
|
93 |
public class FastIndexIterator implements DisposableIterator { |
94 |
Iterator it = null; |
95 |
DefaultFeature feature = null;
|
96 |
|
97 |
public FastIndexIterator(Iterator it) throws DataException { |
98 |
this.it = it;
|
99 |
feature = (DefaultFeature) store.createFeature(storeProvider |
100 |
.createFeatureProvider(index.getFeatureType())); |
101 |
} |
102 |
|
103 |
public boolean hasNext() { |
104 |
return it.hasNext();
|
105 |
} |
106 |
|
107 |
public Object next() { |
108 |
Object oid = it.next();
|
109 |
try {
|
110 |
FeatureReference ref = FeatureReferenceFactory.createFromOID( |
111 |
store.getFeatureStore(), oid); |
112 |
FeatureProvider data = storeProvider |
113 |
.getFeatureProviderByReference((FeatureReferenceProviderServices) ref); |
114 |
|
115 |
feature.setData(data); |
116 |
|
117 |
return feature;
|
118 |
} catch (DataException e) {
|
119 |
throw new ReadRuntimeException(store.getName(),oid, e); |
120 |
} |
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 |
public DisposableIterator fastIterator(long index) throws DataException { |
154 |
return fastIterator(index, 0); |
155 |
} |
156 |
|
157 |
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 |
public long getSize() throws DataException { |
166 |
return featureReferences.getSize();
|
167 |
} |
168 |
|
169 |
public boolean isEmpty() { |
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 |
public DisposableIterator iterator(long index, long elements) throws DataException { |
178 |
return new IndexIterator(this.featureReferences.iterator(index)); |
179 |
} |
180 |
|
181 |
public void delete(Feature feature) throws DataException { |
182 |
index.delete(feature); |
183 |
store.getFeatureStore().delete(feature); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public void commitChanges() throws DataException { |
188 |
store.getFeatureStore().commitChanges(); |
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 |
@Override
|
218 |
public FeatureStore getFeatureStore() {
|
219 |
return this.store.getFeatureStore(); |
220 |
} |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
} |