KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > api > properties > BeanPropertyMap


1 package org.apache.beehive.controls.api.properties;
2 /*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * $Header:$
18  */

19
20 import java.lang.annotation.Annotation JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * The BeanPropertyMap class represents a collection of property values where properties are
28  * stored in a local HashMap.
29  */

30 public class BeanPropertyMap extends BaseMap implements PropertyMap,java.io.Serializable JavaDoc
31 {
32     private static final HashMap JavaDoc _primToObject = new HashMap JavaDoc();
33
34     static
35     {
36         _primToObject.put(Integer.TYPE, Integer JavaDoc.class);
37         _primToObject.put(Long.TYPE, Long JavaDoc.class);
38         _primToObject.put(Short.TYPE, Short JavaDoc.class);
39         _primToObject.put(Byte.TYPE, Byte JavaDoc.class);
40         _primToObject.put(Float.TYPE, Float JavaDoc.class);
41         _primToObject.put(Double.TYPE, Double JavaDoc.class);
42         _primToObject.put(Character.TYPE, Character JavaDoc.class);
43         _primToObject.put(Boolean.TYPE, Boolean JavaDoc.class);
44     }
45
46     /**
47      * Creates an empty BeanPropertyMap associated with the specific Control public
48      * interface, PropertySet, or annotation type.
49      */

50     public BeanPropertyMap(Class JavaDoc mapClass)
51     {
52         setMapClass(mapClass);
53     }
54
55     /**
56      * Creates a BeanPropertyMap that wraps another PropertyMap. Any changes via setProperty
57      * will be maintained locally on the constructed map, but getProperty will delegate to the
58      * base PropertyMap for properties not set locally.
59      */

60     public BeanPropertyMap(PropertyMap map)
61     {
62         setMapClass(map.getMapClass());
63         setDelegateMap(map);
64     }
65
66     /**
67      * Creates a BeanPropertyMap where default values are derived from a single annotation
68      * type instance. This can be used to create a map from a property getter return value,
69      * to modify element values.
70      */

71     public <T extends Annotation JavaDoc> BeanPropertyMap(T annot)
72     {
73         // If the annotation value is actually a PropertySetProxy, then unwrap it and use
74
// the standard delegation model
75
try
76         {
77             Object JavaDoc handler = Proxy.getInvocationHandler(annot);
78             if (handler instanceof PropertySetProxy)
79             {
80                 PropertySetProxy psp = (PropertySetProxy)handler;
81                 setMapClass(psp.getPropertySet());
82                 setDelegateMap(psp.getPropertyMap());
83                 return;
84             }
85         }
86         catch (IllegalArgumentException JavaDoc iae) {} // regular annotation
87

88         _annot = annot;
89         setMapClass(annot.getClass());
90     }
91
92     /**
93      * Sets the property specifed by 'key' within this map.
94      */

95     public synchronized void setProperty(PropertyKey key, Object JavaDoc value)
96     {
97         if (!isValidKey(key))
98             throw new IllegalArgumentException JavaDoc("Key " + key + " is not valid for " + getMapClass());
99
100         //
101
// Validate the value argument, based upon the property type reference by the key
102
//
103
Class JavaDoc propType = key.getPropertyType();
104         if (value == null)
105         {
106             if (propType.isPrimitive() || propType.isAnnotation())
107                 throw new IllegalArgumentException JavaDoc("Invalid null value for key " + key);
108         }
109         else
110         {
111             if (propType.isPrimitive())
112                 propType = (Class JavaDoc)_primToObject.get(propType);
113
114             if (!propType.isAssignableFrom(value.getClass()))
115             {
116                 throw new IllegalArgumentException JavaDoc("Value class (" + value.getClass() +
117                                                    ") not of expected type: " + propType);
118             }
119         }
120         _properties.put(key, value);
121         _propertySets.add(key.getPropertySet());
122     }
123
124     /**
125      * Returns the property value specified by 'key' within this map.
126      */

127     public Object JavaDoc getProperty(PropertyKey key)
128     {
129         if (!isValidKey(key))
130             throw new IllegalArgumentException JavaDoc("Key " + key + " is not valid for " + getMapClass());
131
132         //
133
// Check local properties first
134
//
135
if (_properties.containsKey(key))
136             return _properties.get(key);
137
138         //
139
// Return the value of the annotation type instance (if any)
140
//
141
if (_annot != null)
142             return key.extractValue(_annot);
143
144         //
145
// Call up to superclass, for delegation model / default value
146
//
147
return super.getProperty(key);
148     }
149
150     /**
151      * Returns true if the PropertyMap contains one or more values for the specified
152      * PropertySet, false otherwise
153      */

154     public boolean containsPropertySet(Class JavaDoc<? extends Annotation JavaDoc> propertySet)
155     {
156         // If we have an annotation type instance and it matches up with the requested
157
// type, then return true
158
if (_annot != null && _annot.getClass().equals(propertySet))
159             return true;
160
161         if (_propertySets.contains(propertySet))
162             return true;
163
164         //
165
// Call up to superclass, for delegation model
166
//
167
return super.containsPropertySet(propertySet);
168     }
169
170     /**
171      * Returns the set of PropertyKeys that are locally set in this property map. Note:
172      * this <b>does not</b> include any properties that might be set as a result of
173      * property lookup delegation.
174      */

175     public Set JavaDoc<PropertyKey> getPropertyKeys() { return _properties.keySet(); }
176
177     // local default annotation value, only set if annot constructor form is used
178
Annotation JavaDoc _annot;
179
180     // locally maintained property values
181
HashMap JavaDoc<PropertyKey,Object JavaDoc> _properties = new HashMap JavaDoc<PropertyKey,Object JavaDoc>();
182
183     // locally maintained PropertySets
184
HashSet JavaDoc<Class JavaDoc> _propertySets = new HashSet JavaDoc<Class JavaDoc>();
185 }
186
Popular Tags