Revision 37297 branches/v2_0_0_prep/libraries/libFMap_dal/src/org/gvsig/fmap/dal/feature/impl/DefaultFeature.java

View differences:

DefaultFeature.java
21 21
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
22 22
import org.gvsig.fmap.geom.Geometry;
23 23
import org.gvsig.fmap.geom.primitive.Envelope;
24
import org.gvsig.timesupport.Instant;
25
import org.gvsig.timesupport.Interval;
24 26
import org.gvsig.tools.ToolsLocator;
25 27
import org.gvsig.tools.dataTypes.CoercionException;
26 28
import org.gvsig.tools.dataTypes.DataTypesManager;
......
94 96
			this.data.set(i, null);
95 97
			return;
96 98

  
97
		}
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
        }
98 109

  
99 110
        if (!attribute.getObjectClass().isInstance(value)) {
100 111
            try {
......
111 122
		}
112 123
		
113 124
        this.data.set(i, value);
114
	}
115

  
116
	private Object get(int index,Class theClass, int type) {
117
		Object value = this.get(index);
118
		if( theClass.isInstance(value) ) {
119
			return value;
120
		}
121
		try {
122
			return this.getDataTypesManager().coerce(type, value);
123
		} catch (CoercionException e) {
124
			throw new IllegalArgumentException(
125
					"Can't convert to "+theClass.getSimpleName()+" from '"+value.getClass().getName()+"' with value '"+value.toString()+"'.",
126
					e);
127
		}
128
	}
125
    }
129 126
	
130
//	private Object getNumberByType(Number value, int type) {
131
//		if (type==DataTypes.DOUBLE){
132
//			return new Double(value.doubleValue());
133
//		}else if (type==DataTypes.FLOAT){
134
//			return new Float(value.floatValue());
135
//		}else if (type==DataTypes.LONG){
136
//			return new Long(value.longValue());
137
//		}else if (type==DataTypes.INT){
138
//			return new Integer(value.intValue());
139
//		}else if (type==DataTypes.STRING){
140
//			return value.toString();
141
//		}
142
//		return value;
143
//	}
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
//  }
144 155

  
145
	public void initializeValues() {
146
		FeatureType type = this.getType();
147
		Iterator iterator = type.iterator();
156
    public void initializeValues() {
157
        FeatureType type = this.getType();
158
        Iterator iterator = type.iterator();
148 159

  
149
		while (iterator.hasNext()) {
150
			FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
151
			.next();
152
			if (attribute.isAutomatic() || attribute.isReadOnly()
153
					|| attribute.getEvaluator() != null) {
154
				continue;
155
			}
156
			if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
157
				continue;
158
			}
159
			this.set(attribute, attribute.getDefaultValue());
160
		}
161
	}
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
    }
162 173

  
163
	public void clear() {
164
		initializeValues();
165
	}
174
    public void clear() {
175
        initializeValues();
176
    }
166 177

  
167
	public void initializeValues(Feature feature) {
168
		FeatureType myType=this.getType();
169
		FeatureType type =feature.getType();
170
		Iterator iterator = type.iterator();
178
    public void initializeValues(Feature feature) {
179
        FeatureType myType=this.getType();
180
        FeatureType type =feature.getType();
181
        Iterator iterator = type.iterator();
171 182

  
172
		while (iterator.hasNext()) {
173
			FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
174
			.next();
175
			FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
176
			if (myAttribute != null) {
177
				this.set(myAttribute, feature.get(attribute.getIndex()));
178
			}
179
		}
180
	}
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
    }
181 192

  
182
	public FeatureStore getStore() {
183
		return (FeatureStore) this.storeRef.get();
184
	}
185 193

  
186
	public FeatureType getType() {
187
		return this.data.getType();
188
	}
194
    public FeatureStore getStore() {
195
        return (FeatureStore) this.storeRef.get();
196
    }
189 197

  
190
	public EditableFeature getEditable() {
191
		return new DefaultEditableFeature(this);
192
	}
198
    public FeatureType getType() {
199
        return this.data.getType();
200
    }
193 201

  
194
	public Feature getCopy() {
195
		return new DefaultFeature(this);
196
	}
