KickJava   Java API By Example, From Geeks To Geeks.

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


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 number converter. It is able to
17  * convert strings to primitive types or sub-classes of
18  * java.lang.Number. Primitive types are handled by
19  * conversion to the equivalent wrapper class. In most cases
20  * the valueOf method is used on the appropriate wrapper
21  * class in order to convert the string value. Since this
22  * class sub-classes BaseTypeConverter it is able to convert
23  * to and from arrays. See the parent for more information.
24  *
25  * @author Brian Pontarelli
26  */

27 public class NumberTypeConverter extends BaseTypeConverter {
28
29     /**
30      * Converts the string to the given type. If the given type is an array type
31      * then this method returns the result of calling convertStringToArray.
32      * Otherwise, the string is converted to the correct type. If the string passed
33      * in is either null, empty or only whitespace, then this method returns null,
34      * unless the given type is a primitive type, then a wrapper class with the the
35      * default value for the primitive (usually 0) is returned.
36      *
37      * @param value The string value to convert
38      * @param convertTo The type to convert the string to. Primitive types and
39      * sub-classes of java.lang.Number are handled by this class
40      * @return The converted object
41      * @throws TypeConversionException If there was an error converting such as
42      * an NumberFormatException or something similar
43      */

44     public Object JavaDoc convertString(String JavaDoc value, Class JavaDoc convertTo) throws TypeConversionException {
45
46         // If the string is null or empty, then the conversion is null
47
boolean emptyNull = StringTools.isTrimmedEmpty(value);
48         if (emptyNull && !convertTo.isPrimitive()) {
49             return null;
50         } else if (value != null) {
51             value = value.trim();
52         }
53
54         // See if the the type is an array and if so, let BaseTypeConverter handle
55
// the conversion
56
if (convertTo.isArray()) {
57             return super.convertStringToArray(value, null, convertTo);
58         }
59
60         // Not an array so convert the string to the correct type
61
try {
62             if (convertTo == Byte JavaDoc.class || convertTo == Byte.TYPE) {
63                 return (emptyNull) ? new Byte JavaDoc((byte) 0) : Byte.valueOf(value);
64             } else if (convertTo == Short JavaDoc.class || convertTo == Short.TYPE) {
65                 return (emptyNull) ? new Short JavaDoc((short) 0) : Short.valueOf(value);
66             } else if (convertTo == Integer JavaDoc.class || convertTo == Integer.TYPE) {
67                 return (emptyNull) ? new Integer JavaDoc(0) : Integer.valueOf(value);
68             } else if (convertTo == Long JavaDoc.class || convertTo == Long.TYPE) {
69                 return (emptyNull) ? new Long JavaDoc(0l) : Long.valueOf(value);
70             } else if (convertTo == Float JavaDoc.class || convertTo == Float.TYPE) {
71                 return (emptyNull) ? new Float JavaDoc(0.0f) : Float.valueOf(value);
72             } else if (convertTo == Double JavaDoc.class || convertTo == Double.TYPE) {
73                 return (emptyNull) ? new Double JavaDoc(0.0): Double.valueOf(value);
74             }
75         } catch (Exception JavaDoc e) {
76             throw new TypeConversionException("Error converting to type: " + convertTo.getName(), e);
77         }
78
79         throw new TypeConversionException("Type: " + convertTo.getName()
80             + " not supported by the NumberTypeConverter");
81     }
82
83     /**
84      * This method only handles the case where values is null or zero length and the
85      * convertTo type is the primitive boolean type. In this case, this method returns
86      * the initial value for that type (false). If this is not the case, this method
87      * calls {@link BaseTypeConverter#convertArray(Object[],Class)
88      * super.convertArray(...)}.
89      *
90      * @param values The array of values to convert to the given type
91      * @param convertTo The type to convert to
92      * @return The converted type
93      * @throws TypeConversionException If there was a problem while calling the super
94      * class method.
95      */

96     public Object JavaDoc convertArray(Object JavaDoc [] values, Class JavaDoc convertTo) throws TypeConversionException {
97
98         // If the values is null, no use in converting it
99
if ((values == null || values.length == 0) && convertTo.isPrimitive()) {
100             return convertString(null, convertTo);
101         }
102
103         return super.convertArray(values, convertTo);
104     }
105 }
106
Popular Tags