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 |
/**
|
---|---|
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.DynObject_v2; |
38 |
import org.gvsig.tools.dynobject.DynStruct; |
39 |
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 |
public class DefaultDynObject implements DelegatedDynObject, DynObject_v2 { |
44 |
|
45 |
protected DynClass dynClass;
|
46 |
protected Map values; |
47 |
protected DynObject[] delegateds; |
48 |
|
49 |
public DefaultDynObject(DynStruct dynClass) {
|
50 |
this.dynClass = (DynClass) dynClass;
|
51 |
this.delegateds = null; |
52 |
this.values = createValues(null); |
53 |
} |
54 |
|
55 |
private Map createValues(Map oldValues) { |
56 |
HashMap extended = new HashMap(); |
57 |
if ( oldValues != null ) { |
58 |
extended.putAll(oldValues); |
59 |
} |
60 |
return extended;
|
61 |
} |
62 |
|
63 |
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 |
|
69 |
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 |
if( field instanceof DynField_v2 && ((DynField_v2)field).isCalculated() ) { |
78 |
return ((DynField_v2)field).getCalculatedValue(this); |
79 |
} |
80 |
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 |
|
116 |
public void setDynValue(String name, Object value) |
117 |
throws DynFieldNotFoundException {
|
118 |
name = name.toLowerCase(); |
119 |
|
120 |
if ( this.dynClass.getDynField(name) == null ) { |
121 |
throw new DynFieldNotFoundException(name, this.getDynClass() |
122 |
.getName()); |
123 |
} |
124 |
|
125 |
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 |
|
132 |
public class CoerceValueException extends DynObjectRuntimeException { |
133 |
|
134 |
/**
|
135 |
*
|
136 |
*/
|
137 |
private static final long serialVersionUID = 8974502669097158348L; |
138 |
|
139 |
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 |
|
155 |
} |
156 |
|
157 |
public boolean instanceOf(DynClass dynClass) { |
158 |
return dynClass.isInstance(this); |
159 |
} |
160 |
|
161 |
public DynClass getDynClass() {
|
162 |
return this.dynClass; |
163 |
} |
164 |
|
165 |
@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 |
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 |
|
237 |
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 |
|
249 |
@Override
|
250 |
public Object invokeDynMethod(String name, Object[] args) |
251 |
throws DynMethodException {
|
252 |
return this.invokeDynMethod(this, name, args); |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public Object invokeDynMethod(int code, Object[] args) throws DynMethodException { |
257 |
return this.invokeDynMethod(this, code, args); |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public Object invokeDynMethod(Object self, String methodName, Object[] args) throws DynMethodException { |
262 |
DynMethod method = this.dynClass.getDynMethod(methodName);
|
263 |
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 |
} |
273 |
} |
274 |
} |
275 |
throw new DynMethodNotSupportedException(methodName, self.getClass().getName()); |
276 |
} |
277 |
|
278 |
@Override
|
279 |
public Object invokeDynMethod(Object self, int methodCode, Object[] args) throws DynMethodException { |
280 |
DynMethod method = this.dynClass.getDynMethod(methodCode);
|
281 |
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 |
} |
291 |
} |
292 |
} |
293 |
throw new DynMethodNotSupportedException(methodCode, self.getClass().getName()); |
294 |
} |
295 |
|
296 |
public void clear() { |
297 |
DynField[] fields = getDynClass().getDeclaredDynFields();
|
298 |
|
299 |
for ( int i = 0; i < fields.length; i++ ) { |
300 |
this.setDynValue(fields[i].getName(), fields[i].getDefaultValue());
|
301 |
} |
302 |
} |
303 |
|
304 |
public String toString() { |
305 |
return DefaultDynObject.toString(this); |
306 |
} |
307 |
|
308 |
public static String toString(DynObject obj) { |
309 |
StringBuffer buffer = new StringBuffer(); |
310 |
|
311 |
DynClass dynClass = obj.getDynClass(); |
312 |
buffer.append("DynClass name: ").append(dynClass.getName()).append(
|
313 |
"; Fields: ");
|
314 |
|
315 |
DynField[] fields = dynClass.getDynFields();
|
316 |
|
317 |
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 |
public boolean hasEmptyValues() { |
334 |
return this.values.isEmpty(); |
335 |
} |
336 |
} |