202
    public EditableFeature getEditable() {
203
        return new DefaultEditableFeature(this);
204
    }
197 205

  
198
	public FeatureReference getReference() {
199
		if (this.reference == null) {
200
			this.reference = new DefaultFeatureReference(this);
201
		}
202
		return this.reference;
203
	}
206
    public Feature getCopy() {
207
        return new DefaultFeature(this);
208
    }
204 209

  
205
	public void validate(int mode) {
206
		((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
207
	}
210
    public FeatureReference getReference() {
211
        if (this.reference == null) {
212
            this.reference = new DefaultFeatureReference(this);
213
        }
214
        return this.reference;
215
    }
208 216

  
209
	public List getSRSs() {
210
		// TODO Auto-generated method stub
211
		return null;
212
	}
217
    public void validate(int mode) {
218
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
219
    }
213 220

  
214
	public Envelope getDefaultEnvelope() {
215
		return this.data.getDefaultEnvelope();
216
	}
221
    public List getSRSs() {
222
        // TODO Auto-generated method stub
223
        return null;
224
    }
217 225

  
218
	public Geometry getDefaultGeometry() {
219
		return this.data.getDefaultGeometry();
220
	}
226
    public Envelope getDefaultEnvelope() {
227
        return this.data.getDefaultEnvelope();
228
    }
221 229

  
222
	public IProjection getDefaultSRS() {
223
		return this.data.getType().getDefaultSRS();
224
	}
230
    public Geometry getDefaultGeometry() {
231
        return this.data.getDefaultGeometry();
232
    }
225 233

  
226
	public List getGeometries() {
227
		// TODO Auto-generated method stub
228
		return null;
229
	}
234
    public IProjection getDefaultSRS() {
235
        return this.data.getType().getDefaultSRS();
236
    }
230 237

  
231
	public Object get(String name) {
232
		int index = this.data.getType().getIndex(name);
233
		if( index < 0 ) {
234
			throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
235
		}
236
		return this.get(index);
237
	}
238
    public List getGeometries() {
239
        // TODO Auto-generated method stub
240
        return null;
241
    }
238 242

  
239
	public Object get(int index) {
240
		FeatureType type = this.data.getType();
241
		if( index <0 || index >= type.size() ) {
242
			throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
243
		}
244
		if (!this.data.getType().hasEvaluators()) {
245
			return this.data.get(index);
246
		}
247
		FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
248
		Evaluator eval = attribute.getEvaluator();
249
		if (eval == null) {
250
			return this.data.get(index);
251
		} else {
252
			Object value = this.data.get(index);
253
			if (value != null) { // FIXME: para comprobar si esta calculado usar
254
									// un array
255
									// especifico.
256
				return this.data.get(index);
257
			}
258
			try {
259
				value = eval.evaluate(this);
260
			} catch (EvaluatorException e) {
261
				throw new DataEvaluatorRuntimeException(e);
262
			}
263
			this.data.set(index, value);
264
			return value;
265
		}
266
	}
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
    }
267 250

  
268
	public Object[] getArray(String name) {
269
		return this.getArray(this.data.getType().getIndex(name));
270
	}
271 251

  
272
	public Object[] getArray(int index) {
273
		return (Object[]) this.get(index);
274
	}
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
    }
275 280

  
276
	public boolean getBoolean(String name) {
277
		return this.getBoolean(this.data.getType().getIndex(name));
278
	}
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
    }
279 288

  
280
	public boolean getBoolean(int index) {
281
		Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
282
		if (value == null) {
283
			return false;
284
		}
285
		return value.booleanValue();
286
	}
289
    public Object[] getArray(String name) {
290
        return this.getArray(this.data.getType().getIndex(name));
291
    }
287 292

  
288
	public byte getByte(String name) {
289
		return this.getByte(this.data.getType().getIndex(name));
290
	}
293
    public Object[] getArray(int index) {
294
        return (Object[]) this.get(index);
295
    }
291 296

  
292
	public byte getByte(int index) {
293
		Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
294
		if (value == null) {
295
			return 0;
296
		}
297
		return value.byteValue();
298
	}
297
    public boolean getBoolean(String name) {
298
        return this.getBoolean(this.data.getType().getIndex(name));
299
    }
