Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObject.java @ 1405

History | View | Annotate | Download (10.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dynobject.impl;
25

    
26
import java.util.HashMap;
27
import java.util.Map;
28

    
29
import org.gvsig.tools.dataTypes.CoercionException;
30
import org.gvsig.tools.dynobject.DelegatedDynObject;
31
import org.gvsig.tools.dynobject.DynClass;
32
import org.gvsig.tools.dynobject.DynField;
33
import org.gvsig.tools.dynobject.DynField_v2;
34
import org.gvsig.tools.dynobject.DynMethod;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.dynobject.DynObjectRuntimeException;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
39
import org.gvsig.tools.dynobject.exception.DynMethodException;
40
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
41

    
42
public class DefaultDynObject implements DelegatedDynObject {
43

    
44
    protected DynClass dynClass;
45
    protected Map values;
46
    protected DynObject[] delegateds;
47

    
48
    public DefaultDynObject(DynStruct dynClass) {
49
        this.dynClass = (DynClass) dynClass;
50
        this.delegateds = null;
51
        this.values = createValues(null);
52
    }
53

    
54
    private Map createValues(Map oldValues) {
55
        HashMap extended = new HashMap();
56
        if ( oldValues != null ) {
57
            extended.putAll(oldValues);
58
        }
59
        return extended;
60
    }
61

    
62
    public void implement(DynClass dynClass) {
63
        this.dynClass = (DefaultDynClass) ((DefaultDynObjectManager) ((DefaultDynClass) dynClass)
64
                .getManager()).get(new DynClass[]{this.dynClass, dynClass});
65
        this.values = createValues(this.values);
66
    }
67

    
68
    public Object getDynValue(String name) throws DynFieldNotFoundException {
69
        boolean defined = false;
70
        Object defaultValue = null;
71
        name = name.toLowerCase();
72
        
73
        
74
        DynField field = dynClass.getDynField(name);
75
        if( field != null ) {
76
            if( field instanceof DynField_v2 && ((DynField_v2)field).isCalculated() ) {
77
                return ((DynField_v2)field).getCalculatedValue(this);
78
            }
79
            if( values.containsKey(name) ) {
80
                return values.get(name);
81
            }
82
            defined = true;
83
            defaultValue = field.getDefaultValue();
84
        }
85
//        
86
//        FieldAndIndex fieldAndIndex = dynClass.getDynFieldAndIndex(name);
87
//        if ( fieldAndIndex != null ) {
88
//            if ( values.containsKey(name) ) {
89
//                return values.get(name);
90
//            }
91
//            defined = true;
92
//            defaultValue = fieldAndIndex.getDynField().getDefaultValue();
93
//        }
94
        if ( delegateds != null ) {
95
            for ( int i = 0; i < delegateds.length; i++ ) {
96
                DynObject dynObj = delegateds[i];
97
                try {
98
                    if ( dynObj.hasDynValue(name) ) {
99
                        return dynObj.getDynValue(name);
100
                    } else {
101
                        defined = true;
102
                        defaultValue = dynObj.getDynValue(name);
103
                    }
104
                } catch (DynFieldNotFoundException ex) {
105
                    ;
106
                }
107
            }
108
        }
109
        if ( defined ) {
110
            return defaultValue;
111
        }
112
        throw new DynFieldNotFoundException(name, dynClass.getName());
113
    }
114

    
115
    public void setDynValue(String name, Object value)
116
            throws DynFieldNotFoundException {
117
        name = name.toLowerCase();
118

    
119
        if ( this.dynClass.getDynField(name) == null ) {
120
            throw new DynFieldNotFoundException(name, this.getDynClass()
121
                    .getName());
122
        }
123

    
124
        try {
125
            values.put(name, this.dynClass.getDynField(name).coerce(value));
126
        } catch (CoercionException e) {
127
            throw new CoerceValueException(this.dynClass, name, value, e);
128
        }
129
    }
130

    
131
    public class CoerceValueException extends DynObjectRuntimeException {
132

    
133
        /**
134
         *
135
         */
136
        private static final long serialVersionUID = 8974502669097158348L;
137

    
138
        public CoerceValueException(DynStruct dynStruct, String fieldName,
139
                Object value, Throwable cause) {
140
            super(
141
                    "Can't convert value %(value) for field %(field) of class %(class).",
142
                    cause,
143
                    "Cant_convert_value_XvalueX_for_field_XfieldX_of_class_XclassX",
144
                    serialVersionUID);
145
            setValue("field", fieldName);
146
            setValue("class", dynStruct.getFullName());
147
            try {
148
                setValue("value", value.toString());
149
            } catch (Exception e1) {
150
                setValue("value", "???");
151
            }
152
        }
153

    
154
    }
155

    
156
    public boolean instanceOf(DynClass dynClass) {
157
        return dynClass.isInstance(this);
158
    }
159

    
160
    public DynClass getDynClass() {
161
        return this.dynClass;
162
    }
163

    
164
    public boolean hasDynValue(String name) throws DynFieldNotFoundException {
165
        boolean defined = false;
166
        name = name.toLowerCase();
167
        
168
        DynField field = dynClass.getDynField(name);
169
        if( field!=null ) {
170
            if ( this.values.containsKey(name) ) {
171
                return true;
172
            }
173
            defined = true;
174
        }
175
        
176
//        int index = dynClass.getFieldIndex(name); xxxx
177
//        if ( index >= 0 ) {
178
//            if ( this.values.containsKey(name) ) {
179
//                return true;
180
//            }
181
//            defined = true;
182
//        }
183
        if ( delegateds != null ) {
184
            for ( int i = 0; i < delegateds.length; i++ ) {
185
                DynObject dynObj = delegateds[i];
186
                try {
187
                    if ( dynObj.hasDynValue(name) ) {
188
                        return true;
189
                    } else {
190
                        defined = true;
191
                    }
192
                } catch (DynFieldNotFoundException ex) {
193
                    ;
194
                }
195
            }
196
        }
197
        if ( defined ) {
198
            return false;
199
        }
200
        throw new DynFieldNotFoundException(name, dynClass.getName());
201
    }
202

    
203
    public void delegate(DynObject dynObjects) {
204
        if ( delegateds == null ) {
205
            this.delegateds = new DynObject[1];
206
            this.delegateds[0] = dynObjects;
207
            return;
208
        }
209
        DynObject[] newValues = new DynObject[this.delegateds.length + 1];
210
        System.arraycopy(delegateds, 0, newValues, 0, delegateds.length);
211
        newValues[delegateds.length] = dynObjects;
212
        this.delegateds = newValues;
213
    }
214

    
215
    @Override
216
    public Object invokeDynMethod(String name, Object[] args)
217
            throws DynMethodException {
218
        return this.invokeDynMethod(this, name, args);
219
    }
220

    
221
    @Override
222
    public Object invokeDynMethod(int code, Object[] args) throws DynMethodException {
223
        return this.invokeDynMethod(this, code, args);
224
    }
225
    
226
    @Override
227
    public Object invokeDynMethod(Object self, String methodName, Object[] args) throws DynMethodException {
228
        DynMethod method = this.dynClass.getDynMethod(methodName);
229
        if (method != null) {
230
            return method.invoke((DynObject)self, args);
231
        }
232
        if ( delegateds != null ) {
233
            for ( int i = 0; i < delegateds.length; i++ ) {
234
                try {
235
                    return delegateds[i].invokeDynMethod(methodName,args);
236
                } catch (DynMethodNotSupportedException e) {
237
                    // continue next delegated
238
                }
239
            }
240
        }
241
        throw new DynMethodNotSupportedException(methodName, self.getClass().getName());
242
    }
243

    
244
    @Override
245
    public Object invokeDynMethod(Object self, int methodCode, Object[] args) throws DynMethodException {
246
        DynMethod method = this.dynClass.getDynMethod(methodCode);
247
        if (method != null) {
248
            return method.invoke((DynObject)self, args);
249
        }
250
        if ( delegateds != null ) {
251
            for ( int i = 0; i < delegateds.length; i++ ) {
252
                try {
253
                    return delegateds[i].invokeDynMethod(methodCode,args);
254
                } catch (DynMethodNotSupportedException e) {
255
                    // continue next delegated
256
                }
257
            }
258
        }
259
        throw new DynMethodNotSupportedException(methodCode, self.getClass().getName());
260
    }
261

    
262
    public void clear() {
263
        DynField[] fields = getDynClass().getDeclaredDynFields();
264

    
265
        for ( int i = 0; i < fields.length; i++ ) {
266
            this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
267
        }
268
    }
269

    
270
    public String toString() {
271
        return DefaultDynObject.toString(this);
272
    }
273

    
274
    public static String toString(DynObject obj) {
275
        StringBuffer buffer = new StringBuffer();
276

    
277
        DynClass dynClass = obj.getDynClass();
278
        buffer.append("DynClass name: ").append(dynClass.getName()).append(
279
                ";  Fields: ");
280

    
281
        DynField[] fields = dynClass.getDynFields();
282

    
283
        if ( fields == null || fields.length == 0 ) {
284
            buffer.append("(none)");
285
        } else {
286
            buffer.append("[");
287
            for ( int i = 0; i < fields.length; i++ ) {
288
                if ( i != 0 ) {
289
                    buffer.append(", ");
290
                }
291
                buffer.append(fields[i].getName()).append(" = ")
292
                        .append(obj.getDynValue(fields[i].getName()));
293
            }
294
            buffer.append("]");
295
        }
296
        return buffer.toString();
297
    }
298

    
299
    public boolean hasEmptyValues() {
300
        return this.values.isEmpty();
301
    }
302
}