gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObject.java @ 1548
History | View | Annotate | Download (11.3 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
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 | 1119 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 802 | cordinyana | * 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 | 1119 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 802 | cordinyana | *
|
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 | 18 | cordinyana | package org.gvsig.tools.dynobject.impl; |
25 | |||
26 | 1119 | jjdelcerro | import java.util.HashMap; |
27 | 387 | cmartin | import java.util.Map; |
28 | |||
29 | 112 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
30 | 18 | cordinyana | import org.gvsig.tools.dynobject.DelegatedDynObject; |
31 | import org.gvsig.tools.dynobject.DynClass; |
||
32 | import org.gvsig.tools.dynobject.DynField; |
||
33 | 1405 | jjdelcerro | import org.gvsig.tools.dynobject.DynField_v2; |
34 | 18 | cordinyana | import org.gvsig.tools.dynobject.DynMethod; |
35 | import org.gvsig.tools.dynobject.DynObject; |
||
36 | 146 | jjdelcerro | import org.gvsig.tools.dynobject.DynObjectRuntimeException; |
37 | 1548 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject_v2; |
38 | 128 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
39 | 18 | cordinyana | import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
40 | import org.gvsig.tools.dynobject.exception.DynMethodException; |
||
41 | import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
||
42 | |||
43 | 1548 | jjdelcerro | public class DefaultDynObject implements DelegatedDynObject, DynObject_v2 { |
44 | 18 | cordinyana | |
45 | 1119 | jjdelcerro | protected DynClass dynClass;
|
46 | protected Map values; |
||
47 | protected DynObject[] delegateds; |
||
48 | 18 | cordinyana | |
49 | 1119 | jjdelcerro | public DefaultDynObject(DynStruct dynClass) {
|
50 | this.dynClass = (DynClass) dynClass;
|
||
51 | this.delegateds = null; |
||
52 | this.values = createValues(null); |
||
53 | } |
||
54 | 18 | cordinyana | |
55 | 1119 | jjdelcerro | private Map createValues(Map oldValues) { |
56 | HashMap extended = new HashMap(); |
||
57 | if ( oldValues != null ) { |
||
58 | extended.putAll(oldValues); |
||
59 | } |
||
60 | return extended;
|
||
61 | } |
||
62 | 18 | cordinyana | |
63 | 1119 | jjdelcerro | public void implement(DynClass dynClass) { |
64 | this.dynClass = (DefaultDynClass) ((DefaultDynObjectManager) ((DefaultDynClass) dynClass)
|
||
65 | .getManager()).get(new DynClass[]{this.dynClass, dynClass}); |
||
66 | this.values = createValues(this.values); |
||
67 | } |
||
68 | 18 | cordinyana | |
69 | 1119 | jjdelcerro | public Object getDynValue(String name) throws DynFieldNotFoundException { |
70 | boolean defined = false; |
||
71 | Object defaultValue = null; |
||
72 | name = name.toLowerCase(); |
||
73 | |||
74 | |||
75 | DynField field = dynClass.getDynField(name); |
||
76 | if( field != null ) { |
||
77 | 1405 | jjdelcerro | if( field instanceof DynField_v2 && ((DynField_v2)field).isCalculated() ) { |
78 | return ((DynField_v2)field).getCalculatedValue(this); |
||
79 | } |
||
80 | 1119 | jjdelcerro | if( values.containsKey(name) ) {
|
81 | return values.get(name);
|
||
82 | } |
||
83 | defined = true;
|
||
84 | defaultValue = field.getDefaultValue(); |
||
85 | } |
||
86 | //
|
||
87 | // FieldAndIndex fieldAndIndex = dynClass.getDynFieldAndIndex(name);
|
||
88 | // if ( fieldAndIndex != null ) {
|
||
89 | // if ( values.containsKey(name) ) {
|
||
90 | // return values.get(name);
|
||
91 | // }
|
||
92 | // defined = true;
|
||
93 | // defaultValue = fieldAndIndex.getDynField().getDefaultValue();
|
||
94 | // }
|
||
95 | if ( delegateds != null ) { |
||
96 | for ( int i = 0; i < delegateds.length; i++ ) { |
||
97 | DynObject dynObj = delegateds[i]; |
||
98 | try {
|
||
99 | if ( dynObj.hasDynValue(name) ) {
|
||
100 | return dynObj.getDynValue(name);
|
||
101 | } else {
|
||
102 | defined = true;
|
||
103 | defaultValue = dynObj.getDynValue(name); |
||
104 | } |
||
105 | } catch (DynFieldNotFoundException ex) {
|
||
106 | ; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | if ( defined ) {
|
||
111 | return defaultValue;
|
||
112 | } |
||
113 | throw new DynFieldNotFoundException(name, dynClass.getName()); |
||
114 | } |
||
115 | 387 | cmartin | |
116 | 1119 | jjdelcerro | public void setDynValue(String name, Object value) |
117 | throws DynFieldNotFoundException {
|
||
118 | name = name.toLowerCase(); |
||
119 | 387 | cmartin | |
120 | 1119 | jjdelcerro | if ( this.dynClass.getDynField(name) == null ) { |
121 | throw new DynFieldNotFoundException(name, this.getDynClass() |
||
122 | .getName()); |
||
123 | } |
||
124 | 18 | cordinyana | |
125 | 1119 | jjdelcerro | try {
|
126 | values.put(name, this.dynClass.getDynField(name).coerce(value));
|
||
127 | } catch (CoercionException e) {
|
||
128 | throw new CoerceValueException(this.dynClass, name, value, e); |
||
129 | } |
||
130 | } |
||
131 | 146 | jjdelcerro | |
132 | 1119 | jjdelcerro | public class CoerceValueException extends DynObjectRuntimeException { |
133 | 387 | cmartin | |
134 | 1119 | jjdelcerro | /**
|
135 | *
|
||
136 | */
|
||
137 | private static final long serialVersionUID = 8974502669097158348L; |
||
138 | 146 | jjdelcerro | |
139 | 1119 | jjdelcerro | public CoerceValueException(DynStruct dynStruct, String fieldName, |
140 | Object value, Throwable cause) { |
||
141 | super(
|
||
142 | "Can't convert value %(value) for field %(field) of class %(class).",
|
||
143 | cause, |
||
144 | "Cant_convert_value_XvalueX_for_field_XfieldX_of_class_XclassX",
|
||
145 | serialVersionUID); |
||
146 | setValue("field", fieldName);
|
||
147 | setValue("class", dynStruct.getFullName());
|
||
148 | try {
|
||
149 | setValue("value", value.toString());
|
||
150 | } catch (Exception e1) { |
||
151 | setValue("value", "???"); |
||
152 | } |
||
153 | } |
||
154 | 18 | cordinyana | |
155 | 1119 | jjdelcerro | } |
156 | 18 | cordinyana | |
157 | 1119 | jjdelcerro | public boolean instanceOf(DynClass dynClass) { |
158 | return dynClass.isInstance(this); |
||
159 | } |
||
160 | 387 | cmartin | |
161 | 1119 | jjdelcerro | public DynClass getDynClass() {
|
162 | return this.dynClass; |
||
163 | } |
||
164 | 18 | cordinyana | |
165 | 1548 | jjdelcerro | @Override
|
166 | public boolean hasDynMethod(String name) { |
||
167 | name = name.toLowerCase(); |
||
168 | |||
169 | DynMethod method; |
||
170 | try {
|
||
171 | method = this.dynClass.getDynMethod(name);
|
||
172 | if( method!=null ) { |
||
173 | return true; |
||
174 | } |
||
175 | } catch (DynMethodException ex) {
|
||
176 | } |
||
177 | if ( delegateds != null ) { |
||
178 | for( DynObject dynObj : delegateds ) {
|
||
179 | try {
|
||
180 | if( dynObj instanceof DynObject_v2 ) { |
||
181 | if( ((DynObject_v2)dynObj).hasDynMethod(name) ) {
|
||
182 | return true; |
||
183 | } |
||
184 | } else {
|
||
185 | DynClass dynClass = dynObj.getDynClass(); |
||
186 | if( dynClass.getDynMethod(name)!=null ) { |
||
187 | return true; |
||
188 | } |
||
189 | } |
||
190 | } catch(Throwable th) { |
||
191 | // continue checking others
|
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | 1119 | jjdelcerro | public boolean hasDynValue(String name) throws DynFieldNotFoundException { |
199 | boolean defined = false; |
||
200 | name = name.toLowerCase(); |
||
201 | |||
202 | DynField field = dynClass.getDynField(name); |
||
203 | if( field!=null ) { |
||
204 | if ( this.values.containsKey(name) ) { |
||
205 | return true; |
||
206 | } |
||
207 | defined = true;
|
||
208 | } |
||
209 | |||
210 | // int index = dynClass.getFieldIndex(name); xxxx
|
||
211 | // if ( index >= 0 ) {
|
||
212 | // if ( this.values.containsKey(name) ) {
|
||
213 | // return true;
|
||
214 | // }
|
||
215 | // defined = true;
|
||
216 | // }
|
||
217 | if ( delegateds != null ) { |
||
218 | for ( int i = 0; i < delegateds.length; i++ ) { |
||
219 | DynObject dynObj = delegateds[i]; |
||
220 | try {
|
||
221 | if ( dynObj.hasDynValue(name) ) {
|
||
222 | return true; |
||
223 | } else {
|
||
224 | defined = true;
|
||
225 | } |
||
226 | } catch (DynFieldNotFoundException ex) {
|
||
227 | ; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | if ( defined ) {
|
||
232 | return false; |
||
233 | } |
||
234 | throw new DynFieldNotFoundException(name, dynClass.getName()); |
||
235 | } |
||
236 | 18 | cordinyana | |
237 | 1119 | jjdelcerro | public void delegate(DynObject dynObjects) { |
238 | if ( delegateds == null ) { |
||
239 | this.delegateds = new DynObject[1]; |
||
240 | this.delegateds[0] = dynObjects; |
||
241 | return;
|
||
242 | } |
||
243 | DynObject[] newValues = new DynObject[this.delegateds.length + 1]; |
||
244 | System.arraycopy(delegateds, 0, newValues, 0, delegateds.length); |
||
245 | newValues[delegateds.length] = dynObjects; |
||
246 | this.delegateds = newValues;
|
||
247 | } |
||
248 | 18 | cordinyana | |
249 | 1405 | jjdelcerro | @Override
|
250 | public Object invokeDynMethod(String name, Object[] args) |
||
251 | 1119 | jjdelcerro | throws DynMethodException {
|
252 | 1405 | jjdelcerro | return this.invokeDynMethod(this, name, args); |
253 | 1119 | jjdelcerro | } |
254 | 18 | cordinyana | |
255 | 1405 | jjdelcerro | @Override
|
256 | public Object invokeDynMethod(int code, Object[] args) throws DynMethodException { |
||
257 | return this.invokeDynMethod(this, code, args); |
||
258 | 1119 | jjdelcerro | } |
259 | 1405 | jjdelcerro | |
260 | @Override
|
||
261 | public Object invokeDynMethod(Object self, String methodName, Object[] args) throws DynMethodException { |
||
262 | 1119 | jjdelcerro | DynMethod method = this.dynClass.getDynMethod(methodName);
|
263 | 1405 | jjdelcerro | if (method != null) { |
264 | return method.invoke((DynObject)self, args);
|
||
265 | } |
||
266 | if ( delegateds != null ) { |
||
267 | for ( int i = 0; i < delegateds.length; i++ ) { |
||
268 | try {
|
||
269 | return delegateds[i].invokeDynMethod(methodName,args);
|
||
270 | } catch (DynMethodNotSupportedException e) {
|
||
271 | // continue next delegated
|
||
272 | 1119 | jjdelcerro | } |
273 | } |
||
274 | } |
||
275 | 1405 | jjdelcerro | throw new DynMethodNotSupportedException(methodName, self.getClass().getName()); |
276 | 1119 | jjdelcerro | } |
277 | 18 | cordinyana | |
278 | 1405 | jjdelcerro | @Override
|
279 | public Object invokeDynMethod(Object self, int methodCode, Object[] args) throws DynMethodException { |
||
280 | 1119 | jjdelcerro | DynMethod method = this.dynClass.getDynMethod(methodCode);
|
281 | 1405 | jjdelcerro | if (method != null) { |
282 | return method.invoke((DynObject)self, args);
|
||
283 | } |
||
284 | if ( delegateds != null ) { |
||
285 | for ( int i = 0; i < delegateds.length; i++ ) { |
||
286 | try {
|
||
287 | return delegateds[i].invokeDynMethod(methodCode,args);
|
||
288 | } catch (DynMethodNotSupportedException e) {
|
||
289 | // continue next delegated
|
||
290 | 1119 | jjdelcerro | } |
291 | } |
||
292 | } |
||
293 | 1405 | jjdelcerro | throw new DynMethodNotSupportedException(methodCode, self.getClass().getName()); |
294 | 1119 | jjdelcerro | } |
295 | 18 | cordinyana | |
296 | 1119 | jjdelcerro | public void clear() { |
297 | DynField[] fields = getDynClass().getDeclaredDynFields();
|
||
298 | 92 | cordinyana | |
299 | 1119 | jjdelcerro | for ( int i = 0; i < fields.length; i++ ) { |
300 | this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
|
||
301 | 1085 | jjdelcerro | } |
302 | 1119 | jjdelcerro | } |
303 | 1085 | jjdelcerro | |
304 | 1119 | jjdelcerro | public String toString() { |
305 | return DefaultDynObject.toString(this); |
||
306 | } |
||
307 | 92 | cordinyana | |
308 | 1119 | jjdelcerro | public static String toString(DynObject obj) { |
309 | StringBuffer buffer = new StringBuffer(); |
||
310 | 92 | cordinyana | |
311 | 1119 | jjdelcerro | DynClass dynClass = obj.getDynClass(); |
312 | buffer.append("DynClass name: ").append(dynClass.getName()).append(
|
||
313 | "; Fields: ");
|
||
314 | 92 | cordinyana | |
315 | 1348 | jjdelcerro | DynField[] fields = dynClass.getDynFields();
|
316 | 465 | cmartin | |
317 | 1119 | jjdelcerro | if ( fields == null || fields.length == 0 ) { |
318 | buffer.append("(none)");
|
||
319 | } else {
|
||
320 | buffer.append("[");
|
||
321 | for ( int i = 0; i < fields.length; i++ ) { |
||
322 | if ( i != 0 ) { |
||
323 | buffer.append(", ");
|
||
324 | } |
||
325 | buffer.append(fields[i].getName()).append(" = ")
|
||
326 | .append(obj.getDynValue(fields[i].getName())); |
||
327 | } |
||
328 | buffer.append("]");
|
||
329 | } |
||
330 | return buffer.toString();
|
||
331 | } |
||
332 | |||
333 | 465 | cmartin | public boolean hasEmptyValues() { |
334 | return this.values.isEmpty(); |
||
335 | } |
||
336 | 73 | jjdelcerro | } |