KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > values > types > PropertyType


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.values.types;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.apache.commons.beanutils.DynaBean;
10 import org.apache.commons.beanutils.DynaClass;
11 import org.apache.commons.beanutils.LazyDynaBean;
12 import org.apache.commons.beanutils.PropertyUtils;
13 import org.oddjob.arooa.reflect.IntrospectionHelper;
14
15 /**
16  * This is an internal type used to allow nested properties
17  * in VariablesJob.
18  */

19 public class PropertyType implements DynaBean {
20
21     private LazyDynaBean props = new LazyDynaBean();
22     private Object JavaDoc value;
23     private final String JavaDoc name;
24     
25     /**
26      * Only public constructor. Used to create the root of a property
27      * hierarchy.
28      *
29      */

30     public PropertyType() {
31         this.name = null;
32     }
33     
34     private PropertyType(String JavaDoc name) {
35         this.name = name;
36     }
37     
38     /**
39      * Used to add a long name x.y.z property value.
40      *
41      * This is because set(x.y.z, value) won't create a new property
42      * but will set an existing one.
43      *
44      * @param longName The name.
45      * @param value
46      */

47     public void add(String JavaDoc longName, Object JavaDoc value) {
48         int i = longName.indexOf(PropertyUtils.NESTED_DELIM);
49         String JavaDoc name = longName;
50         if (i > 0) {
51             name = longName.substring(0, i);
52         }
53             
54         PropertyType prop = (PropertyType) props.get(name);
55         if (prop == null) {
56             prop = new PropertyType(name);
57             props.set(prop.name, prop);
58         }
59         
60         if (i < 0) {
61             prop.value = value;
62         } else {
63             prop.add(longName.substring(i+1), value);
64         }
65     }
66     
67     /**
68      * Get the size, which is the number of children,
69      * for this property.
70      *
71      * @return The number of children.
72      */

73     public int size() {
74         return props.getMap().size();
75     }
76     
77     /* (non-Javadoc)
78      * @see org.apache.commons.beanutils.DynaBean#contains(java.lang.String, java.lang.String)
79      */

80     public boolean contains(String JavaDoc name, String JavaDoc key) {
81         // no mapped properties.
82
return false;
83     }
84     
85     /* (non-Javadoc)
86      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
87      */

88     public Object JavaDoc get(String JavaDoc name) {
89         return (PropertyType) props.get(name);
90     }
91     
92     /* (non-Javadoc)
93      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, int)
94      */

95     public Object JavaDoc get(String JavaDoc name, int index) {
96         return null;
97     }
98     /* (non-Javadoc)
99      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, java.lang.String)
100      */

101     public Object JavaDoc get(String JavaDoc name, String JavaDoc key) {
102         return null;
103     }
104     /* (non-Javadoc)
105      * @see org.apache.commons.beanutils.DynaBean#getDynaClass()
106      */

107     public DynaClass getDynaClass() {
108         return props.getDynaClass();
109     }
110     
111     /*
112      * (non-Javadoc)
113      * @see org.apache.commons.beanutils.DynaBean#remove(java.lang.String, java.lang.String)
114      */

115     public void remove(String JavaDoc name, String JavaDoc key) {
116         props.remove(name, key);
117     }
118     
119     /*
120      * (non-Javadoc)
121      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, int, java.lang.Object)
122      */

123     public void set(String JavaDoc name, int index, Object JavaDoc value) {
124         throw new UnsupportedOperationException JavaDoc("Can not set an indexed property.");
125     }
126     
127     /* (non-Javadoc)
128      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.Object)
129      */

130     public void set(String JavaDoc name, Object JavaDoc value) {
131         PropertyType prop = (PropertyType) props.get(name);
132         if (prop == null) {
133             prop = new PropertyType(name);
134             props.set(prop.name, prop);
135         }
136         prop.value = value;
137     }
138     
139     /* (non-Javadoc)
140      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.String, java.lang.Object)
141      */

142     public void set(String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
143         throw new UnsupportedOperationException JavaDoc(
144                 "Can not set a mapped property [" + name + "(" + key + ")]");
145
146     }
147
148     /**
149      * Resolve the value for a this property. This merley returns
150      * the result of resolving the value.
151      *
152      * @param required The required class.
153      * @return A resolved value.
154      */

155     public Object JavaDoc valueFor(Class JavaDoc required) {
156         return IntrospectionHelper.valueFor(value, required);
157     }
158
159     /**
160      * Override toString().
161      *
162      * @return a String or null if the resolved value is null.
163      */

164     public String JavaDoc toString() {
165         if (value != null) {
166             return value.toString();
167         }
168         if (size() == 0) {
169             return null;
170         }
171         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
172         for (Iterator JavaDoc it = props.getMap().entrySet().iterator(); it.hasNext(); ) {
173             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
174             results.append("(");
175             results.append(entry.getKey().toString());
176             results.append("=");
177             results.append(entry.getValue().toString());
178             results.append(")");
179         }
180         return results.toString();
181     }
182 }
183
Popular Tags