Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 27992

History | View | Annotate | Download (10.5 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
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.spi.FeatureData;
19
import org.gvsig.fmap.geom.Geometry;
20
import org.gvsig.fmap.geom.primitive.Envelope;
21
import org.gvsig.tools.evaluator.Evaluator;
22
import org.gvsig.tools.evaluator.EvaluatorData;
23
import org.gvsig.tools.evaluator.EvaluatorException;
24

    
25
public class DefaultFeature implements Feature, EvaluatorData {
26

    
27
        protected FeatureData data;
28
        protected FeatureReference reference;
29
        private WeakReference storeRef;
30

    
31
        /*
32
         * Usar con mucha precaucion o mejor no usar. Lo precisa el
33
         * DefaultFeatureSet en la ordenacion.
34
         */
35
        public DefaultFeature(FeatureStore store) {
36
                this.storeRef = new WeakReference(store);
37
                this.reference = null;
38
        }
39

    
40
        public DefaultFeature(FeatureStore store, FeatureData data) {
41
                this.data = data;
42
                this.storeRef = new WeakReference(store);
43
                this.reference = null;
44
        }
45

    
46
        DefaultFeature(DefaultFeature feature) {
47
                this.data = feature.data.getCopy();
48
                this.storeRef = feature.storeRef;
49
                this.reference = feature.reference;
50
        }
51

    
52
        public void setData(FeatureData data) {
53
                this.data = data;
54
                this.reference = null;
55
        }
56

    
57
        public FeatureData getData() {
58
                return this.data;
59
        }
60

    
61
        void set(FeatureAttributeDescriptor attribute, Object value) {
62
                int i = attribute.getIndex();
63

    
64
                if (attribute.isReadOnly()) {
65
                        throw new IllegalArgumentException();
66
                }
67

    
68
                if (attribute.getEvaluator() != null) {
69
                        throw new IllegalArgumentException();
70
                }
71

    
72
                if (value == null) {
73
                        if (!attribute.allowNull()) {
74
                                throw new IllegalArgumentException();
75
                        }
76
                        this.data.set(i, null);
77
                        return;
78

    
79
                }
80

    
81
                if (attribute.getObjectClass().isInstance(value)) {
82
                        this.data.set(i, value);
83
                        return;
84
                }
85

    
86
                if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass()))
87
                                || (attribute.getDataType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) {
88
                        Object number=getNumberByType((Number)value, attribute.getDataType());
89
                        this.data.set(i, number);
90
                        return;
91
                }
92

    
93
                if (!(value instanceof String)) {
94
                        throw new ClassCastException();
95
                }
96
                int dataType = attribute.getDataType();
97

    
98
                switch (dataType) {
99
                case DataTypes.BYTE:
100
                        this.data.set(i, Byte.valueOf((String) value));
101
                        break;
102

    
103
                case DataTypes.DATE:
104
                        try {
105
                                this.data.set(i, attribute.getDateFormat()
106
                                                .parse((String) value));
107
                        } catch (ParseException e) {
108
                                throw new ClassCastException();
109
                        }
110
                        break;
111

    
112
                case DataTypes.DOUBLE:
113
                        this.data.set(i, Double.valueOf((String) value));
114
                        break;
115

    
116
                case DataTypes.FLOAT:
117
                        this.data.set(i, Float.valueOf((String) value));
118
                        break;
119

    
120
                case DataTypes.INT:
121
                        this.data.set(i, Integer.valueOf((String) value));
122
                        break;
123

    
124
                case DataTypes.LONG:
125
                        this.data.set(i, Long.valueOf((String) value));
126
                        break;
127

    
128
                default:
129
                        throw new ClassCastException();
130
                }
131
        }
132

    
133
        private Object getNumberByType(Number value, int type) {
134
                if (type==DataTypes.DOUBLE){
135
                        return new Double(value.doubleValue());
136
                }else if (type==DataTypes.FLOAT){
137
                        return new Float(value.floatValue());
138
                }else if (type==DataTypes.LONG){
139
                        return new Long(value.longValue());
140
                }else if (type==DataTypes.INT){
141
                        return new Integer(value.intValue());
142
                }else if (type==DataTypes.STRING){
143
                        return value.toString();
144
                }
145
                return value;
146
        }
