root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / dynobject / impl / DefaultDynObject.java @ 25451
History | View | Annotate | Download (2.61 KB)
1 | 24535 | jjdelcerro | package org.gvsig.tools.dynobject.impl; |
---|---|---|---|
2 | |||
3 | 24616 | jjdelcerro | import org.gvsig.tools.dynobject.DynClass; |
4 | 25016 | jjdelcerro | import org.gvsig.tools.dynobject.DynFieldNotFoundException; |
5 | 24616 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
6 | 25238 | jjdelcerro | import org.gvsig.tools.dynobject.impl.DefaultDynClass.FieldAndIndex; |
7 | 24616 | jjdelcerro | |
8 | public class DefaultDynObject implements DynObject { |
||
9 | |||
10 | 25016 | jjdelcerro | private DefaultDynClass dynClass;
|
11 | 25021 | jjdelcerro | private Object[] values; |
12 | 25238 | jjdelcerro | private DynObject[] delegateds; |
13 | 24616 | jjdelcerro | |
14 | 25016 | jjdelcerro | public DefaultDynObject(DynClass dynClass) {
|
15 | this.dynClass = (DefaultDynClass) dynClass;
|
||
16 | 25021 | jjdelcerro | // this.values = null;
|
17 | 25238 | jjdelcerro | this.delegateds = null; |
18 | 25021 | jjdelcerro | this.values = this.dynClass.createValues(null); |
19 | 25016 | jjdelcerro | } |
20 | 24616 | jjdelcerro | |
21 | 25016 | jjdelcerro | public void implement(DynClass dynClass) { |
22 | 25451 | cordinyana | this.dynClass = (DefaultDynClass) ((DefaultDynObjectManager) ((DefaultDynClass) dynClass)
|
23 | .getManager()).get(new DynClass[] { this.dynClass, dynClass }); |
||
24 | 25021 | jjdelcerro | this.values = this.dynClass.createValues(this.values); |
25 | 25016 | jjdelcerro | } |
26 | 24616 | jjdelcerro | |
27 | 25016 | jjdelcerro | public Object getDynValue(String name) throws DynFieldNotFoundException { |
28 | 25021 | jjdelcerro | // if( this.values == null ) {
|
29 | // this.values = this.dynClass.createValues(null);
|
||
30 | // return null;
|
||
31 | // }
|
||
32 | 25238 | jjdelcerro | FieldAndIndex x = dynClass.getDynFieldAndIndex(name); |
33 | Object value = values[ x.getIndex() ];
|
||
34 | if( value != null ) { |
||
35 | return x.getDynField().coerce( value );
|
||
36 | } |
||
37 | if( delegateds != null ) { |
||
38 | for( int i=0; i<delegateds.length; i++) { |
||
39 | DynObject dynObj = delegateds[i]; |
||
40 | if( dynObj.hasDynValue(name) ) {
|
||
41 | return dynObj.getDynValue(name);
|
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | return x.getDynField().getDefaultValue();
|
||
46 | 25016 | jjdelcerro | } |
47 | 24616 | jjdelcerro | |
48 | 25016 | jjdelcerro | public void setDynValue(String name, Object value) throws DynFieldNotFoundException { |
49 | 25021 | jjdelcerro | // if( this.values == null ) {
|
50 | // this.values = this.dynClass.createValues(null);
|
||
51 | // }
|
||
52 | values[dynClass.getFieldIndex(name)] = value; |
||
53 | 25016 | jjdelcerro | } |
54 | 24616 | jjdelcerro | |
55 | 25016 | jjdelcerro | public boolean instanceOf(DynClass dynClass) { |
56 | 25021 | jjdelcerro | return dynClass.isInstance(this); |
57 | 25016 | jjdelcerro | } |
58 | 24616 | jjdelcerro | |
59 | 25016 | jjdelcerro | public DynClass getDynClass() {
|
60 | return this.dynClass; |
||
61 | } |
||
62 | |||
63 | 25238 | jjdelcerro | public boolean hasDynValue(String name) { |
64 | // if( this.values == null ) {
|
||
65 | // this.values = this.dynClass.createValues(null);
|
||
66 | // }
|
||
67 | int index = dynClass.getFieldIndex(name);
|
||
68 | if( this.values[index] == null ) { |
||
69 | return false; |
||
70 | } |
||
71 | return true; |
||
72 | 25034 | jjdelcerro | } |
73 | |||
74 | 25238 | jjdelcerro | public void delegate(DynObject[] dynObjects) { |
75 | if( delegateds == null ) { |
||
76 | this.delegateds = new DynObject[ dynObjects.length]; |
||
77 | System.arraycopy(dynObjects, 0, this.delegateds, 0, dynObjects.length); |
||
78 | return ;
|
||
79 | } |
||
80 | DynObject[] newValues = new DynObject[ this.delegateds.length+dynObjects.length]; |
||
81 | System.arraycopy(delegateds, 0, newValues, 0, delegateds.length); |
||
82 | System.arraycopy(dynObjects, delegateds.length, newValues, delegateds.length, dynObjects.length);
|
||
83 | // TODO, seguro que me he ido en algun +-1 en algun lado del arraycopy.
|
||
84 | } |
||
85 | |||
86 | 25016 | jjdelcerro | } |