001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/util/TypeConverter.java,v 1.4 2003/08/30 17:34:52 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.util;
021:
022: import java.math.BigDecimal;
023: import java.text.ParseException;
024: import java.text.SimpleDateFormat;
025: import java.util.Date;
026:
027: /**
028: * Contains static methods for converting between various Java types.
029: */
030: public class TypeConverter {
031: /**
032: * Calls through to the three-argument version of this method,
033: * using null as the value for the format String.
034: */
035: public static Object convertToType(Object input, Class clazz) {
036: return convertToType(input, clazz, null);
037: }
038:
039: /**
040: * Converts the input object into a return value of the type
041: * requested. Currently, the following classes are supported:
042: * String, Integer, Long, BigDecimal, Float, Double, Boolean.
043: * Dates will be converted according to the format string,
044: * using the syntax for java.text.SimpleDateFormat.
045: */
046: public static Object convertToType(Object input, Class clazz,
047: String format) {
048: if (input == null)
049: return null;
050: if (clazz.isInstance(input))
051: return input;
052: if (String.class.equals(clazz)) {
053: if (Date.class.equals(input.getClass())) {
054: if (format != null) {
055: SimpleDateFormat dateFormat = new SimpleDateFormat(
056: format);
057: return dateFormat.format(input);
058: }
059: }
060: return input.toString();
061: }
062: if (input instanceof Number) {
063: Number number = (Number) input;
064: if (BigDecimal.class.equals(clazz)) {
065: return BigDecimal.valueOf(number.longValue());
066: }
067: if (Long.class.equals(clazz) || Long.TYPE.equals(clazz)) {
068: return new Long(number.longValue());
069: }
070: if (Integer.class.equals(clazz)
071: || Integer.TYPE.equals(clazz)) {
072: return new Integer(number.intValue());
073: }
074: } else if (input instanceof String) {
075: String str = (String) input;
076: if (BigDecimal.class.equals(clazz)) {
077: return new BigDecimal(str);
078: }
079: if (Long.class.equals(clazz) || Long.TYPE.equals(clazz)) {
080: return Long.valueOf(str);
081: }
082: if (Integer.class.equals(clazz)
083: || Integer.TYPE.equals(clazz)) {
084: return Integer.valueOf(str);
085: }
086: if (Double.class.equals(clazz) || Double.TYPE.equals(clazz)) {
087: return Double.valueOf(str);
088: }
089: if (Float.class.equals(clazz) || Float.TYPE.equals(clazz)) {
090: return Float.valueOf(str);
091: }
092: if (Boolean.class.equals(clazz)
093: || Boolean.TYPE.equals(clazz)) {
094: return Boolean.valueOf(str);
095: }
096: if (Date.class.equals(clazz)) {
097: if (format != null) {
098: SimpleDateFormat dateFormat = new SimpleDateFormat(
099: format);
100: try {
101: return dateFormat.parse((String) input);
102: } catch (ParseException e) {
103: // TODO use real logging
104: e.printStackTrace();
105: }
106: }
107: }
108: }
109:
110: // Otherwise not handled
111: return input;
112: }
113: }
|