root / branches / dal_time_support / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 34409
History | View | Annotate | Download (12.3 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.text.ParseException; |
5 |
import java.util.Date; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
|
9 |
import org.cresques.cts.IProjection; |
10 |
|
11 |
import org.gvsig.fmap.dal.DataTypes; |
12 |
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
13 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
14 |
import org.gvsig.fmap.dal.feature.Feature; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.dal.feature.FeatureType; |
19 |
import org.gvsig.fmap.dal.feature.exception.IllegalValueException; |
20 |
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException; |
21 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
22 |
import org.gvsig.fmap.geom.Geometry; |
23 |
import org.gvsig.fmap.geom.primitive.Envelope; |
24 |
import org.gvsig.timesupport.Instant; |
25 |
import org.gvsig.timesupport.Interval; |
26 |
import org.gvsig.timesupport.Partial; |
27 |
import org.gvsig.timesupport.Period; |
28 |
import org.gvsig.tools.evaluator.Evaluator; |
29 |
import org.gvsig.tools.evaluator.EvaluatorData; |
30 |
import org.gvsig.tools.evaluator.EvaluatorException; |
31 |
|
32 |
public class DefaultFeature implements Feature, EvaluatorData { |
33 |
|
34 |
protected FeatureProvider data;
|
35 |
protected FeatureReference reference;
|
36 |
private WeakReference storeRef; |
37 |
|
38 |
/*
|
39 |
* Usar con mucha precaucion o mejor no usar. Lo precisa el
|
40 |
* DefaultFeatureSet en la ordenacion.
|
41 |
*/
|
42 |
public DefaultFeature(FeatureStore store) {
|
43 |
this.storeRef = new WeakReference(store); |
44 |
this.reference = null; |
45 |
} |
46 |
|
47 |
public DefaultFeature(FeatureStore store, FeatureProvider data) {
|
48 |
this.data = data;
|
49 |
this.storeRef = new WeakReference(store); |
50 |
this.reference = null; |
51 |
} |
52 |
|
53 |
DefaultFeature(DefaultFeature feature) { |
54 |
this.data = feature.data.getCopy();
|
55 |
this.storeRef = feature.storeRef;
|
56 |
this.reference = feature.reference;
|
57 |
} |
58 |
|
59 |
public void setData(FeatureProvider data) { |
60 |
this.data = data;
|
61 |
this.reference = null; |
62 |
} |
63 |
|
64 |
public FeatureProvider getData() {
|
65 |
return this.data; |
66 |
} |
67 |
|
68 |
void set(FeatureAttributeDescriptor attribute, Object value) { |
69 |
int i = attribute.getIndex();
|
70 |
|
71 |
if (attribute.isReadOnly()) {
|
72 |
throw new SetReadOnlyAttributeException(); |
73 |
} |
74 |
|
75 |
if (attribute.getEvaluator() != null) { |
76 |
throw new SetReadOnlyAttributeException(); |
77 |
} |
78 |
|
79 |
if (value == null) { |
80 |
if (!attribute.allowNull()) {
|
81 |
if (!attribute.isAutomatic()) {
|
82 |
throw new IllegalValueException(attribute, value); |
83 |
} |
84 |
} |
85 |
this.data.set(i, null); |
86 |
return;
|
87 |
|
88 |
} |
89 |
|
90 |
if (attribute.getObjectClass().isInstance(value)) {
|
91 |
this.data.set(i, value);
|
92 |
return;
|
93 |
} |
94 |
|
95 |
if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass())) |
96 |
|| (attribute.getType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) {
|
97 |
Object number=getNumberByType((Number)value, attribute.getType()); |
98 |
this.data.set(i, number);
|
99 |
return;
|
100 |
} |
101 |
|
102 |
if (!(value instanceof String)) { |
103 |
throw new IllegalValueException(attribute, value); |
104 |
} |
105 |
int dataType = attribute.getType();
|
106 |
|
107 |
switch (dataType) {
|
108 |
case DataTypes.BYTE:
|
109 |
this.data.set(i, Byte.valueOf((String) value)); |
110 |
break;
|
111 |
|
112 |
case DataTypes.DATE:
|
113 |
try {
|
114 |
this.data.set(i, attribute.getDateFormat()
|
115 |
.parse((String) value));
|
116 |
} catch (ParseException e) { |
117 |
throw new IllegalValueException(attribute, value, e); |
118 |
} |
119 |
break;
|
120 |
|
121 |
case DataTypes.DOUBLE:
|
122 |
this.data.set(i, Double.valueOf((String) value)); |
123 |
break;
|
124 |
|
125 |
case DataTypes.FLOAT:
|
126 |
this.data.set(i, Float.valueOf((String) value)); |
127 |
break;
|
128 |
|
129 |
case DataTypes.INT:
|
130 |
this.data.set(i, Integer.valueOf((String) value)); |
131 |
break;
|
132 |
|
133 |
case DataTypes.LONG:
|
134 |
this.data.set(i, Long.valueOf((String) value)); |
135 |
break;
|
136 |
|
137 |
default:
|
138 |
throw new IllegalValueException(attribute, value); |
139 |
} |
140 |
} |
141 |
|
142 |
private Object getNumberByType(Number value, int type) { |
143 |
if (type==DataTypes.DOUBLE){
|
144 |
return new Double(value.doubleValue()); |
145 |
}else if (type==DataTypes.FLOAT){ |
146 |
return new Float(value.floatValue()); |
147 |
}else if (type==DataTypes.LONG){ |
148 |
return new Long(value.longValue()); |
149 |
}else if (type==DataTypes.INT){ |
150 |
return new Integer(value.intValue()); |
151 |
}else if (type==DataTypes.STRING){ |
152 |
return value.toString();
|
153 |
} |
154 |
return value;
|
155 |
} |
156 |
|
157 |
public void initializeValues() { |
158 |
FeatureType type = this.getType();
|
159 |
Iterator iterator = type.iterator();
|
160 |
|
161 |
while (iterator.hasNext()) {
|
162 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
163 |
.next(); |
164 |
if (attribute.isAutomatic() || attribute.isReadOnly()
|
165 |
|| attribute.getEvaluator() != null) {
|
166 |
continue;
|
167 |
} |
168 |
if (attribute.getDefaultValue() == null && !attribute.allowNull()) { |
169 |
continue;
|
170 |
} |
171 |
this.set(attribute, attribute.getDefaultValue());
|
172 |
} |
173 |
} |
174 |
|
175 |
public void clear() { |
176 |
initializeValues(); |
177 |
} |
178 |
|
179 |
public void initializeValues(Feature feature) { |
180 |
FeatureType myType=this.getType();
|
181 |
FeatureType type =feature.getType(); |
182 |
Iterator iterator = type.iterator();
|
183 |
|
184 |
while (iterator.hasNext()) {
|
185 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
186 |
.next(); |
187 |
FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName()); |
188 |
if (myAttribute != null) { |
189 |
this.set(myAttribute, feature.get(attribute.getIndex()));
|
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
public FeatureStore getStore() {
|
195 |
return (FeatureStore) this.storeRef.get(); |
196 |
} |
197 |
|
198 |
public FeatureType getType() {
|
199 |
return this.data.getType(); |
200 |
} |
201 |
|
202 |
public EditableFeature getEditable() {
|
203 |
return new DefaultEditableFeature(this); |
204 |
} |
205 |
|
206 |
public Feature getCopy() {
|
207 |
return new DefaultFeature(this); |
208 |
} |
209 |
|
210 |
public FeatureReference getReference() {
|
211 |
if (this.reference == null) { |
212 |
this.reference = new DefaultFeatureReference(this); |
213 |
} |
214 |
return this.reference; |
215 |
} |
216 |
|
217 |
public void validate(int mode) { |
218 |
((DefaultFeatureType) this.data.getType()).validateFeature(this, mode); |
219 |
} |
220 |
|
221 |
public List getSRSs() { |
222 |
// TODO Auto-generated method stub
|
223 |
return null; |
224 |
} |
225 |
|
226 |
public Envelope getDefaultEnvelope() {
|
227 |
return this.data.getDefaultEnvelope(); |
228 |
} |
229 |
|
230 |
public Geometry getDefaultGeometry() {
|
231 |
return this.data.getDefaultGeometry(); |
232 |
} |
233 |
|
234 |
public IProjection getDefaultSRS() {
|
235 |
return this.data.getType().getDefaultSRS(); |
236 |
} |
237 |
|
238 |
public List getGeometries() { |
239 |
// TODO Auto-generated method stub
|
240 |
return null; |
241 |
} |
242 |
|
243 |
public Object get(String name) { |
244 |
int index = this.data.getType().getIndex(name); |
245 |
if( index < 0 ) { |
246 |
throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature."); |
247 |
} |
248 |
return this.get(index); |
249 |
} |
250 |
|
251 |
public Object get(int index) { |
252 |
FeatureType type = this.data.getType();
|
253 |
if( index <0 || index >= type.size() ) { |
254 |
throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+"."); |
255 |
} |
256 |
if (!this.data.getType().hasEvaluators()) { |
257 |
return this.data.get(index); |
258 |
} |
259 |
FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index); |
260 |
Evaluator eval = attribute.getEvaluator(); |
261 |
if (eval == null) { |
262 |
return this.data.get(index); |
263 |
} else {
|
264 |
Object value = this.data.get(index); |
265 |
if (value != null) { // FIXME: para comprobar si esta calculado usar |
266 |
// un array
|
267 |
// especifico.
|
268 |
return this.data.get(index); |
269 |
} |
270 |
try {
|
271 |
value = eval.evaluate(this);
|
272 |
} catch (EvaluatorException e) {
|
273 |
throw new DataEvaluatorRuntimeException(e); |
274 |
} |
275 |
this.data.set(index, value);
|
276 |
return value;
|
277 |
} |
278 |
} |
279 |
|
280 |
public Object[] getArray(String name) { |
281 |
return this.getArray(this.data.getType().getIndex(name)); |
282 |
} |
283 |
|
284 |
public Object[] getArray(int index) { |
285 |
return (Object[]) this.get(index); |
286 |
} |
287 |
|
288 |
public boolean getBoolean(String name) { |
289 |
return this.getBoolean(this.data.getType().getIndex(name)); |
290 |
} |
291 |
|
292 |
public boolean getBoolean(int index) { |
293 |
Boolean value = (Boolean) this.get(index); |
294 |
if (value == null) { |
295 |
return false; |
296 |
} |
297 |
return value.booleanValue();
|
298 |
} |
299 |
|
300 |
public byte getByte(String name) { |
301 |
return this.getByte(this.data.getType().getIndex(name)); |
302 |
} |
303 |
|
304 |
public byte getByte(int index) { |
305 |
Byte value = (Byte) this.get(index); |
306 |
if (value == null) { |
307 |
return 0; |
308 |
} |
309 |
return value.byteValue();
|
310 |
} |
311 |
|
312 |
public Date getDate(String name) { |
313 |
return this.getDate(this.data.getType().getIndex(name)); |
314 |
} |
315 |
|
316 |
public Date getDate(int index) { |
317 |
return (Date) this.get(index); |
318 |
} |
319 |
|
320 |
public double getDouble(String name) { |
321 |
return this.getDouble(this.data.getType().getIndex(name)); |
322 |
} |
323 |
|
324 |
public double getDouble(int index) { |
325 |
Double value = (Double) this.get(index); |
326 |
if (value == null) { |
327 |
return 0; |
328 |
} |
329 |
return value.doubleValue();
|
330 |
} |
331 |
|
332 |
public Feature getFeature(String name) { |
333 |
return this.getFeature(this.data.getType().getIndex(name)); |
334 |
} |
335 |
|
336 |
public Feature getFeature(int index) { |
337 |
return (Feature) this.get(index); |
338 |
} |
339 |
|
340 |
public float getFloat(String name) { |
341 |
return this.getFloat(this.data.getType().getIndex(name)); |
342 |
} |
343 |
|
344 |
public float getFloat(int index) { |
345 |
Float value = (Float) this.get(index); |
346 |
if (value == null) { |
347 |
return 0; |
348 |
} |
349 |
return value.floatValue();
|
350 |
} |
351 |
|
352 |
public Geometry getGeometry(String name) { |
353 |
return this.getGeometry(this.data.getType().getIndex(name)); |
354 |
} |
355 |
|
356 |
public Geometry getGeometry(int index) { |
357 |
return (Geometry) this.get(index); |
358 |
} |
359 |
|
360 |
public int getInt(String name) { |
361 |
return this.getInt(this.data.getType().getIndex(name)); |
362 |
} |
363 |
|
364 |
public int getInt(int index) { |
365 |
Integer value = (Integer) this.get(index); |
366 |
if (value == null) { |
367 |
return 0; |
368 |
} |
369 |
return value.intValue();
|
370 |
} |
371 |
|
372 |
public long getLong(String name) { |
373 |
return this.getLong(this.data.getType().getIndex(name)); |
374 |
} |
375 |
|
376 |
public long getLong(int index) { |
377 |
Long value = (Long) this.get(index); |
378 |
if (value == null) { |
379 |
return 0; |
380 |
} |
381 |
return value.longValue();
|
382 |
} |
383 |
|
384 |
public String getString(String name) { |
385 |
return this.getString(this.data.getType().getIndex(name)); |
386 |
} |
387 |
|
388 |
public String getString(int index) { |
389 |
return (String) this.get(index); |
390 |
} |
391 |
|
392 |
public Object getContextValue(String name) { |
393 |
name = name.toLowerCase(); |
394 |
if (name.equals("store")) { |
395 |
return this.getStore(); |
396 |
} |
397 |
|
398 |
if (name.equals("featuretype")) { |
399 |
return this.data.getType(); |
400 |
} |
401 |
|
402 |
if (name.equals("feature")) { |
403 |
return this; |
404 |
} |
405 |
|
406 |
throw new IllegalArgumentException(name); |
407 |
} |
408 |
|
409 |
public Iterator getDataNames() { |
410 |
class DataNamesIterator implements Iterator { |
411 |
Iterator attributeIteraror;
|
412 |
|
413 |
DataNamesIterator(DefaultFeature feature) { |
414 |
this.attributeIteraror = feature.getType().iterator();
|
415 |
} |
416 |
|
417 |
public boolean hasNext() { |
418 |
return this.attributeIteraror.hasNext(); |
419 |
} |
420 |
|
421 |
public Object next() { |
422 |
return ((FeatureAttributeDescriptor) this.attributeIteraror |
423 |
.next()).getName(); |
424 |
} |
425 |
|
426 |
public void remove() { |
427 |
throw new UnsupportedOperationException(); |
428 |
} |
429 |
|
430 |
} |
431 |
return new DataNamesIterator(this); |
432 |
} |
433 |
|
434 |
public Object getDataValue(String name) { |
435 |
name = name.toLowerCase(); |
436 |
return get(name);
|
437 |
} |
438 |
|
439 |
public Iterator getDataValues() { |
440 |
class DataValuesIterator implements Iterator { |
441 |
DefaultFeature feature; |
442 |
int current = 0; |
443 |
|
444 |
DataValuesIterator(DefaultFeature feature) { |
445 |
this.feature = feature;
|
446 |
} |
447 |
|
448 |
public boolean hasNext() { |
449 |
return current < feature.getType().size() - 1; |
450 |
} |
451 |
|
452 |
public Object next() { |
453 |
return feature.get(current++);
|
454 |
} |
455 |
|
456 |
public void remove() { |
457 |
throw new UnsupportedOperationException(); |
458 |
} |
459 |
|
460 |
} |
461 |
return new DataValuesIterator(this); |
462 |
} |
463 |
|
464 |
public boolean hasContextValue(String name) { |
465 |
name = name.toLowerCase(); |
466 |
if (name.equals("store")) { |
467 |
return true; |
468 |
} |
469 |
|
470 |
if (name.equals("featuretype")) { |
471 |
return true; |
472 |
} |
473 |
|
474 |
if (name.equals("feature")) { |
475 |
return true; |
476 |
} |
477 |
return false; |
478 |
} |
479 |
|
480 |
public boolean hasDataValue(String name) { |
481 |
name = name.toLowerCase(); |
482 |
return this.data.getType().getIndex(name) >= 0; |
483 |
} |
484 |
|
485 |
public Instant getTimeInstant(int index) { |
486 |
return (Instant) this.get(index); |
487 |
} |
488 |
|
489 |
public Instant getTimeInstant(String name) { |
490 |
return (Instant) this.get(this.data.getType().getIndex(name)); |
491 |
} |
492 |
|
493 |
public Partial getTimePartial(int index) { |
494 |
return (Partial) this.get(index); |
495 |
} |
496 |
|
497 |
public Partial getTimePartial(String name) { |
498 |
return (Partial) this.get(this.data.getType().getIndex(name)); |
499 |
} |
500 |
|
501 |
public Interval getTimeInterval(int index) { |
502 |
return (Interval) this.get(index); |
503 |
} |
504 |
|
505 |
public Interval getTimeInterval(String name) { |
506 |
return (Interval) this.get(this.data.getType().getIndex(name)); |
507 |
} |
508 |
|
509 |
public Period getTimePeriod(int index) { |
510 |
return (Period) this.get(index); |
511 |
} |
512 |
|
513 |
public Period getTimePeriod(String name) { |
514 |
return (Period) this.get(this.data.getType().getIndex(name)); |
515 |
} |
516 |
} |