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