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 @ 43089
History | View | Annotate | Download (6.05 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.DataStore; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.ReadRuntimeException; |
36 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
37 |
import org.gvsig.fmap.dal.feature.Feature; |
38 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
39 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
42 |
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectSetFeatureSetFacade; |
43 |
import org.gvsig.fmap.dal.feature.impl.featureset.AbstractFeatureSet; |
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
48 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
49 |
import org.gvsig.fmap.dal.feature.spi.LongList; |
50 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProviderServices; |
51 |
import org.gvsig.tools.dispose.DisposableIterator; |
52 |
import org.gvsig.tools.dynobject.DynObjectSet; |
53 |
import org.gvsig.tools.exception.BaseException; |
54 |
import org.gvsig.tools.visitor.Visitor; |
55 |
|
56 |
public class IndexFeatureSet |
57 |
extends AbstractFeatureSet
|
58 |
implements FeatureSet, FeatureSetProvider, Iterable<Feature> { |
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(),oid, 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(),oid, 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 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 void delete(Feature feature) throws DataException { |
178 |
index.delete(feature); |
179 |
store.getFeatureStore().delete(feature); |
180 |
} |
181 |
|
182 |
public FeatureType getDefaultFeatureType() {
|
183 |
return index.getFeatureType();
|
184 |
} |
185 |
|
186 |
public List getFeatureTypes() { |
187 |
List types = new ArrayList(); |
188 |
types.add(index.getFeatureType()); |
189 |
return Collections.unmodifiableList(types); |
190 |
} |
191 |
|
192 |
public void insert(EditableFeature feature) throws DataException { |
193 |
index.insert(feature); |
194 |
store.getFeatureStore().insert(feature); |
195 |
} |
196 |
|
197 |
public void update(EditableFeature feature) throws DataException { |
198 |
store.getFeatureStore().update(feature); |
199 |
// we need to re-index the feature, since its shape might have changed
|
200 |
index.delete(feature); |
201 |
index.insert(feature); |
202 |
} |
203 |
|
204 |
public void dispose() { |
205 |
|
206 |
} |
207 |
|
208 |
@Override
|
209 |
public FeatureStore getFeatureStore() {
|
210 |
return this.store.getFeatureStore(); |
211 |
} |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
} |