299 300

  
300
	public Date getDate(String name) {
301
		return this.getDate(this.data.getType().getIndex(name));
302
	}
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
    }
303 308

  
304
	public Date getDate(int index) {
305
		Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
309
    public byte getByte(String name) {
310
        return this.getByte(this.data.getType().getIndex(name));
311
    }
306 312

  
307
		return value;
308
	}
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
    }
309 320

  
310
	public double getDouble(String name) {
311
		return this.getDouble(this.data.getType().getIndex(name));
312
	}
321
    public Date getDate(String name) {
322
        return this.getDate(this.data.getType().getIndex(name));
323
    }
313 324

  
314
	public double getDouble(int index) {
315
		
316
		Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
317
		if (value == null) {
318
			return 0;
319
		}
320
		return value.doubleValue();
321
	}
325
    public Date getDate(int index) {
326
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
322 327

  
323
	public Feature getFeature(String name) {
324
		return this.getFeature(this.data.getType().getIndex(name));
325
	}
328
        return value;
329
    }
326 330

  
327
	public Feature getFeature(int index) {
328
		return (Feature) this.get(index);
329
	}
331
    public double getDouble(String name) {
332
        return this.getDouble(this.data.getType().getIndex(name));
333
    }
330 334

  
331
	public float getFloat(String name) {
332
		return this.getFloat(this.data.getType().getIndex(name));
333
	}
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
    }
334 343

  
335
	public float getFloat(int index) {
336
		Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
337
		if (value == null) {
338
			return 0;
339
		}
340
		return value.floatValue();
341
	}
344
    public Feature getFeature(String name) {
345
        return this.getFeature(this.data.getType().getIndex(name));
346
    }
342 347

  
343
	public Geometry getGeometry(String name) {
344
		return this.getGeometry(this.data.getType().getIndex(name));
345
	}
348
    public Feature getFeature(int index) {
349
        return (Feature) this.get(index);
350
    }
346 351

  
347
	public Geometry getGeometry(int index) {
348
		return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
349
	}
352
    public float getFloat(String name) {
353
        return this.getFloat(this.data.getType().getIndex(name));
354
    }
350 355

  
351
	public int getInt(String name) {
352
		return this.getInt(this.data.getType().getIndex(name));
353
	}
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
    }
354 363

  
355
	public int getInt(int index) {
356
		Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
357
		if (value == null) {
358
			return 0;
359
		}
360
		return ((Integer)value).intValue();
361
	}
364
    public Geometry getGeometry(String name) {
365
        return this.getGeometry(this.data.getType().getIndex(name));
366
    }
362 367

  
363
	public long getLong(String name) {
364
		return this.getLong(this.data.getType().getIndex(name));
365
	}
368
    public Geometry getGeometry(int index) {
369
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
370
    }
366 371

  
367
	public long getLong(int index) {
368
		Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
369
		if (value == null) {
370
			return 0;
371
		}
372
		return value.longValue();
373
	}
372
    public int getInt(String name) {
373
        return this.getInt(this.data.getType().getIndex(name));
374
    }
374 375

  
375
	public String getString(String name) {
376
		return this.getString(this.data.getType().getIndex(name));
377
	}
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
    }
378 383

  
379
	public String getString(int index) {
380
		return (String) this.get(index,String.class,DataTypes.STRING);
381
	}
384
    public long getLong(String name) {
385
        return this.getLong(this.data.getType().getIndex(name));
386
    }
382 387

  
383
	public Object getContextValue(String name) {
384
		name = name.toLowerCase();
385
		if (name.equals("store")) {
386
			return this.getStore();
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
    }
388 395

  
389
		if (name.equals("featuretype")) {
390
			return this.data.getType();
391
		}
396
    public String getString(String name) {
397
        return this.getString(this.data.getType().getIndex(name));
398
    }
392 399

  
393
		if (name.equals("feature")) {
394
			return this;
395
		}
400
    public String getString(int index) {
401
        return (String) this.get(index,String.class,DataTypes.STRING);
402
    }
396 403

  
397
		throw new IllegalArgumentException(name);
398
	}
