Revision 35115 branches/dal_time_support/libraries/libFMap_dal/src/org/gvsig/fmap/dal/feature/impl/DefaultFeature.java
DefaultFeature.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.feature.impl; |
2 | 2 |
|
3 | 3 |
import java.lang.ref.WeakReference; |
4 |
import java.text.ParseException; |
|
5 | 4 |
import java.util.Date; |
6 | 5 |
import java.util.Iterator; |
7 | 6 |
import java.util.List; |
... | ... | |
23 | 22 |
import org.gvsig.fmap.geom.primitive.Envelope; |
24 | 23 |
import org.gvsig.timesupport.Instant; |
25 | 24 |
import org.gvsig.timesupport.Interval; |
26 |
import org.gvsig.timesupport.RelativeInstant; |
|
27 |
import org.gvsig.timesupport.RelativeInterval; |
|
25 |
import org.gvsig.tools.ToolsLocator; |
|
26 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
27 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
28 | 28 |
import org.gvsig.tools.evaluator.Evaluator; |
29 | 29 |
import org.gvsig.tools.evaluator.EvaluatorData; |
30 | 30 |
import org.gvsig.tools.evaluator.EvaluatorException; |
31 | 31 |
|
32 | 32 |
public class DefaultFeature implements Feature, EvaluatorData { |
33 | 33 |
|
34 |
private static DataTypesManager dataTypesManager = null; |
|
34 | 35 |
protected FeatureProvider data; |
35 | 36 |
protected FeatureReference reference; |
36 | 37 |
private WeakReference storeRef; |
37 | 38 |
|
39 |
|
|
38 | 40 |
/* |
39 | 41 |
* Usar con mucha precaucion o mejor no usar. Lo precisa el |
40 | 42 |
* DefaultFeatureSet en la ordenacion. |
... | ... | |
65 | 67 |
return this.data; |
66 | 68 |
} |
67 | 69 |
|
70 |
protected DataTypesManager getDataTypesManager() { |
|
71 |
if( dataTypesManager==null ) { |
|
72 |
dataTypesManager = ToolsLocator.getDataTypesManager(); |
|
73 |
} |
|
74 |
return dataTypesManager; |
|
75 |
} |
|
76 |
|
|
68 | 77 |
void set(FeatureAttributeDescriptor attribute, Object value) { |
69 | 78 |
int i = attribute.getIndex(); |
70 | 79 |
|
... | ... | |
92 | 101 |
return; |
93 | 102 |
} |
94 | 103 |
|
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; |
|
104 |
try { |
|
105 |
this.getDataTypesManager().coerce(attribute.getType(), value); |
|
106 |
} catch (CoercionException e) { |
|
107 |
throw new IllegalArgumentException( |
|
108 |
"Can't convert to "+this.getDataTypesManager().getTypeName(attribute.getType())+" from '"+value.getClass().getName()+"' with value '"+value.toString()+"'.", |
|
109 |
e); |
|
100 | 110 |
} |
111 |
|
|
112 |
// if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass())) |
|
113 |
// || (attribute.getType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) { |
|
114 |
// Object number=getNumberByType((Number)value, attribute.getType()); |
|
115 |
// this.data.set(i, number); |
|
116 |
// return; |
|
117 |
// } |
|
118 |
// |
|
119 |
// if (!(value instanceof String)) { |
|
120 |
// throw new IllegalValueException(attribute, value); |
|
121 |
// } |
|
122 |
// int dataType = attribute.getType(); |
|
123 |
// |
|
124 |
// switch (dataType) { |
|
125 |
// case DataTypes.BYTE: |
|
126 |
// this.data.set(i, Byte.valueOf((String) value)); |
|
127 |
// break; |
|
128 |
// |
|
129 |
// case DataTypes.DATE: |
|
130 |
// try { |
|
131 |
// this.data.set(i, attribute.getDateFormat() |
|
132 |
// .parse((String) value)); |
|
133 |
// } catch (ParseException e) { |
|
134 |
// throw new IllegalValueException(attribute, value, e); |
|
135 |
// } |
|
136 |
// break; |
|
137 |
// |
|
138 |
// case DataTypes.DOUBLE: |
|
139 |
// this.data.set(i, Double.valueOf((String) value)); |
|
140 |
// break; |
|
141 |
// |
|
142 |
// case DataTypes.FLOAT: |
|
143 |
// this.data.set(i, Float.valueOf((String) value)); |
|
144 |
// break; |
|
145 |
// |
|
146 |
// case DataTypes.INT: |
|
147 |
// this.data.set(i, Integer.valueOf((String) value)); |
|
148 |
// break; |
|
149 |
// |
|
150 |
// case DataTypes.LONG: |
|
151 |
// this.data.set(i, Long.valueOf((String) value)); |
|
152 |
// break; |
|
153 |
// |
|
154 |
// default: |
|
155 |
// throw new IllegalValueException(attribute, value); |
|
156 |
// } |
|
157 |
} |
|
101 | 158 |
|
102 |
if (!(value instanceof String)) { |
|
103 |
throw new IllegalValueException(attribute, value); |
|
159 |
private Object get(int index,Class theClass, int type) { |
|
160 |
Object value = this.get(index); |
|
161 |
if( theClass.isInstance(value) ) { |
|
162 |
return value; |
|
104 | 163 |
} |
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); |
|
164 |
try { |
|
165 |
return this.getDataTypesManager().coerce(type, value); |
|
166 |
} catch (CoercionException e) { |
|
167 |
throw new IllegalArgumentException( |
|
168 |
"Can't convert to "+theClass.getSimpleName()+" from '"+value.getClass().getName()+"' with value '"+value.toString()+"'.", |
|
169 |
e); |
|
139 | 170 |
} |
140 | 171 |
} |
172 |
|
|
173 |
// private Object getNumberByType(Number value, int type) { |
|
174 |
// if (type==DataTypes.DOUBLE){ |
|
175 |
// return new Double(value.doubleValue()); |
|
176 |
// }else if (type==DataTypes.FLOAT){ |
|
177 |
// return new Float(value.floatValue()); |
|
178 |
// }else if (type==DataTypes.LONG){ |
|
179 |
// return new Long(value.longValue()); |
|
180 |
// }else if (type==DataTypes.INT){ |
|
181 |
// return new Integer(value.intValue()); |
|
182 |
// }else if (type==DataTypes.STRING){ |
|
183 |
// return value.toString(); |
|
184 |
// } |
|
185 |
// return value; |
|
186 |
// } |
|
141 | 187 |
|
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 | 188 |
public void initializeValues() { |
158 | 189 |
FeatureType type = this.getType(); |
159 | 190 |
Iterator iterator = type.iterator(); |
... | ... | |
290 | 321 |
} |
291 | 322 |
|
292 | 323 |
public boolean getBoolean(int index) { |
293 |
Boolean value = (Boolean) this.get(index);
|
|
324 |
Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
|
|
294 | 325 |
if (value == null) { |
295 | 326 |
return false; |
296 | 327 |
} |
... | ... | |
302 | 333 |
} |
303 | 334 |
|
304 | 335 |
public byte getByte(int index) { |
305 |
Byte value = (Byte) this.get(index);
|
|
336 |
Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
|
|
306 | 337 |
if (value == null) { |
307 | 338 |
return 0; |
308 | 339 |
} |
... | ... | |
314 | 345 |
} |
315 | 346 |
|
316 | 347 |
public Date getDate(int index) { |
317 |
return (Date) this.get(index); |
|
348 |
Date value = ((Date) this.get(index,Date.class,DataTypes.DATE)); |
|
349 |
|
|
350 |
return value; |
|
318 | 351 |
} |
319 | 352 |
|
320 | 353 |
public double getDouble(String name) { |
... | ... | |
322 | 355 |
} |
323 | 356 |
|
324 | 357 |
public double getDouble(int index) { |
325 |
Double value = (Double) this.get(index); |
|
358 |
|
|
359 |
Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE)); |
|
326 | 360 |
if (value == null) { |
327 | 361 |
return 0; |
328 | 362 |
} |
... | ... | |
342 | 376 |
} |
343 | 377 |
|
344 | 378 |
public float getFloat(int index) { |
345 |
Float value = (Float) this.get(index);
|
|
379 |
Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
|
|
346 | 380 |
if (value == null) { |
347 | 381 |
return 0; |
348 | 382 |
} |
... | ... | |
354 | 388 |
} |
355 | 389 |
|
356 | 390 |
public Geometry getGeometry(int index) { |
357 |
return (Geometry) this.get(index); |
|
391 |
return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
|
|
358 | 392 |
} |
359 | 393 |
|
360 | 394 |
public int getInt(String name) { |
... | ... | |
362 | 396 |
} |
363 | 397 |
|
364 | 398 |
public int getInt(int index) { |
365 |
Integer value = (Integer) this.get(index);
|
|
399 |
Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
|
|
366 | 400 |
if (value == null) { |
367 | 401 |
return 0; |
368 | 402 |
} |
369 |
return value.intValue();
|
|
403 |
return ((Integer)value).intValue();
|
|
370 | 404 |
} |
371 | 405 |
|
372 | 406 |
public long getLong(String name) { |
... | ... | |
374 | 408 |
} |
375 | 409 |
|
376 | 410 |
public long getLong(int index) { |
377 |
Long value = (Long) this.get(index);
|
|
411 |
Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
|
|
378 | 412 |
if (value == null) { |
379 | 413 |
return 0; |
380 | 414 |
} |
... | ... | |
386 | 420 |
} |
387 | 421 |
|
388 | 422 |
public String getString(int index) { |
389 |
return (String) this.get(index); |
|
423 |
return (String) this.get(index,String.class,DataTypes.STRING);
|
|
390 | 424 |
} |
391 | 425 |
|
392 | 426 |
public Object getContextValue(String name) { |
... | ... | |
482 | 516 |
return this.data.getType().getIndex(name) >= 0; |
483 | 517 |
} |
484 | 518 |
|
485 |
public Instant getTimeInstant(int index) {
|
|
486 |
return (RelativeInstant) this.get(index);
|
|
487 |
}
|
|
519 |
public Instant getInstant(int index) {
|
|
520 |
return ((Instant) this.get(index,Date.class,DataTypes.INSTANT));
|
|
521 |
}
|
|
488 | 522 |
|
489 |
public Instant getTimeInstant(String name) {
|
|
490 |
return (RelativeInstant) this.get(this.data.getType().getIndex(name));
|
|
491 |
}
|
|
523 |
public Instant getInstant(String name) {
|
|
524 |
return this.getInstant(this.data.getType().getIndex(name));
|
|
525 |
}
|
|
492 | 526 |
|
493 |
public Interval getTimeInterval(int index) {
|
|
494 |
return (RelativeInterval) this.get(index);
|
|
495 |
}
|
|
527 |
public Interval getInterval(int index) {
|
|
528 |
return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
|
|
529 |
}
|
|
496 | 530 |
|
497 |
public Interval getTimeInterval(String name) {
|
|
498 |
return (RelativeInterval) this.get(this.data.getType().getIndex(name));
|
|
499 |
}
|
|
531 |
public Interval getInterval(String name) {
|
|
532 |
return this.getInterval(this.data.getType().getIndex(name));
|
|
533 |
}
|
|
500 | 534 |
} |
Also available in: Unified diff