OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / main / java / org / xerial / util / bean / TypeConverter.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2008 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // TypeConverter.java\r
20 // Since: Oct 27, 2008 4:36:50 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/bean/TypeConverter.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.util.bean;\r
26 \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
31 \r
32 import org.xerial.core.XerialErrorCode;\r
33 import org.xerial.core.XerialException;\r
34 \r
35 /**\r
36  * Data type converter\r
37  * \r
38  * @author leo\r
39  * \r
40  */\r
41 public class TypeConverter\r
42 {\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
47             return (T) value;\r
48         else\r
49             return (T) convertToBasicType(targetType, value);\r
50     }\r
51 \r
52     /**\r
53      * Convert the input to the specified type\r
54      * \r
55      * @param <T>\r
56      * @param targetType\r
57      * @param input\r
58      * @return\r
59      */\r
60     public static <T extends Enum<T>> T convertToEnum(Class<T> targetType, Object input) {\r
61         assert (targetType.isEnum());\r
62 \r
63         String value = input.toString();\r
64         try {\r
65             return Enum.valueOf(targetType, value);\r
66         }\r
67         catch (IllegalArgumentException e) {\r
68             // try capitalized value\r
69             return Enum.valueOf(targetType, value.toString().toUpperCase());\r
70         }\r
71     }\r
72 \r
73     /**\r
74      * Convert the input to the basic type (int, double, String,... etc.)\r
75      * \r
76      * @param targetType\r
77      * @param input\r
78      * @return\r
79      * @throws XerialException\r
80      */\r
81     @SuppressWarnings("unchecked")\r
82     public static Object convertToBasicType(Class< ? > targetType, Object input) throws XerialException {\r
83         assert (TypeInfo.isBasicType(targetType));\r
84 \r
85         if (targetType.isEnum())\r
86             return convertToEnum((Class<Enum>) targetType, input);\r
87 \r
88         try {\r
89             String value = input.toString();\r
90 \r
91             if (targetType == String.class)\r
92                 return value;\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
109             }\r
110             else if (targetType == File.class) {\r
111                 return new File(value);\r
112             }\r
113             throw new XerialException(XerialErrorCode.InvalidBeanClass, String.format("%s is not a basic type",\r
114                     targetType.getSimpleName()));\r
115         }\r
116         catch (NumberFormatException e) {\r
117             throw new XerialException(XerialErrorCode.InvalidFormat, String.format("%s %s", targetType.getName(), e\r
118                     .getMessage()));\r
119         }\r
120         catch (ParseException e) {\r
121             throw new XerialException(XerialErrorCode.InvalidDateFormat, e);\r
122         }\r
123     }\r
124 }\r