Statistics
| Revision:

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

History | View | Annotate | Download (9.66 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.exceptions.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
        }
54

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

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

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

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

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

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

    
87
                int dataType = attribute.getDataType();
88

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
352
                throw new IllegalArgumentException(name);
353
        }
354

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
431
}