147

    
148
        public void initializeValues() {
149
                FeatureType type = this.getType();
150
                Iterator iterator = type.iterator();
151

    
152
                while (iterator.hasNext()) {
153
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
154
                        .next();
155
//                        if (attribute.getDefaultValue()!=null)
156
                                this.set(attribute, attribute.getDefaultValue());
157
                }
158
        }
159

    
160
        public void initializeValues(Feature feature) {
161
                FeatureType myType=this.getType();
162
                FeatureType type =feature.getType();
163
                Iterator iterator = type.iterator();
164

    
165
                while (iterator.hasNext()) {
166
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
167
                        .next();
168
                        FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
169
                        if (myAttribute != null) {
170
                                this.set(myAttribute, feature.get(attribute.getIndex()));
171
                        }
172
                }
173
        }
174

    
175
        public FeatureStore getStore() {
176
                return (FeatureStore) this.storeRef.get();
177
        }
178

    
179
        public FeatureType getType() {
180
                return this.data.getType();
181
        }
182

    
183
        public EditableFeature getEditable() {
184
                return new DefaultEditableFeature(this);
185
        }
186

    
187
        public Feature getCopy() {
188
                return new DefaultFeature(this);
189
        }
190

    
191
        public FeatureReference getReference() {
192
                if (this.reference == null) {
193
                        this.reference = new DefaultFeatureReference(this);
194
                }
195
                return this.reference;
196
        }
197

    
198
        public void validate(int mode) {
199
                ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
200
        }
201

    
202
        public List getSRSs() {
203
                // TODO Auto-generated method stub
204
                return null;
205
        }
206

    
207
        public Envelope getDefaultEnvelope() {
208
                return this.data.getDefaultEnvelope();
209
        }
210

    
211
        public Geometry getDefaultGeometry() {
212
                return this.data.getDefaultGeometry();
213
        }
214

    
215
        public IProjection getDefaultSRS() {
216
                return this.data.getType().getDefaultSRS();
217
        }
218

    
219
        public List getGeometries() {
220
                // TODO Auto-generated method stub
221
                return null;
222
        }
223

    
224
        public Object get(String name) {
225
                return this.get(this.data.getType().getIndex(name));
226
        }
227

    
228
        public Object get(int index) {
229
                if (!this.data.getType().hasEvaluators()) {
230
                        return this.data.get(index);
231
                }
232
                FeatureAttributeDescriptor attribute = this.data.getType()
233
                .getAttributeDescriptor(index);
234
                Evaluator eval = attribute.getEvaluator();
235
                if (eval == null) {
236
                        return this.data.get(index);
237
                } else {
238
                        Object value = this.data.get(index);
239
                        if (value != null) { // FIXME: para comprobar si esta calculado usar
240
                                                                        // un array
241
                                                                        // especifico.
242
                                return this.data.get(index);
243
                        }
244
                        try {
245
                                value = eval.evaluate(this);
246
                        } catch (EvaluatorException e) {
247
                                throw new DataEvaluatorRuntimeException(e);
248
                        }
249
                        this.data.set(index, value);
250
                        return value;
251
                }
252
        }
253

    
254
        public Object[] getArray(String name) {
255
                return this.getArray(this.data.getType().getIndex(name));
256
        }
257

    
258
        public Object[] getArray(int index) {
259
                return (Object[]) this.get(index);
260
        }
261

    
262
        public boolean getBoolean(String name) {
263
                return this.getBoolean(this.data.getType().getIndex(name));
264
        }
265

    
266
        public boolean getBoolean(int index) {
267
                Boolean value = (Boolean) this.get(index);
268
                if (value == null) {
269
                        return false;
270
                }
271
                return value.booleanValue();
272
        }
273

    
274
        public byte getByte(String name) {
275
                return this.getByte(this.data.getType().getIndex(name));
276
        }
277

    
278
        public byte getByte(int index) {
279
                Byte value = (Byte) this.get(index);
280
                if (value == null) {
281
                        return 0;
282
                }
283
                return value.byteValue();
284
        }
285

    
286
        public Date getDate(String name) {
287
                return this.getDate(this.data.getType().getIndex(name));
288
        }
289

    
290
        public Date getDate(int index) {
291
                return (Date) this.get(index);
292
        }
293

    
294
        public double getDouble(String name) {
295
                return this.getDouble(this.data.getType().getIndex(name));
296
        }
