KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.beehive.controls.api.ControlException;
25
26 /**
27  * The PropertyKey class represents a key that can be used to set a JSR-175 attribute member
28  * value within a <code>PropertyMap</code>.
29  */

30 public class PropertyKey implements java.io.Serializable JavaDoc
31 {
32     /**
33      * This constructor takes the JSR-175 metadata interface that is associated with
34      * the contained attributes.
35      */

36     public PropertyKey(Class JavaDoc<? extends Annotation JavaDoc> propertySet, String JavaDoc propertyName)
37     {
38         if (!propertySet.isAnnotation())
39         {
40             throw new IllegalArgumentException JavaDoc("Class " + propertySet +
41                                                " is not a valid annotation type");
42         }
43
44         try
45         {
46             _getMethod = propertySet.getMethod(propertyName, (Class JavaDoc [])null);
47             _propertySet = propertySet;
48             _propertyName = propertyName;
49             _propertyType = _getMethod.getReturnType();
50
51             //
52
// Compute a hash code for the key instance that will be constant for all keys
53
// that reference the same interface/member combo
54
//
55
_hashCode = new String JavaDoc(propertySet.getName() + "." + propertyName).hashCode();
56         }
57         catch (NoSuchMethodException JavaDoc nsme)
58         {
59             throw new IllegalArgumentException JavaDoc(propertyName +
60                           "is not a valid member of the metadata interface " + propertySet);
61         }
62     }
63
64     protected Method JavaDoc getMethod()
65     {
66         if (null == _getMethod)
67         {
68             try
69             {
70                 _getMethod = _propertySet.getMethod(_propertyName, (Class JavaDoc [])null);
71             }
72             catch(NoSuchMethodException JavaDoc nsmEx)
73             {
74                 // This can only happen if a PropertySet is incompatibly changed after
75
// serialization of a PropertyKey (since it is initially validated in
76
// the constructor).
77
throw new ControlException("Unable to locate PropertyKey accessor method", nsmEx);
78             }
79         }
80         return _getMethod;
81     }
82
83     /**
84      * Computes the default value for the value of this property key, or null if there
85      * is no defined default.
86      */

87     public Object JavaDoc getDefaultValue()
88     {
89         // Query the accessor method for the default value
90
// This method will return 'null' if there is no defined default
91
return getMethod().getDefaultValue();
92     }
93
94     /**
95      * Extracts the value of the key from an Annotation instance
96      */

97     /* package */ Object JavaDoc extractValue(Annotation JavaDoc annot)
98     {
99         try
100         {
101             return getMethod().invoke(annot, new Object JavaDoc [] {});
102         }
103         // TODO -- cleanup exception handling, property defining a PropertyException
104
catch (RuntimeException JavaDoc re) { throw re; }
105         catch (Exception JavaDoc e)
106         {
107             throw new RuntimeException JavaDoc("Unable to extract value for " + _propertyName, e);
108         }
109     }
110
111     public boolean equals(Object JavaDoc obj)
112     {
113         // fast success for static key declaration cases
114
if (this == obj)
115             return true;
116
117         // fast fail on obvious differences
118
if (obj == null || !(obj instanceof PropertyKey) || _hashCode != obj.hashCode())
119             return false;
120
121         // slower success on two equivalent keys constructed independently
122
PropertyKey keyObj = (PropertyKey)obj;
123         return _propertySet.equals(keyObj._propertySet) &&
124                _propertyName.equals(keyObj._propertyName);
125     }
126
127     public int hashCode() { return _hashCode; }
128
129     public String JavaDoc toString()
130     {
131         return "PropertyKey: " + _propertySet.getName() + "." + _propertyName;
132     }
133
134     public Class JavaDoc<? extends Annotation JavaDoc> getPropertySet() { return _propertySet; }
135     public String JavaDoc getPropertyName() { return _propertyName; }
136     public Class JavaDoc getPropertyType() { return _propertyType; }
137     public Annotation JavaDoc[] getAnnotations() { return getMethod().getAnnotations();}
138
139     Class JavaDoc<? extends Annotation JavaDoc> _propertySet;
140     String JavaDoc _propertyName;
141     Class JavaDoc _propertyType;
142     int _hashCode;
143
144     // WARNING: This field should never be accessed directly but instead via the getMethod()
145
// API. This ensures that the (transient) value is appropriately recomputed when necessary.
146
private transient Method JavaDoc _getMethod;
147 }
148
Popular Tags