1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2008 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // TypeConverter.java
\r
20 // Since: Oct 27, 2008 4:36:50 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/TypeConverter.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.util.bean;
\r
27 import java.io.File;
\r
28 import java.text.DateFormat;
\r
29 import java.text.ParseException;
\r
30 import java.util.Date;
\r
32 import org.xerial.core.XerialErrorCode;
\r
33 import org.xerial.core.XerialException;
\r
36 * Data type converter
\r
41 public class TypeConverter
\r
43 // conversion to the Enum type is tested before the cast
\r
44 @SuppressWarnings("unchecked")
\r
45 public static <T> T convertType(Class<T> targetType, Object value) throws XerialException {
\r
46 if (targetType.isAssignableFrom(value.getClass()) || targetType == Object.class)
\r
49 return (T) convertToBasicType(targetType, value);
\r
53 * Convert the input to the specified type
\r
60 public static <T extends Enum<T>> T convertToEnum(Class<T> targetType, Object input) {
\r
61 assert (targetType.isEnum());
\r
63 String value = input.toString();
\r
65 return Enum.valueOf(targetType, value);
\r
67 catch (IllegalArgumentException e) {
\r
68 // try capitalized value
\r
69 return Enum.valueOf(targetType, value.toString().toUpperCase());
\r
74 * Convert the input to the basic type (int, double, String,... etc.)
\r
79 * @throws XerialException
\r
81 @SuppressWarnings("unchecked")
\r
82 public static Object convertToBasicType(Class< ? > targetType, Object input) throws XerialException {
\r
83 assert (TypeInfo.isBasicType(targetType));
\r
85 if (targetType.isEnum())
\r
86 return convertToEnum((Class<Enum>) targetType, input);
\r
89 String value = input.toString();
\r
91 if (targetType == String.class)
\r
93 else if (targetType == int.class || targetType == Integer.class)
\r
94 return new Integer(value);
\r
95 else if (targetType == long.class || targetType == Long.class)
\r
96 return new Long(value);
\r
97 else if (targetType == double.class || targetType == Double.class)
\r
98 return new Double(value);
\r
99 else if (targetType == float.class || targetType == Float.class)
\r
100 return new Float(value);
\r
101 else if (targetType == short.class || targetType == Short.class)
\r
102 return new Short(value);
\r
103 else if (targetType == boolean.class || targetType == Boolean.class)
\r
104 return new Boolean(value);
\r
105 else if ((targetType == char.class || targetType == Character.class) && value.length() == 1)
\r
106 return new Character(value.charAt(0));
\r
107 else if (targetType == Date.class) {
\r
108 return DateFormat.getDateTimeInstance().parse(value);
\r
110 else if (targetType == File.class) {
\r
111 return new File(value);
\r
113 throw new XerialException(XerialErrorCode.InvalidBeanClass, String.format("%s is not a basic type",
\r
114 targetType.getSimpleName()));
\r
116 catch (NumberFormatException e) {
\r
117 throw new XerialException(XerialErrorCode.InvalidFormat, String.format("%s %s", targetType.getName(), e
\r
120 catch (ParseException e) {
\r
121 throw new XerialException(XerialErrorCode.InvalidDateFormat, e);
\r