|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassAdaptor.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.util;
|
|
16 |
|
|
17 |
import java.beans.PropertyDescriptor;
|
|
18 |
import java.util.ArrayList;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
import java.util.Map;
|
|
23 |
|
|
24 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* Provides access to an object (of a particular class) as a set of individual property
|
|
28 |
* that may be read or updated.
|
|
29 |
*
|
|
30 |
* @author Howard Lewis Ship
|
|
31 |
*/
|
|
32 |
class ClassAdaptor
|
|
33 |
{ |
|
34 |
private final Map _propertyAdaptorMap = new HashMap(); |
|
35 |
|
|
36 | 62 |
public ClassAdaptor(PropertyDescriptor[] properties)
|
37 |
{ |
|
38 | 62 |
for (int i = 0; i < properties.length; i++) |
39 |
{ |
|
40 | 204 |
PropertyDescriptor d = properties[i]; |
41 |
|
|
42 | 204 |
String name = d.getName(); |
43 |
|
|
44 | 204 |
_propertyAdaptorMap.put( |
45 |
name, |
|
46 |
new PropertyAdaptor(
|
|
47 |
name, |
|
48 |
d.getPropertyType(), |
|
49 |
d.getReadMethod(), |
|
50 |
d.getWriteMethod())); |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Updates the property of the target object.
|
|
56 |
*
|
|
57 |
* @param target the object to update
|
|
58 |
* @param value the value to be stored into the target object property
|
|
59 |
*/
|
|
60 | 3797 |
public void write(Object target, String propertyName, Object value) |
61 |
{ |
|
62 | 3797 |
PropertyAdaptor a = getAdaptor(target, propertyName); |
63 |
|
|
64 | 3797 |
a.write(target, value); |
65 |
} |
|
66 |
|
|
67 |
/**
|
|
68 |
* Reads the property of the target object.
|
|
69 |
*
|
|
70 |
* @param target the object to read
|
|
71 |
* @param propertyName the name of the property to read
|
|
72 |
*/
|
|
73 | 9 |
public Object read(Object target, String propertyName)
|
74 |
{ |
|
75 | 9 |
PropertyAdaptor a = getAdaptor(target, propertyName); |
76 |
|
|
77 | 8 |
return a.read(target);
|
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Returns the type of the named property.
|
|
82 |
*
|
|
83 |
* @param target the object to examine
|
|
84 |
* @param propertyName the name of the property to check
|
|
85 |
*/
|
|
86 | 3853 |
public Class getPropertyType(Object target, String propertyName)
|
87 |
{ |
|
88 | 3853 |
PropertyAdaptor a = getAdaptor(target, propertyName); |
89 |
|
|
90 | 3852 |
return a.getPropertyType();
|
91 |
} |
|
92 |
|
|
93 |
/**
|
|
94 |
* Returns true if the named property exists and is readable.
|
|
95 |
*/
|
|
96 |
|
|
97 | 4 |
public boolean isReadable(String propertyName) |
98 |
{ |
|
99 | 4 |
PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); |
100 |
|
|
101 | 4 |
return result != null && result.isReadable(); |
102 |
} |
|
103 |
|
|
104 |
/**
|
|
105 |
* Returns true if the named property exists and is writable.
|
|
106 |
*/
|
|
107 |
|
|
108 | 1142 |
public boolean isWritable(String propertyName) |
109 |
{ |
|
110 | 1142 |
PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); |
111 |
|
|
112 | 1142 |
return result != null && result.isWritable(); |
113 |
} |
|
114 |
|
|
115 | 7659 |
private PropertyAdaptor getAdaptor(Object target, String propertyName)
|
116 |
{ |
|
117 | 7659 |
PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName); |
118 |
|
|
119 | 7659 |
if (result == null) |
120 | 2 |
throw new ApplicationRuntimeException( |
121 |
UtilMessages.noSuchProperty(target, propertyName), |
|
122 |
target, |
|
123 |
null,
|
|
124 |
null);
|
|
125 |
|
|
126 | 7657 |
return result;
|
127 |
} |
|
128 |
|
|
129 |
/**
|
|
130 |
* Returns a List of the names of readable properties (properties with a non-null getter).
|
|
131 |
*/
|
|
132 | 1 |
public List getReadableProperties()
|
133 |
{ |
|
134 | 1 |
List result = new ArrayList(_propertyAdaptorMap.size());
|
135 |
|
|
136 | 1 |
Iterator i = _propertyAdaptorMap.values().iterator(); |
137 |
|
|
138 | 1 |
while (i.hasNext())
|
139 |
{ |
|
140 | 3 |
PropertyAdaptor a = (PropertyAdaptor) i.next(); |
141 |
|
|
142 | 3 |
if (a.isReadable())
|
143 | 2 |
result.add(a.getPropertyName()); |
144 |
} |
|
145 |
|
|
146 | 1 |
return result;
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* Returns a List of the names of readable properties (properties with a non-null setter).
|
|
151 |
*/
|
|
152 | 236 |
public List getWriteableProperties()
|
153 |
{ |
|
154 | 236 |
List result = new ArrayList(_propertyAdaptorMap.size());
|
155 |
|
|
156 | 236 |
Iterator i = _propertyAdaptorMap.values().iterator(); |
157 |
|
|
158 | 236 |
while (i.hasNext())
|
159 |
{ |
|
160 | 589 |
PropertyAdaptor a = (PropertyAdaptor) i.next(); |
161 |
|
|
162 | 589 |
if (a.isWritable())
|
163 | 348 |
result.add(a.getPropertyName()); |
164 |
} |
|
165 |
|
|
166 | 236 |
return result;
|
167 |
} |
|
168 |
} |
|
169 |
|
|