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 @ 44036
History | View | Annotate | Download (6.13 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.featureset.AbstractFeatureSet; |
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 |
|
51 |
public class IndexFeatureSet |
52 |
extends AbstractFeatureSet
|
53 |
implements FeatureSet, FeatureSetProvider, Iterable<Feature> { |
54 |
|
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 |
throw new ReadRuntimeException(store.getName(),oid, e); |
80 |
} |
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 |
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() 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 |
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 |
public FeatureType getDefaultFeatureType() {
|
187 |
return index.getFeatureType();
|
188 |
} |
189 |
|
190 |
public List getFeatureTypes() { |
191 |
List types = new ArrayList(); |
192 |
types.add(index.getFeatureType()); |
193 |
return Collections.unmodifiableList(types); |
194 |
} |
195 |
|
196 |
public void insert(EditableFeature feature) throws DataException { |
197 |
index.insert(feature); |
198 |
store.getFeatureStore().insert(feature); |
199 |
} |
200 |
|
201 |
public void update(EditableFeature feature) throws DataException { |
202 |
store.getFeatureStore().update(feature); |
203 |
// we need to re-index the feature, since its shape might have changed
|
204 |
index.delete(feature); |
205 |
index.insert(feature); |
206 |
} |
207 |
|
208 |
public void dispose() { |
209 |
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public FeatureStore getFeatureStore() {
|
214 |
return this.store.getFeatureStore(); |
215 |
} |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
} |