001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Convert.java 3691 2007-03-13 22:32:59Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.tools.exceptions.ConversionException;
012: import java.text.ParseException;
013: import java.util.Calendar;
014: import java.util.Date;
015:
016: public class Convert {
017: public static Object toType(Object value, Class target)
018: throws ConversionException {
019: if (null == target)
020: return null;
021:
022: if (target.isPrimitive()) {
023: if (target == boolean.class)
024: return toBoolean(value);
025: if (target == char.class)
026: return toChar(value);
027: if (target == byte.class)
028: return toByte(value);
029: if (target == short.class)
030: return toShort(value);
031: if (target == int.class)
032: return toInt(value);
033: if (target == long.class)
034: return toLong(value);
035: if (target == float.class)
036: return toFloat(value);
037: if (target == double.class)
038: return toDouble(value);
039: }
040:
041: if (null == value)
042: return null;
043:
044: if (target == Boolean.class)
045: return toBoolean(value);
046: if (target == Character.class)
047: return toChar(value);
048: if (target == Byte.class)
049: return toByte(value);
050: if (target == Short.class)
051: return toShort(value);
052: if (target == Integer.class)
053: return toInt(value);
054: if (target == Long.class)
055: return toLong(value);
056: if (target == Float.class)
057: return toFloat(value);
058: if (target == Double.class)
059: return toDouble(value);
060: if (target == Date.class)
061: return toDate(value);
062: if (String.class.isAssignableFrom(target))
063: return toString(value);
064: if (target.isAssignableFrom(value.getClass()))
065: return value;
066:
067: throw new ConversionException(value, target, null);
068: }
069:
070: public static String toString(Object value) {
071: if (null == value) {
072: return null;
073: }
074:
075: return value.toString();
076: }
077:
078: public static char toChar(Object value, char defaultValue) {
079: try {
080: return toChar(value);
081: } catch (ConversionException e) {
082: return defaultValue;
083: }
084: }
085:
086: public static char toChar(Object value) throws ConversionException {
087: if (null == value) {
088: throw new ConversionException(value, char.class, null);
089: }
090:
091: if (value instanceof Character) {
092: return ((Character) value).charValue();
093: }
094:
095: if (value instanceof Number) {
096: int int_value = ((Number) value).intValue();
097: return (char) int_value;
098: }
099:
100: if (value instanceof String && 1 == ((String) value).length()) {
101: return ((String) value).charAt(0);
102: }
103:
104: throw new ConversionException(value, char.class, null);
105: }
106:
107: public static boolean toBoolean(Object value, boolean defaultValue) {
108: try {
109: return toBoolean(value);
110: } catch (ConversionException e) {
111: return defaultValue;
112: }
113: }
114:
115: public static boolean toBoolean(Object value)
116: throws ConversionException {
117: if (null == value) {
118: throw new ConversionException(value, boolean.class, null);
119: }
120:
121: if (value instanceof Boolean) {
122: return ((Boolean) value).booleanValue();
123: }
124:
125: if (value instanceof Number) {
126: byte byte_value = ((Number) value).byteValue();
127: if (0 == byte_value) {
128: return false;
129: }
130:
131: if (1 == byte_value) {
132: return true;
133: }
134:
135: throw new ConversionException(value, boolean.class, null);
136: }
137:
138: if (value instanceof String) {
139: return StringUtils.convertToBoolean((String) value);
140: }
141:
142: throw new ConversionException(value, boolean.class, null);
143: }
144:
145: public static byte toByte(Object value, byte defaultValue) {
146: try {
147: return toByte(value);
148: } catch (ConversionException e) {
149: return defaultValue;
150: }
151: }
152:
153: public static byte toByte(Object value) throws ConversionException {
154: if (null == value) {
155: throw new ConversionException(value, byte.class, null);
156: }
157:
158: if (value instanceof Number) {
159: return ((Number) value).byteValue();
160: }
161:
162: if (value instanceof String) {
163: try {
164: return Byte.parseByte((String) value);
165: } catch (NumberFormatException e) {
166: throw new ConversionException(value, byte.class, e);
167: }
168: }
169:
170: throw new ConversionException(value, byte.class, null);
171: }
172:
173: public static short toShort(Object value, short defaultValue) {
174: try {
175: return toShort(value);
176: } catch (ConversionException e) {
177: return defaultValue;
178: }
179: }
180:
181: public static short toShort(Object value)
182: throws ConversionException {
183: if (null == value) {
184: throw new ConversionException(value, short.class, null);
185: }
186:
187: if (value instanceof Number) {
188: return ((Number) value).shortValue();
189: }
190:
191: if (value instanceof String) {
192: try {
193: return Short.parseShort((String) value);
194: } catch (NumberFormatException e) {
195: throw new ConversionException(value, short.class, e);
196: }
197: }
198:
199: throw new ConversionException(value, short.class, null);
200: }
201:
202: public static int toInt(Object value, int defaultValue) {
203: try {
204: return toInt(value);
205: } catch (ConversionException e) {
206: return defaultValue;
207: }
208: }
209:
210: public static int toInt(Object value) throws ConversionException {
211: if (null == value) {
212: throw new ConversionException(value, int.class, null);
213: }
214:
215: if (value instanceof Number) {
216: return ((Number) value).intValue();
217: }
218:
219: if (value instanceof String) {
220: try {
221: return Integer.parseInt((String) value);
222: } catch (NumberFormatException e) {
223: throw new ConversionException(value, int.class, e);
224: }
225: }
226:
227: throw new ConversionException(value, int.class, null);
228: }
229:
230: public static long toLong(Object value, long defaultValue) {
231: try {
232: return toLong(value);
233: } catch (ConversionException e) {
234: return defaultValue;
235: }
236: }
237:
238: public static long toLong(Object value) throws ConversionException {
239: if (null == value) {
240: throw new ConversionException(value, long.class, null);
241: }
242:
243: if (value instanceof Number) {
244: return ((Number) value).longValue();
245: }
246:
247: if (value instanceof String) {
248: try {
249: return Long.parseLong((String) value);
250: } catch (NumberFormatException e) {
251: throw new ConversionException(value, long.class, e);
252: }
253: }
254:
255: throw new ConversionException(value, long.class, null);
256: }
257:
258: public static float toFloat(Object value, float defaultValue) {
259: try {
260: return toFloat(value);
261: } catch (ConversionException e) {
262: return defaultValue;
263: }
264: }
265:
266: public static float toFloat(Object value)
267: throws ConversionException {
268: if (null == value) {
269: throw new ConversionException(value, float.class, null);
270: }
271:
272: if (value instanceof Number) {
273: return ((Number) value).floatValue();
274: }
275:
276: if (value instanceof String) {
277: try {
278: return Float.parseFloat((String) value);
279: } catch (NumberFormatException e) {
280: throw new ConversionException(value, float.class, e);
281: }
282: }
283:
284: throw new ConversionException(value, float.class, null);
285: }
286:
287: public static double toDouble(Object value, double defaultValue) {
288: try {
289: return toDouble(value);
290: } catch (ConversionException e) {
291: return defaultValue;
292: }
293: }
294:
295: public static double toDouble(Object value)
296: throws ConversionException {
297: if (null == value) {
298: throw new ConversionException(value, double.class, null);
299: }
300:
301: if (value instanceof Number) {
302: return ((Number) value).doubleValue();
303: }
304:
305: if (value instanceof String) {
306: try {
307: return Double.parseDouble((String) value);
308: } catch (NumberFormatException e) {
309: throw new ConversionException(value, double.class, e);
310: }
311: }
312:
313: throw new ConversionException(value, double.class, null);
314: }
315:
316: public static Date toDate(Object value) throws ConversionException {
317: if (null == value) {
318: return null;
319: }
320:
321: if (value instanceof Date) {
322: return (Date) value;
323: }
324:
325: if (value instanceof Number) {
326: return new Date(((Number) value).longValue());
327: }
328:
329: if (value instanceof String) {
330: try {
331: return new Date(Long.parseLong((String) value));
332: } catch (NumberFormatException e) {
333: try {
334: return BeanUtils.getConcisePreciseDateFormat()
335: .parse((String) value);
336: } catch (ParseException e2) {
337: try {
338: return RifeConfig.Tools
339: .getDefaultInputDateFormat().parse(
340: (String) value);
341: } catch (ParseException e3) {
342: throw new ConversionException(value,
343: Date.class, e2);
344: }
345: }
346: }
347: }
348:
349: if (value instanceof Calendar) {
350: return ((Calendar) value).getTime();
351: }
352:
353: throw new ConversionException(value, Date.class, null);
354: }
355: }
|