svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 37327
History | View | Annotate | Download (15.6 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.impl.featureset.DynObjectFeatureFacade; |
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.tools.ToolsLocator; |
27 |
import org.gvsig.tools.dataTypes.CoercionException; |
28 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
29 |
import org.gvsig.tools.dynobject.DynObject; |
30 |
import org.gvsig.tools.evaluator.Evaluator; |
31 |
import org.gvsig.tools.evaluator.EvaluatorData; |
32 |
import org.gvsig.tools.evaluator.EvaluatorException; |
33 |
|
34 |
public class DefaultFeature implements Feature, EvaluatorData { |
35 |
|
36 |
private static DataTypesManager dataTypesManager = null; |
37 |
protected FeatureProvider data;
|
38 |
protected FeatureReference reference;
|
39 |
private WeakReference storeRef; |
40 |
|
41 |
|
42 |
/*
|
43 |
* Usar con mucha precaucion o mejor no usar. Lo precisa el
|
44 |
* DefaultFeatureSet en la ordenacion.
|
45 |
*/
|
46 |
public DefaultFeature(FeatureStore store) {
|
47 |
this.storeRef = new WeakReference(store); |
48 |
this.reference = null; |
49 |
} |
50 |
|
51 |
public DefaultFeature(FeatureStore store, FeatureProvider data) {
|
52 |
this.data = data;
|
53 |
this.storeRef = new WeakReference(store); |
54 |
this.reference = null; |
55 |
} |
56 |
|
57 |
DefaultFeature(DefaultFeature feature) { |
58 |
this.data = feature.data.getCopy();
|
59 |
this.storeRef = feature.storeRef;
|
60 |
this.reference = feature.reference;
|
61 |
} |
62 |
|
63 |
public void setData(FeatureProvider data) { |
64 |
this.data = data;
|
65 |
this.reference = null; |
66 |
} |
67 |
|
68 |
public FeatureProvider getData() {
|
69 |
return this.data; |
70 |
} |
71 |
|
72 |
protected DataTypesManager getDataTypesManager() {
|
73 |
if( dataTypesManager==null ) { |
74 |
dataTypesManager = ToolsLocator.getDataTypesManager(); |
75 |
} |
76 |
return dataTypesManager;
|
77 |
} |
78 |
|
79 |
void set(FeatureAttributeDescriptor attribute, Object value) { |
80 |
int i = attribute.getIndex();
|
81 |
|
82 |
if (attribute.isReadOnly()) {
|
83 |
throw new SetReadOnlyAttributeException(); |
84 |
} |
85 |
|
86 |
if (attribute.getEvaluator() != null) { |
87 |
throw new SetReadOnlyAttributeException(); |
88 |
} |
89 |
|
90 |
if (value == null) { |
91 |
if (!attribute.allowNull()) {
|
92 |
if (!attribute.isAutomatic()) {
|
93 |
throw new IllegalValueException(attribute, value); |
94 |
} |
95 |
} |
96 |
this.data.set(i, null); |
97 |
return;
|
98 |
|
99 |
} |
100 |
|
101 |
if (attribute.getFeatureAttributeGetter() != null){ |
102 |
value = attribute.getFeatureAttributeGetter().setter(value); |
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 |
|
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 |
|
252 |
public Object get(int index) { |
253 |
FeatureType type = this.data.getType();
|
254 |
if( index <0 || index >= type.size() ) { |
255 |
throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+"."); |
256 |
} |
257 |
FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index); |
258 |
if (!this.data.getType().hasEvaluators()) { |
259 |
return get(attribute, this.data.get(index)); |
260 |
} |
261 |
Evaluator eval = attribute.getEvaluator(); |
262 |
if (eval == null) { |
263 |
return this.data.get(index); |
264 |
} else {
|
265 |
Object value = this.data.get(index); |
266 |
if (value != null) { // FIXME: para comprobar si esta calculado usar |
267 |
// un array
|
268 |
// especifico.
|
269 |
return get(attribute, this.data.get(index)); |
270 |
} |
271 |
try {
|
272 |
value = eval.evaluate(this);
|
273 |
} catch (EvaluatorException e) {
|
274 |
throw new DataEvaluatorRuntimeException(e); |
275 |
} |
276 |
this.data.set(index, value);
|
277 |
return get(attribute, value);
|
278 |
} |
279 |
} |
280 |
|
281 |
private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){ |
282 |
if (featureAttributeDescriptor.getFeatureAttributeGetter() == null){ |
283 |
return value;
|
284 |
}else{
|
285 |
return featureAttributeDescriptor.getFeatureAttributeGetter().getter(value);
|
286 |
} |
287 |
} |
288 |
|
289 |
public Object[] getArray(String name) { |
290 |
return this.getArray(this.data.getType().getIndex(name)); |
291 |
} |
292 |
|
293 |
public Object[] getArray(int index) { |
294 |
return (Object[]) this.get(index); |
295 |
} |
296 |
|
297 |
public boolean getBoolean(String name) { |
298 |
return this.getBoolean(this.data.getType().getIndex(name)); |
299 |
} |
300 |
|
301 |
public boolean getBoolean(int index) { |
302 |
Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN)); |
303 |
if (value == null) { |
304 |
return false; |
305 |
} |
306 |
return value.booleanValue();
|
307 |
} |
308 |
|
309 |
public byte getByte(String name) { |
310 |
return this.getByte(this.data.getType().getIndex(name)); |
311 |
} |
312 |
|
313 |
public byte getByte(int index) { |
314 |
Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE)); |
315 |
if (value == null) { |
316 |
return 0; |
317 |
} |
318 |
return value.byteValue();
|
319 |
} |
320 |
|
321 |
public Date getDate(String name) { |
322 |
return this.getDate(this.data.getType().getIndex(name)); |
323 |
} |
324 |
|
325 |
public Date getDate(int index) { |
326 |
Date value = ((Date) this.get(index,Date.class,DataTypes.DATE)); |
327 |
|
328 |
return value;
|
329 |
} |
330 |
|
331 |
public double getDouble(String name) { |
332 |
return this.getDouble(this.data.getType().getIndex(name)); |
333 |
} |
334 |
|
335 |
public double getDouble(int index) { |
336 |
|
337 |
Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE)); |
338 |
if (value == null) { |
339 |
return 0; |
340 |
} |
341 |
return value.doubleValue();
|
342 |
} |
343 |
|
344 |
public Feature getFeature(String name) { |
345 |
return this.getFeature(this.data.getType().getIndex(name)); |
346 |
} |
347 |
|
348 |
public Feature getFeature(int index) { |
349 |
return (Feature) this.get(index); |
350 |
} |
351 |
|
352 |
public float getFloat(String name) { |
353 |
return this.getFloat(this.data.getType().getIndex(name)); |
354 |
} |
355 |
|
356 |
public float getFloat(int index) { |
357 |
Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT)); |
358 |
if (value == null) { |
359 |
return 0; |
360 |
} |
361 |
return value.floatValue();
|
362 |
} |
363 |
|
364 |
public Geometry getGeometry(String name) { |
365 |
return this.getGeometry(this.data.getType().getIndex(name)); |
366 |
} |
367 |
|
368 |
public Geometry getGeometry(int index) { |
369 |
return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY); |
370 |
} |
371 |
|
372 |
public int getInt(String name) { |
373 |
return this.getInt(this.data.getType().getIndex(name)); |
374 |
} |
375 |
|
376 |
public int getInt(int index) { |
377 |
Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT)); |
378 |
if (value == null) { |
379 |
return 0; |
380 |
} |
381 |
return ((Integer)value).intValue(); |
382 |
} |
383 |
|
384 |
public long getLong(String name) { |
385 |
return this.getLong(this.data.getType().getIndex(name)); |
386 |
} |
387 |
|
388 |
public long getLong(int index) { |
389 |
Long value = ((Long) this.get(index,Long.class,DataTypes.LONG)); |
390 |
if (value == null) { |
391 |
return 0; |
392 |
} |
393 |
return value.longValue();
|
394 |
} |
395 |
|
396 |
public String getString(String name) { |
397 |
return this.getString(this.data.getType().getIndex(name)); |
398 |
} |
399 |
|
400 |
public String getString(int index) { |
401 |
return (String) this.get(index,String.class,DataTypes.STRING); |
402 |
} |
403 |
|
404 |
public Object getContextValue(String name) { |
405 |
name = name.toLowerCase(); |
406 |
if (name.equals("store")) { |
407 |
return this.getStore(); |
408 |
} |
409 |
|
410 |
if (name.equals("featuretype")) { |
411 |
return this.data.getType(); |
412 |
} |
413 |
|
414 |
if (name.equals("feature")) { |
415 |
return this; |
416 |
} |
417 |
|
418 |
throw new IllegalArgumentException(name); |
419 |
} |
420 |
|
421 |
public Iterator getDataNames() { |
422 |
class DataNamesIterator implements Iterator { |
423 |
Iterator attributeIteraror;
|
424 |
|
425 |
DataNamesIterator(DefaultFeature feature) { |
426 |
this.attributeIteraror = feature.getType().iterator();
|
427 |
} |
428 |
|
429 |
public boolean hasNext() { |
430 |
return this.attributeIteraror.hasNext(); |
431 |
} |
432 |
|
433 |
public Object next() { |
434 |
return ((FeatureAttributeDescriptor) this.attributeIteraror |
435 |
.next()).getName(); |
436 |
} |
437 |
|
438 |
public void remove() { |
439 |
throw new UnsupportedOperationException(); |
440 |
} |
441 |
|
442 |
} |
443 |
return new DataNamesIterator(this); |
444 |
} |
445 |
|
446 |
public Object getDataValue(String name) { |
447 |
name = name.toLowerCase(); |
448 |
return get(name);
|
449 |
} |
450 |
|
451 |
public Iterator getDataValues() { |
452 |
class DataValuesIterator implements Iterator { |
453 |
DefaultFeature feature; |
454 |
int current = 0; |
455 |
|
456 |
DataValuesIterator(DefaultFeature feature) { |
457 |
this.feature = feature;
|
458 |
} |
459 |
|
460 |
public boolean hasNext() { |
461 |
return current < feature.getType().size() - 1; |
462 |
} |
463 |
|
464 |
public Object next() { |
465 |
return feature.get(current++);
|
466 |
} |
467 |
|
468 |
public void remove() { |
469 |
throw new UnsupportedOperationException(); |
470 |
} |
471 |
|
472 |
} |
473 |
return new DataValuesIterator(this); |
474 |
} |
475 |
|
476 |
public boolean hasContextValue(String name) { |
477 |
name = name.toLowerCase(); |
478 |
if (name.equals("store")) { |
479 |
return true; |
480 |
} |
481 |
|
482 |
if (name.equals("featuretype")) { |
483 |
return true; |
484 |
} |
485 |
|
486 |
if (name.equals("feature")) { |
487 |
return true; |
488 |
} |
489 |
return false; |
490 |
} |
491 |
|
492 |
public boolean hasDataValue(String name) { |
493 |
name = name.toLowerCase(); |
494 |
return this.data.getType().getIndex(name) >= 0; |
495 |
} |
496 |
|
497 |
public Instant getInstant(int index) { |
498 |
return ((Instant) this.get(index,Date.class,DataTypes.INSTANT)); |
499 |
} |
500 |
|
501 |
public Instant getInstant(String name) { |
502 |
return this.getInstant(this.data.getType().getIndex(name)); |
503 |
} |
504 |
|
505 |
public Interval getInterval(int index) { |
506 |
return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL)); |
507 |
} |
508 |
|
509 |
public Interval getInterval(String name) { |
510 |
return this.getInterval(this.data.getType().getIndex(name)); |
511 |
} |
512 |
|
513 |
public DynObject getAsDynObject() {
|
514 |
DynObjectFeatureFacade facade = new DynObjectFeatureFacade();
|
515 |
facade.setFeature(this);
|
516 |
return facade;
|
517 |
} |
518 |
|
519 |
public String toString() { |
520 |
StringBuffer buffer = new StringBuffer("Feature with values: "); |
521 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
522 |
getType().getAttributeDescriptors(); |
523 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
524 |
String name = attributeDescriptors[i].getName();
|
525 |
buffer.append(name).append("=").append(get(name));
|
526 |
if (i < attributeDescriptors.length - 1) { |
527 |
buffer.append(", ");
|
528 |
} |
529 |
} |
530 |
return super.toString(); |
531 |
} |
532 |
} |