KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > coerce > CoercionHandler


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util.coerce;
11
12 import java.util.Map JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Collections JavaDoc;
15
16 import org.jboss.util.CoercionException;
17 import org.jboss.util.NotCoercibleException;
18 import org.jboss.util.NullArgumentException;
19 import org.jboss.util.NotImplementedException;
20
21 /**
22  * An abstract class to allow extending the default behavior of
23  * {@link org.jboss.util.Objects#coerce(Object,Class)} when it is
24  * not possible to implement {@link org.jboss.util.Coercible}
25  * directly in the target class or where coercion is desired from
26  * an unrelated class. Also provides a registry for all of the
27  * currently installed handlers.
28  *
29  * @todo Implement a URL package handler style method for finding
30  * handlers as well as the current explict methods.
31  * @todo Add URL handler.
32  *
33  * @version <tt>$Revision: 1.5 $</tt>
34  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
35  */

36 public abstract class CoercionHandler
37 {
38    /**
39     * Coerce the given value into the specified type.
40     *
41     * @param value Value to coerce
42     * @param type Object type to coerce into
43     * @return Coerced value
44     *
45     * @exception CoercionException Failed to coerce
46     */

47    public abstract Object JavaDoc coerce(Object JavaDoc value, Class JavaDoc type)
48       throws CoercionException;
49
50    /**
51     * Get the target class type for this CoercionHandler.
52     *
53     * @return Class type
54     *
55     * @throws NotImplementedException Handler is not bound
56     */

57    public Class JavaDoc getType() {
58       throw new NotImplementedException("handler is not bound");
59    }
60
61    /////////////////////////////////////////////////////////////////////////
62
// Factory Methods //
63
/////////////////////////////////////////////////////////////////////////
64

65    /** Class -> CoercionHandler map */
66    private static Map JavaDoc handlers = Collections.synchronizedMap(new HashMap JavaDoc());
67
68    /** Initializes the CoercionHandler map */
69    static {
70       // initialzie the helper map with some defaults
71
install(new CharacterHandler());
72       install(new ClassHandler());
73       install(new FileHandler());
74    }
75
76    /**
77     * Install a CoercionHandler for a given class type.
78     *
79     * @param handler Coercion handler
80     *
81     * @throws NullArgumentException type or handler
82     */

83    public static void install(Class JavaDoc type, CoercionHandler handler) {
84       if (type == null)
85          throw new NullArgumentException("type");
86       if (handler == null)
87          throw new NullArgumentException("handler");
88
89       handlers.put(type, handler);
90    }
91
92    /**
93     * Install a BoundCoercionHandler.
94     *
95     * @param handler Bound coercion handler
96     *
97     * @throws NullArgumentException handler
98     */

99    public static void install(BoundCoercionHandler handler) {
100       if (handler == null)
101          throw new NullArgumentException("handler");
102
103       handlers.put(handler.getType(), handler);
104    }
105
106    /**
107     * Uninstall a CoercionHandler for a given class type.
108     *
109     * @param type Class type
110     *
111     * @throws NullArgumentException type
112     */

113    public static void uninstall(Class JavaDoc type) {
114       if (type == null)
115          throw new NullArgumentException("type");
116
117       handlers.remove(type);
118    }
119
120    /**
121     * Check if there is a CoercionHandler installed for the given class.
122     *
123     * @param type Class type
124     * @return True if there is a CoercionHandler
125     */

126    public static boolean isInstalled(Class JavaDoc type) {
127       return handlers.containsKey(type);
128    }
129
130    /**
131     * Lookup the CoercionHandler for a given class.
132     *
133     * @param type Class type
134     * @return A CoercionHandler or null if there is no installed handler
135     *
136     * @throws NullArgumentException type
137     */

138    public static CoercionHandler lookup(Class JavaDoc type) {
139       if (type == null)
140          throw new NullArgumentException("type");
141
142       return (CoercionHandler)handlers.get(type);
143    }
144
145    /**
146     * Create a CoercionHandler for the given class type.
147     *
148     * @param type Class type
149     * @return A CoercionHandler instance for the given class type.
150     *
151     * @throws CoercionException No installed handler for type
152     */

153    public static CoercionHandler create(Class JavaDoc type) {
154       CoercionHandler handler = lookup(type);
155       if (handler == null)
156          throw new CoercionException
157             ("no installed handler for type: " + type);
158
159       return handler;
160    }
161 }
162
Popular Tags