Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 25230

History | View | Annotate | Download (9.68 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.gvsig.fmap.dal.DataTypes;
10
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
11
import org.gvsig.fmap.dal.feature.EditableFeature;
12
import org.gvsig.fmap.dal.feature.Feature;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.FeatureReference;
15
import org.gvsig.fmap.dal.feature.FeatureStore;
16
import org.gvsig.fmap.dal.feature.FeatureType;
17
import org.gvsig.fmap.dal.feature.spi.FeatureData;
18
import org.gvsig.fmap.geom.Geometry;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20
import org.gvsig.tools.evaluator.Evaluator;
21
import org.gvsig.tools.evaluator.EvaluatorData;
22
import org.gvsig.tools.evaluator.EvaluatorException;
23

    
24
public class DefaultFeature implements Feature, EvaluatorData {
25

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

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

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

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

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

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

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

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

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

    
71
                if (!attribute.allowNull() && value == null) {
72
                        throw new IllegalArgumentException();
73
                }
74

    
75
                if (attribute.getObjectClass().isInstance(value)) {
76
                        this.data.set(i, value);
77
                        if (((DefaultFeatureType) this.data.getType())
78
                                        .getDefaultGeometryAttributeIndex() == i) {
79
                                this.data.setDefaultGeometry((Geometry) value);
80
                                this.data.setDefaultEnvelope(((Geometry) value).getEnvelope());
81
                        }
82
                        return;
83
                }
84
                if (!(value instanceof String)) {
85
                        throw new ClassCastException();
86
                }
87

    
88
                int dataType = attribute.getDataType();
89

    
90
                switch (dataType) {
91
                case DataTypes.BYTE:
92
                        this.data.set(i, Byte.valueOf((String) value));
93
                        break;
94

    
95
                case DataTypes.DATE:
96
                        try {
97
                                this.data.set(i, attribute.getDateFormat()
98
                                                .parse((String) value));
99
                        } catch (ParseException e) {
100
                                throw new ClassCastException();
101
                        }
102
                        break;
103

    
104
                case DataTypes.DOUBLE:
105
                        this.data.set(i, Double.valueOf((String) value));
106
                        break;
107

    
108
                case DataTypes.FLOAT:
109
                        this.data.set(i, Float.valueOf((String) value));
110
                        break;
111

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

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

    
120
                default:
121
                        throw new ClassCastException();
122
                }
123
        }
124

    
125
        public void initializeValues() {
126
                FeatureType type = this.getType();
127
                Iterator iterator = type.iterator();
128

    
129
                while (iterator.hasNext()) {
130
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
131
                        .next();
132
                        this.set(attribute, attribute.getDefaultValue());
133
                }
134
        }
135

    
136
        public void initializeValues(Feature feature) {
137
                FeatureType type = this.getType();
138
                Iterator iterator = type.iterator();
139

    
140
                while (iterator.hasNext()) {
141
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
142
                        .next();
143
                        this.set(attribute, feature.get(attribute.getName()));
144
                }
145
        }
146

    
147
        public FeatureStore getStore() {
148
                return (FeatureStore) this.storeRef.get();
149
        }
150

    
151
        public FeatureType getType() {
152
                return this.data.getType();
153
        }
154

    
155
        public EditableFeature getEditable() {
156
                return new DefaultEditableFeature(this);
157
        }
158

    
159
        public Feature getCopy() {
160
                return new DefaultFeature(this);
161
        }
162

    
163
        public FeatureReference getReference() {
164
                if (this.reference == null) {
165
                        this.reference = new DefaultFeatureReference(this);
166
                }
167
                return this.reference;
168
        }
169

    
170
        public void validate(int mode) {
171
                ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
172
        }
173

    
174
        public List getSRSs() {
175
                // TODO Auto-generated method stub
176
                return null;
177
        }
178

    
179
        public Envelope getDefaultEnvelope() {
180
                return this.data.getDefaultEnvelope();
181
        }
182

    
183
        public Geometry getDefaultGeometry() {
184
                return this.data.getDefaultGeometry();
185
        }
186

    
187
        public String getDefaultSRS() {
188
                // TODO Auto-generated method stub
189
                return null;
190
        }
191

    
192
        public List getGeometries() {
193
                // TODO Auto-generated method stub
194
                return null;
195
        }
196

    
197
        public Object get(String name) {
198
                return this.get(this.data.getType().getIndex(name));
199
        }
200

    
201
        public Object get(int index) {
202
                if (!this.data.getType().hasEvaluators()) {
203
                        return this.data.get(index);
204
                }
205
                FeatureAttributeDescriptor attribute = this.data.getType()
206
                .getAttributeDescriptor(index);
207
                Evaluator eval = attribute.getEvaluator();
208
                if (eval == null) {
209
                        return this.data.get(index);
210
                } else {
211
                        Object value = this.data.get(index);
212
                        if (value != null) { // FIXME: para comprobar si esta calculado usar
213
                                                                        // un array
214
                                                                        // especifico.
215
                                return this.data.get(index);
216
                        }
217
                        try {
218
                                value = eval.evaluate(this);
219
                        } catch (EvaluatorException e) {
220
                                throw new DataEvaluatorRuntimeException(e);
221
                        }
222
                        this.data.set(index, value);
223
                        return value;
224
                }
225
        }
226

    
227
        public Object[] getArray(String name) {
228
                return this.getArray(this.data.getType().getIndex(name));
229
        }
230

    
231
        public Object[] getArray(int index) {
232
                return (Object[]) this.get(index);
233
        }
234

    
235
        public boolean getBoolean(String name) {
236
                return this.getBoolean(this.data.getType().getIndex(name));
237
        }
238

    
239
        public boolean getBoolean(int index) {
240
                Boolean value = (Boolean) this.get(index);
241
                if (value == null) {
242
                        return false;
243
                }
244
                return value.booleanValue();
245
        }
246

    
247
        public byte getByte(String name) {
248
                return this.getByte(this.data.getType().getIndex(name));
249
        }
250

    
251
        public byte getByte(int index) {
252
                Byte value = (Byte) this.get(index);
253
                if (value == null) {
254
                        return 0;
255
                }
256
                return value.byteValue();
257
        }
258

    
259
        public Date getDate(String name) {
260
                return this.getDate(this.data.getType().getIndex(name));
261
        }
262

    
263
        public Date getDate(int index) {
264
                return (Date) this.get(index);
265
        }
266

    
267
        public double getDouble(String name) {
268
                return this.getDouble(this.data.getType().getIndex(name));
269
        }
270

    
271
        public double getDouble(int index) {
272
                Double value = (Double) this.get(index);
273
                if (value == null) {
274
                        return 0;
275
                }
276
                return value.doubleValue();
277
        }
278

    
279
        public Feature getFeature(String name) {
280
                return this.getFeature(this.data.getType().getIndex(name));
281
        }
282

    
283
        public Feature getFeature(int index) {
284
                return (Feature) this.get(index);
285
        }
286

    
287
        public float getFloat(String name) {
288
                return this.getFloat(this.data.getType().getIndex(name));
289
        }
290

    
291
        public float getFloat(int index) {
292
                Float value = (Float) this.get(index);
293
                if (value == null) {
294
                        return 0;
295
                }
296
                return value.floatValue();
297
        }
298

    
299
        public Geometry getGeometry(String name) {
300
                return this.getGeometry(this.data.getType().getIndex(name));
301
        }
302

    
303
        public Geometry getGeometry(int index) {
304
                return (Geometry) this.get(index);
305
        }
306

    
307
        public int getInt(String name) {
308
                return this.getInt(this.data.getType().getIndex(name));
309
        }
310

    
311
        public int getInt(int index) {
312
                Integer value = (Integer) this.get(index);
313
                if (value == null) {
314
                        return 0;
315
                }
316
                return value.intValue();
317
        }
318

    
319
        public long getLong(String name) {
320
                return this.getLong(this.data.getType().getIndex(name));
321
        }
322

    
323
        public long getLong(int index) {
324
                Long value = (Long) this.get(index);
325
                if (value == null) {
326
                        return 0;
327
                }
328
                return value.longValue();
329
        }
330

    
331
        public String getString(String name) {
332
                return this.getString(this.data.getType().getIndex(name));
333
        }
334

    
335
        public String getString(int index) {
336
                return (String) this.get(index);
337
        }
338

    
339
        public Object getContextValue(String name) {
340
                name = name.toLowerCase();
341
                if (name.equals("store")) {
342
                        return this.getStore();
343
                }
344

    
345
                if (name.equals("featuretype")) {
346
                        return this.data.getType();
347
                }
348

    
349
                if (name.equals("feature")) {
350
                        return this;
351
                }
352

    
353
                throw new IllegalArgumentException(name);
354
        }
355

    
356
        public Iterator getDataNames() {
357
                class DataNamesIterator implements Iterator {
358
                        Iterator attributeIteraror;
359

    
360
                        DataNamesIterator(DefaultFeature feature) {
361
                                this.attributeIteraror = feature.getType().iterator();
362
                        }
363

    
364
                        public boolean hasNext() {
365
                                return this.attributeIteraror.hasNext();
366
                        }
367

    
368
                        public Object next() {
369
                                return ((FeatureAttributeDescriptor) this.attributeIteraror
370
                                                .next()).getName();
371
                        }
372

    
373
                        public void remove() {
374
                                throw new UnsupportedOperationException();
375
                        }
376

    
377
                }
378
                return new DataNamesIterator(this);
379
        }
380

    
381
        public Object getDataValue(String name) {
382
                name = name.toLowerCase();
383
                return get(name);
384
        }
385

    
386
        public Iterator getDataValues() {
387
                class DataValuesIterator implements Iterator {
388
                        DefaultFeature feature;
389
                        int current = 0;
390

    
391
                        DataValuesIterator(DefaultFeature feature) {
392
                                this.feature = feature;
393
                        }
394

    
395
                        public boolean hasNext() {
396
                                return current < feature.getType().size() - 1;
397
                        }
398

    
399
                        public Object next() {
400
                                return feature.get(current++);
401
                        }
402

    
403
                        public void remove() {
404
                                throw new UnsupportedOperationException();
405
                        }
406

    
407
                }
408
                return new DataValuesIterator(this);
409
        }
410

    
411
        public boolean hasContextValue(String name) {
412
                name = name.toLowerCase();
413
                if (name.equals("store")) {
414
                        return true;
415
                }
416

    
417
                if (name.equals("featuretype")) {
418
                        return true;
419
                }
420

    
421
                if (name.equals("feature")) {
422
                        return true;
423
                }
424
                return false;
425
        }
426

    
427
        public boolean hasDataValue(String name) {
428
                name = name.toLowerCase();
429
                return this.data.getType().getIndex(name) >= 0;
430
        }
431

    
432
}