001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jCommonTk.
006: *
007: * jCommonTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jCommonTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jCommonTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jcommontk.object;
020:
021: /**
022: * Utility for converting type to type.
023: */
024:
025: @SuppressWarnings("unchecked")
026: // working to complete a Java 1.5 version
027: public class ObjectConverter {
028: /**
029: * Converts value to an instance of class parameterClass.
030: *
031: * @param parameterClass class to convert to
032: * @param value value to convert
033: * @return a new instance of parameterClass with the given value
034: */
035:
036: public static <T> T convertObject(Class<T> parameterClass,
037: Object value) {
038: Object newValue = value;
039:
040: if (value != null) {
041: if (!parameterClass.isAssignableFrom(value.getClass()))
042: if (parameterClass.isAssignableFrom(String.class))
043: newValue = value.toString();
044: else if (parameterClass.isAssignableFrom(int.class)
045: || parameterClass
046: .isAssignableFrom(Integer.class))
047: newValue = value instanceof Number ? new Integer(
048: ((Number) value).intValue()) : new Integer(
049: value.toString());
050: else if (parameterClass.isAssignableFrom(short.class)
051: || parameterClass.isAssignableFrom(Short.class))
052: newValue = value instanceof Number ? new Short(
053: ((Number) value).shortValue()) : new Short(
054: value.toString());
055: else if (parameterClass.isAssignableFrom(long.class)
056: || parameterClass.isAssignableFrom(Long.class))
057: newValue = value instanceof Number ? new Long(
058: ((Number) value).longValue()) : new Long(
059: value.toString());
060: else if (parameterClass.isAssignableFrom(float.class)
061: || parameterClass.isAssignableFrom(Float.class))
062: newValue = value instanceof Number ? new Float(
063: ((Number) value).floatValue()) : new Float(
064: value.toString());
065: else if (parameterClass.isAssignableFrom(double.class)
066: || parameterClass
067: .isAssignableFrom(Double.class))
068: newValue = value instanceof Number ? new Double(
069: ((Number) value).doubleValue())
070: : new Double(value.toString());
071: else if (parameterClass.isAssignableFrom(boolean.class)
072: || parameterClass
073: .isAssignableFrom(Boolean.class)) {
074: String str = value.toString().toLowerCase();
075:
076: newValue = str.equals("on") || str.equals("true")
077: || str.equals("yes") || str.equals("1") ? Boolean.TRUE
078: : Boolean.FALSE;
079: } else if (parameterClass.isAssignableFrom(byte.class)
080: || parameterClass.isAssignableFrom(Byte.class))
081: newValue = new Byte(value.toString());
082: else if ((parameterClass.isAssignableFrom(char.class) || parameterClass
083: .isAssignableFrom(Character.class))
084: && value.toString().length() > 0)
085: newValue = new Character(value.toString().charAt(0));
086: } else if (parameterClass.isPrimitive()) {
087: if (parameterClass.isAssignableFrom(int.class))
088: newValue = new Integer(0);
089: else if (parameterClass.isAssignableFrom(short.class))
090: newValue = new Short((short) 0);
091: else if (parameterClass.isAssignableFrom(long.class))
092: newValue = new Long(0);
093: else if (parameterClass.isAssignableFrom(float.class))
094: newValue = new Float(0);
095: else if (parameterClass.isAssignableFrom(double.class))
096: newValue = new Double(0);
097: else if (parameterClass.isAssignableFrom(boolean.class))
098: newValue = Boolean.FALSE;
099: else if (parameterClass.isAssignableFrom(byte.class))
100: newValue = new Byte((byte) 0);
101: else if (parameterClass.isAssignableFrom(char.class))
102: newValue = new Character((char) 0);
103: }
104:
105: return (T) newValue;
106: }
107: }
|