KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typeconverter > converters > CharacterTypeConverter


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typeconverter.converters;
8
9
10 import com.inversoft.util.StringTools;
11 import com.inversoft.util.typeconverter.BaseTypeConverter;
12 import com.inversoft.util.typeconverter.TypeConversionException;
13
14
15 /**
16  * This class is the main character converter. It is able to
17  * convert strings to the java.lnag.Character wrapper class.
18  * This is done by simply taking the string and transferring
19  * the single character to a Character object. This fails if
20  * the String is longer than one character though. Since this
21  * class sub-classes BaseTypeConverter it is able to convert
22  * to and from arrays. See the parent for more information.
23  *
24  * @author Brian Pontarelli
25  */

26 public class CharacterTypeConverter extends BaseTypeConverter {
27
28     /**
29      * Converts the string to the given type. This method first calls the parents
30      * convertString method to handle the empty and array cases. If the parent
31      * method returns value, then this method will convert the string to a
32      * character. If the string is longer than one character an exception is thrown.
33      * If the convertTo type is a primitive and the value string is null or empty,
34      * then this method returns a new Character wrapper set to the initial value
35      * of the primitive ()
36      *
37      * @param value The string value to convert
38      * @param convertTo The type to convert the string to (ignored except if array)
39      * @return The converted object
40      * @throws TypeConversionException If the string is longer than one character or
41      * the type is an array and the value is invalid
42      */

43     public Object JavaDoc convertString(String JavaDoc value, Class JavaDoc convertTo) throws TypeConversionException {
44
45         boolean empty = StringTools.isTrimmedEmpty(value);
46         if (empty && convertTo == Character.TYPE) {
47             return new Character JavaDoc('\u0000');
48         } else if (empty) {
49             return null;
50         }
51
52         if (convertTo.isArray()) {
53             return convertToArray(value, convertTo);
54         }
55
56         // Check the length
57
if (value.length() > 1) {
58             throw new TypeConversionException("Setter expected a single" +
59                 " character but receieved string with value: " + value);
60         }
61
62         return new Character JavaDoc(value.charAt(0));
63     }
64
65     /**
66      * This method only handles the case where values is null or zero length and the
67      * convertTo type is the primitive boolean type. In this case, this method returns
68      * the initial value for that type (false). If this is not the case, this method
69      * calls {@link BaseTypeConverter#convertArray(Object[],Class)
70      * super.convertArray(...)}.
71      *
72      * @param values The array of values to convert to the given type
73      * @param convertTo The type to convert to
74      * @return The converted type
75      * @throws TypeConversionException If there was a problem while calling the super
76      * class method.
77      */

78     public Object JavaDoc convertArray(Object JavaDoc [] values, Class JavaDoc convertTo)
79     throws TypeConversionException {
80
81         // If the values is null, no use in converting it
82
if ((values == null || values.length == 0) && convertTo == Character.TYPE) {
83             return new Character JavaDoc('\u0000');
84         }
85
86         return super.convertArray(values, convertTo);
87     }
88 }
89
Popular Tags