svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / featureSet / DefaultFeatureSet.java @ 23842
History | View | Annotate | Download (6.78 KB)
1 |
package org.gvsig.fmap.data.feature.impl.featureSet; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
5 |
import java.util.ConcurrentModificationException; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
import java.util.NoSuchElementException; |
9 |
|
10 |
import org.gvsig.fmap.data.DataStore; |
11 |
import org.gvsig.fmap.data.exceptions.DataException; |
12 |
import org.gvsig.fmap.data.feature.Feature; |
13 |
import org.gvsig.fmap.data.feature.FeatureSet; |
14 |
import org.gvsig.fmap.data.feature.FeatureQuery; |
15 |
import org.gvsig.fmap.data.feature.FeatureStoreNotification; |
16 |
import org.gvsig.fmap.data.feature.FeatureType; |
17 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureStore; |
18 |
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider; |
19 |
import org.gvsig.tools.exception.BaseException; |
20 |
import org.gvsig.tools.observer.Observable; |
21 |
import org.gvsig.tools.observer.Observer; |
22 |
import org.gvsig.tools.visitor.Visitor; |
23 |
|
24 |
public class DefaultFeatureSet implements FeatureSet, Observer { |
25 |
|
26 |
|
27 |
private static final int NO_CHECKED = -1; |
28 |
private static final int DEFAULT = 0; |
29 |
private static final int FILTERED = 1; |
30 |
private static final int ORDERED = 2; |
31 |
private static final int ORDERED_FILTERED = 3; |
32 |
private static final int EDITED = 4; |
33 |
private static final int EDITED_FILTERED = 5; |
34 |
private static final int ORDERD_EDITED = 6; |
35 |
private static final int ORDERED_EDITED_FILTER = 7; |
36 |
|
37 |
private boolean modified = false; |
38 |
DefaultFeatureStore store; |
39 |
private List featureTypes; |
40 |
FeatureQuery query; |
41 |
FeatureSetProvider provider; |
42 |
private long size = -1; |
43 |
private int iteratorMode = NO_CHECKED; |
44 |
List orderedData = null; |
45 |
|
46 |
|
47 |
public DefaultFeatureSet(DefaultFeatureStore store, FeatureQuery query)
|
48 |
throws DataException {
|
49 |
this.store = store;
|
50 |
this.query = query;
|
51 |
this.featureTypes = new ArrayList(); |
52 |
this.featureTypes.add(this.query.getFeatureType()); |
53 |
this.provider = this.store.getProvider().createSet(query); |
54 |
} |
55 |
|
56 |
public FeatureType getDefaultFeatureType() {
|
57 |
return this.query.getFeatureType(); |
58 |
} |
59 |
|
60 |
public List getFeatureTypes() { |
61 |
return Collections.unmodifiableList(this.featureTypes); |
62 |
} |
63 |
|
64 |
public long getSize() throws DataException { |
65 |
if (size < 0) { |
66 |
size = calculateSize(); |
67 |
} |
68 |
return size;
|
69 |
} |
70 |
|
71 |
private long calculateSize() throws DataException { |
72 |
int mode = this.getIteratorMode(); |
73 |
if ((mode & FILTERED) == FILTERED) {
|
74 |
long mySize =0; |
75 |
Iterator iter = this.fastIterator(); |
76 |
try{
|
77 |
while (true) { |
78 |
iter.next(); |
79 |
mySize++; |
80 |
} |
81 |
} catch (NoSuchElementException e){ |
82 |
return mySize;
|
83 |
} |
84 |
} else if ((mode & EDITED) == EDITED) { |
85 |
return provider.getSize()
|
86 |
+ store.getFeatureManager().getDeltaSize(); |
87 |
} |
88 |
return provider.getSize();
|
89 |
} |
90 |
|
91 |
public void dispose() { |
92 |
|
93 |
} |
94 |
|
95 |
public boolean isFromStore(DataStore store) { |
96 |
return this.store.equals(store); |
97 |
} |
98 |
|
99 |
public void update(Observable obsevable, Object notification) { |
100 |
if (modified) {
|
101 |
return;
|
102 |
} |
103 |
String type = ((FeatureStoreNotification) notification).getType();
|
104 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
|
105 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_DELETE) |
106 |
|| type |
107 |
.equalsIgnoreCase(FeatureStoreNotification.RESOURCE_CHANGED)) { |
108 |
modified = true;
|
109 |
} |
110 |
} |
111 |
|
112 |
/*
|
113 |
* (non-Javadoc)
|
114 |
*
|
115 |
* @see
|
116 |
* org.gvsig.fmap.data.feature.visitor.FeaturesVisitable#accept(org.gvsig
|
117 |
* .fmap.data.feature.visitor.FeaturesVisitor)
|
118 |
*/
|
119 |
public void accept(Visitor visitor) throws BaseException { |
120 |
Iterator iterator = iterator();
|
121 |
|
122 |
while (iterator.hasNext()) {
|
123 |
Feature feature = (Feature) iterator.next(); |
124 |
visitor.visit(feature); |
125 |
} |
126 |
} |
127 |
|
128 |
protected void checkModified() { |
129 |
if (modified) {
|
130 |
throw new ConcurrentModificationException( |
131 |
"FeatureCollection modified");
|
132 |
} |
133 |
} |
134 |
|
135 |
public boolean isEmpty() throws DataException { |
136 |
checkModified(); |
137 |
if (this.store.isEditing()) { |
138 |
if (this.store.getFeatureManager().hasNews()) { |
139 |
return false; |
140 |
} |
141 |
if (this.provider.isEmpty()) { |
142 |
return true; |
143 |
} |
144 |
return this.provider.getSize() |
145 |
+ this.store.getFeatureManager().getDeltaSize() == 0; |
146 |
} |
147 |
return this.provider.isEmpty(); |
148 |
} |
149 |
public Iterator fastIterator() { |
150 |
return this.fastIterator(0); |
151 |
} |
152 |
|
153 |
public Iterator fastIterator(long index) { |
154 |
if (index < 0) { |
155 |
throw new IndexOutOfBoundsException("The index (" + index |
156 |
+ ") is less than 0");
|
157 |
} |
158 |
int mode = this.getIteratorMode(); |
159 |
|
160 |
switch (mode) {
|
161 |
case DEFAULT:
|
162 |
return new FastDefaultIterator(this, index); |
163 |
|
164 |
case FILTERED:
|
165 |
return new FastFilteredIterator(this, index); |
166 |
|
167 |
case ORDERED_FILTERED:
|
168 |
return new FastOrderedIterator(this, new FastDefaultIterator(this, |
169 |
0),
|
170 |
index); |
171 |
|
172 |
case EDITED:
|
173 |
return new FastEditedIterator(this, index); |
174 |
|
175 |
case EDITED_FILTERED:
|
176 |
return new FastEditedFilteredIterator(this, index); |
177 |
|
178 |
case ORDERD_EDITED:
|
179 |
return new FastOrderedIterator(this, |
180 |
new FastEditedIterator(this, 0), index); |
181 |
|
182 |
case ORDERED_EDITED_FILTER:
|
183 |
return new FastOrderedIterator(this, |
184 |
new FastEditedFilteredIterator(this, 0), index); |
185 |
|
186 |
default:
|
187 |
throw new IllegalArgumentException(); |
188 |
} |
189 |
} |
190 |
|
191 |
public Iterator iterator() { |
192 |
return this.iterator(0); |
193 |
} |
194 |
|
195 |
public Iterator iterator(long index) { |
196 |
if (index < 0) { |
197 |
throw new IndexOutOfBoundsException("The index (" + index |
198 |
+ ") is less than 0");
|
199 |
} |
200 |
int mode = this.getIteratorMode(); |
201 |
|
202 |
switch (mode) {
|
203 |
case DEFAULT:
|
204 |
return new DefaultIterator(this, index); |
205 |
|
206 |
case FILTERED:
|
207 |
return new FilteredIterator(this, index); |
208 |
|
209 |
case ORDERED_FILTERED:
|
210 |
return new OrderedIterator(this, new DefaultIterator(this, 0), |
211 |
index); |
212 |
|
213 |
case EDITED:
|
214 |
return new EditedIterator(this, index); |
215 |
|
216 |
case EDITED_FILTERED:
|
217 |
return new EditedFilteredIterator(this, index); |
218 |
|
219 |
case ORDERD_EDITED:
|
220 |
return new OrderedIterator(this, |
221 |
new EditedIterator(this, 0), index); |
222 |
|
223 |
case ORDERED_EDITED_FILTER:
|
224 |
return new OrderedIterator(this, |
225 |
new EditedFilteredIterator(this, 0), index); |
226 |
|
227 |
default:
|
228 |
throw new IllegalArgumentException(); |
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
private int getIteratorMode() { |
234 |
|
235 |
if (this.iteratorMode != NO_CHECKED) { |
236 |
return this.iteratorMode; |
237 |
} |
238 |
|
239 |
if (store.isEditing() && store.getFeatureManager().hasChanges()) {
|
240 |
if (this.query.hasOrder()) { // En edicion siempre ordeno yo. |
241 |
if (this.query.hasFilter()) { |
242 |
return ORDERED_EDITED_FILTER;
|
243 |
} else {
|
244 |
return ORDERD_EDITED;
|
245 |
} |
246 |
} else {
|
247 |
if (this.query.hasFilter()) { |
248 |
return EDITED_FILTERED;
|
249 |
} else {
|
250 |
return EDITED;
|
251 |
} |
252 |
} |
253 |
} else {
|
254 |
boolean useMyFilter = this.query.hasFilter(); |
255 |
boolean useMyOrder = this.query.hasOrder(); |
256 |
if (this.provider.canOrder()) { |
257 |
useMyOrder = false;
|
258 |
} |
259 |
if (this.provider.canFilter()) { |
260 |
useMyFilter = false;
|
261 |
} |
262 |
|
263 |
if (useMyOrder) {
|
264 |
if (useMyFilter) {
|
265 |
return ORDERED_FILTERED;// ORDERED_FILTERED; |
266 |
} else {
|
267 |
return ORDERED;// ORDERED; |
268 |
} |
269 |
} else {
|
270 |
if (useMyFilter) {
|
271 |
return FILTERED;// FILTERED; |
272 |
} else {
|
273 |
return DEFAULT;// DEFAULT; |
274 |
} |
275 |
} |
276 |
} |
277 |
|
278 |
} |
279 |
|
280 |
} |