KickJava   Java API By Example, From Geeks To Geeks.

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


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 type covnerter for booleans. It only
17  * contains one method that converts a string to a boolean.
18  * All other conversion is handled by the BaseTypeConverter
19  * class. Therefore, this class will handle all the conversions
20  * described in the TypeConverter interface except conversion
21  * from an array to an object when the array has a length
22  * greater than one. This is the same deficiency that
23  * BaseTypeConverter posses.
24  *
25  * @author Brian Pontarelli
26  */

27 public class BooleanTypeConverter extends BaseTypeConverter {
28
29     /**
30      * Converts the given string to a boolean. If the convertTo type is Boolean.TYPE,
31      * then this method will convert the string to a Boolean object and NEVER
32      * return null. This way there are no exceptions when attempting to set a
33      * primitive type to null. If the convertTo type is an array, then this
34      * method calls convertToArray. If the convertTo type is not Boolean.TYPE
35      * it is ignored. If the convertTo type is not Boolean.TYPE and the passed
36      * in string is either null, empty or contains only white space, this method
37      * returns null.
38      *
39      * @param value The string value to convert
40      * @param convertTo The type to convert the string to should be either
41      * Boolean.class or Boolean.TYPE, or an array of either one of those.
42      * @return A Boolean object containing the boolean value of the string (which
43      * must conform to the java.lang.Boolean#valueOf() method) or a false
44      * Boolean obect if the convertTo type is Boolean.TYPE and the string
45      * is null, empty or only whitespace.
46      * @throws TypeConversionException If the value is an invalid boolean
47      * expression or if the convertTo type is an array and there
48      * was a problem converting the string to an array
49      */

50     public Object JavaDoc convertString(String JavaDoc value, Class JavaDoc convertTo) throws TypeConversionException {
51
52         // If the string is null or empty, return null
53
if (StringTools.isTrimmedEmpty(value)) {
54             if (convertTo == Boolean.TYPE) {
55                 return Boolean.FALSE;
56             }
57
58             return null;
59         } else {
60             value = value.trim();
61         }
62
63         // See if the the type is an array and if so, let BaseTypeConverter handle
64
// the conversion
65
if (convertTo.isArray()) {
66             return convertToArray(value, convertTo.getComponentType());
67         }
68
69         // Check if it is a valid boolean string
70
if (StringTools.isValidBoolean(value)) {
71             return Boolean.valueOf(value);
72         }
73
74         throw new TypeConversionException(value + " is not a valid boolean type");
75     }
76
77     /**
78      * This method only handles the case where values is null or zero length and the
79      * convertTo type is the primitive boolean type. In this case, this method returns
80      * the initial value for that type (false). If this is not the case, this method
81      * calls {@link BaseTypeConverter#convertArray(Object[], Class)
82      * super.convertArray(...)}.
83      *
84      * @param values The array of values to convert to the given type
85      * @param convertTo The type to convert to
86      * @return The converted type
87      * @throws TypeConversionException If there was a problem while calling the super
88      * class method.
89      */

90     public Object JavaDoc convertArray(Object JavaDoc [] values, Class JavaDoc convertTo)
91     throws TypeConversionException {
92
93         // If the values is null, no use in converting it
94
if ((values == null || values.length == 0) && convertTo == Boolean.TYPE) {
95             return Boolean.FALSE;
96         }
97
98         return super.convertArray(values, convertTo);
99     }
100 }
Popular Tags