297

    
298
        public double getDouble(int index) {
299
                Double value = (Double) this.get(index);
300
                if (value == null) {
301
                        return 0;
302
                }
303
                return value.doubleValue();
304
        }
305

    
306
        public Feature getFeature(String name) {
307
                return this.getFeature(this.data.getType().getIndex(name));
308
        }
309

    
310
        public Feature getFeature(int index) {
311
                return (Feature) this.get(index);
312
        }
313

    
314
        public float getFloat(String name) {
315
                return this.getFloat(this.data.getType().getIndex(name));
316
        }
317

    
318
        public float getFloat(int index) {
319
                Float value = (Float) this.get(index);
320
                if (value == null) {
321
                        return 0;
322
                }
323
                return value.floatValue();
324
        }
325

    
326
        public Geometry getGeometry(String name) {
327
                return this.getGeometry(this.data.getType().getIndex(name));
328
        }
329

    
330
        public Geometry getGeometry(int index) {
331
                return (Geometry) this.get(index);
332
        }
333

    
334
        public int getInt(String name) {
335
                return this.getInt(this.data.getType().getIndex(name));
336
        }
337

    
338
        public int getInt(int index) {
339
                Integer value = (Integer) this.get(index);
340
                if (value == null) {
341
                        return 0;
342
                }
343
                return value.intValue();
344
        }
345

    
346
        public long getLong(String name) {
347
                return this.getLong(this.data.getType().getIndex(name));
348
        }
349

    
350
        public long getLong(int index) {
351
                Long value = (Long) this.get(index);
352
                if (value == null) {
353
                        return 0;
354
                }
355
                return value.longValue();
356
        }
357

    
358
        public String getString(String name) {
359
                return this.getString(this.data.getType().getIndex(name));
360
        }
361

    
362
        public String getString(int index) {
363
                return (String) this.get(index);
364
        }
365

    
366
        public Object getContextValue(String name) {
367
                name = name.toLowerCase();
368
                if (name.equals("store")) {
369
                        return this.getStore();
370
                }
371

    
372
                if (name.equals("featuretype")) {
373
                        return this.data.getType();
374
                }
375

    
376
                if (name.equals("feature")) {
377
                        return this;
378
                }
379

    
380
                throw new IllegalArgumentException(name);
381
        }
382

    
383
        public Iterator getDataNames() {
384
                class DataNamesIterator implements Iterator {
385
                        Iterator attributeIteraror;
386

    
387
                        DataNamesIterator(DefaultFeature feature) {
388
                                this.attributeIteraror = feature.getType().iterator();
389
                        }
390

    
391
                        public boolean hasNext() {
392
                                return this.attributeIteraror.hasNext();
393
                        }
394

    
395
                        public Object next() {
396
                                return ((FeatureAttributeDescriptor) this.attributeIteraror
397
                                                .next()).getName();
398
                        }
399

    
400
                        public void remove() {
401
                                throw new UnsupportedOperationException();
402
                        }
403

    
404
                }
405
                return new DataNamesIterator(this);
406
        }
407

    
408
        public Object getDataValue(String name) {
409
                name = name.toLowerCase();
410
                return get(name);
411
        }
412

    
413
        public Iterator getDataValues() {
414
                class DataValuesIterator implements Iterator {
415
                        DefaultFeature feature;
416
                        int current = 0;
417

    
418
                        DataValuesIterator(DefaultFeature feature) {
419
                                this.feature = feature;
420
                        }
421

    
422
                        public boolean hasNext() {
423
                                return current < feature.getType().size() - 1;
424
                        }
425

    
426
                        public Object next() {
427
                                return feature.get(current++);
428
                        }
429

    
430
                        public void remove() {
431
                                throw new UnsupportedOperationException();
432
                        }
433

    
434
                }
435
                return new DataValuesIterator(this);
436
        }
437

    
438
        public boolean hasContextValue(String name) {
439
                name = name.toLowerCase();
440
                if (name.equals("store")) {
441
                        return true;
442
                }
443

    
444
                if (name.equals("featuretype")) {
445
                        return true;
446
                }
447

    
448
                if (name.equals("feature")) {
449
                        return true;
450
                }
451
                return false;
452
        }
453

    
454
        public boolean hasDataValue(String name) {
455
                name = name.toLowerCase();
456
                return this.data.getType().getIndex(name) >= 0;
457
        }
458

    
459
}