KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.util.CoercionException;
13 import org.jboss.util.NotCoercibleException;
14
15 /**
16  * A Character coercion handler.
17  *
18  * @version <tt>$Revision: 1.1 $</tt>
19  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
20  */

21 public class CharacterHandler
22    extends BoundCoercionHandler
23 {
24    /**
25     * Get the target class type for this CoercionHandler.
26     *
27     * @return Class type
28     */

29    public Class JavaDoc getType() {
30       return Character JavaDoc.class;
31    }
32
33    /**
34     * Coerces the given value into the given type (which should be
35     * Character.class).
36     *
37     * <p>This currently only support coercion from a String.
38     *
39     * @param value Value to coerce
40     * @param type Character.class
41     * @return Value coerced into a Character
42     *
43     * @throws CoercionException Failed to coerce
44     */

45    public Object JavaDoc coerce(Object JavaDoc value, Class JavaDoc type) throws CoercionException {
46       if (value.getClass().equals(String JavaDoc.class)) {
47          return coerce((String JavaDoc)value);
48       }
49       
50       throw new NotCoercibleException(value);
51    }
52
53    /**
54     * Coerces the given string into a Character, by taking off the first
55     * index of the string and wrapping it.
56     *
57     * @param value String value to convert to a Character
58     * @return Character value or null if the string is empty.
59     */

60    public Object JavaDoc coerce(String JavaDoc value) {
61       char[] temp = value.toCharArray();
62       if (temp.length == 0) {
63          return null;
64       }
65       return new Character JavaDoc(temp[0]);
66    }
67 }
68
69
Popular Tags