404
    public Object getContextValue(String name) {
405
        name = name.toLowerCase();
406
        if (name.equals("store")) {
407
            return this.getStore();
408
        }
399 409

  
400
	public Iterator getDataNames() {
401
		class DataNamesIterator implements Iterator {
402
			Iterator attributeIteraror;
410
        if (name.equals("featuretype")) {
411
            return this.data.getType();
412
        }
403 413

  
404
			DataNamesIterator(DefaultFeature feature) {
405
				this.attributeIteraror = feature.getType().iterator();
406
			}
414
        if (name.equals("feature")) {
415
            return this;
416
        }
407 417

  
408
			public boolean hasNext() {
409
				return this.attributeIteraror.hasNext();
410
			}
418
        throw new IllegalArgumentException(name);
419
    }
411 420

  
412
			public Object next() {
413
				return ((FeatureAttributeDescriptor) this.attributeIteraror
414
						.next()).getName();
415
			}
421
    public Iterator getDataNames() {
422
        class DataNamesIterator implements Iterator {
423
            Iterator attributeIteraror;
416 424

  
417
			public void remove() {
418
				throw new UnsupportedOperationException();
419
			}
425
            DataNamesIterator(DefaultFeature feature) {
426
                this.attributeIteraror = feature.getType().iterator();
427
            }
420 428

  
421
		}
422
		return new DataNamesIterator(this);
423
	}
429
            public boolean hasNext() {
430
                return this.attributeIteraror.hasNext();
431
            }
424 432

  
425
	public Object getDataValue(String name) {
426
		name = name.toLowerCase();
427
		return get(name);
428
	}
433
            public Object next() {
434
                return ((FeatureAttributeDescriptor) this.attributeIteraror
435
                        .next()).getName();
436
            }
429 437

  
430
	public Iterator getDataValues() {
431
		class DataValuesIterator implements Iterator {
432
			DefaultFeature feature;
433
			int current = 0;
438
            public void remove() {
439
                throw new UnsupportedOperationException();
440
            }
434 441

  
435
			DataValuesIterator(DefaultFeature feature) {
436
				this.feature = feature;
437
			}
442
        }
443
        return new DataNamesIterator(this);
444
    }
438 445

  
439
			public boolean hasNext() {
440
				return current < feature.getType().size() - 1;
441
			}
446
    public Object getDataValue(String name) {
447
        name = name.toLowerCase();
448
        return get(name);
449
    }
442 450

  
443
			public Object next() {
444
				return feature.get(current++);
445
			}
451
    public Iterator getDataValues() {
452
        class DataValuesIterator implements Iterator {
453
            DefaultFeature feature;
454
            int current = 0;
446 455

  
447
			public void remove() {
448
				throw new UnsupportedOperationException();
449
			}
456
            DataValuesIterator(DefaultFeature feature) {
457
                this.feature = feature;
458
            }
450 459

  
451
		}
452
		return new DataValuesIterator(this);
453
	}
460
            public boolean hasNext() {
461
                return current < feature.getType().size() - 1;
462
            }
454 463

  
455
	public boolean hasContextValue(String name) {
456
		name = name.toLowerCase();
457
		if (name.equals("store")) {
458
			return true;
459
		}
464
            public Object next() {
465
                return feature.get(current++);
466
            }
460 467

  
461
		if (name.equals("featuretype")) {
462
			return true;
463
		}
468
            public void remove() {
469
                throw new UnsupportedOperationException();
470
            }
464 471

  
465
		if (name.equals("feature")) {
466
			return true;
467
		}
468
		return false;
469
	}
472
        }
473
        return new DataValuesIterator(this);
474
    }
470 475

  
471
	public boolean hasDataValue(String name) {
472
		name = name.toLowerCase();
473
		return this.data.getType().getIndex(name) >= 0;
474
	}
476
    public boolean hasContextValue(String name) {
477
        name = name.toLowerCase();
478
        if (name.equals("store")) {
479
            return true;
480
        }
475 481

  
476
	public DynObject getAsDynObject() {
477
		DynObjectFeatureFacade facade = new DynObjectFeatureFacade();
478
		facade.setFeature(this);
479
		return facade;
480
	}
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
    }
481 518
}

Also available in: Unified diff