Generic Object Converter
This generic ObjectConverter makes use of the reflection and the generics to convert the given object value to the given type class. This utility class may look like overkilled, but it is very useful if you don't always know the type of the given object value before. BalusC uses it in his home-made ORM to convert unmatched types from the ResultSet#getObject() in DTO#setSomething().
/* * net/balusc/util/ObjectConverter.java * * Copyright (C) 2007 BalusC * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; if * not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ package net.balusc.util; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; /** * Generic object converter. * <p> * <h3>Use examples</h3> * * <pre> * Object o1 = Boolean.TRUE; * Integer i = ObjectConverter.convert(o1, Integer.class); * System.out.println(i); // 1 * * Object o2 = "false"; * Boolean b = ObjectConverter.convert(o2, Boolean.class); * System.out.println(b); // false * * Object o3 = new Integer(123); * String s = ObjectConverter.convert(o3, String.class); * System.out.println(s); // 123 * </pre> * * Not all possible conversions are implemented. You can extend the <tt>ObjectConverter</tt> * easily by just adding a new method to it, with the appropriate logic. For example: * * <pre> * public static ToObject fromObjectToObject(FromObject fromObject) { * // Implement. * } * </pre> * * The method name doesn't matter. It's all about the parameter type and the return type. * * @author BalusC * @link http://balusc.blogspot.com/2007/08/generic-object-converter.html */ public final class ObjectConverter { // Init --------------------------------------------------------------------------------------- private static final Map<String, Method> CONVERTERS = new HashMap<String, Method>(); static { // Preload converters. Method[] methods = ObjectConverter.class.getDeclaredMethods(); for (Method method : methods) { if (method.getParameterTypes().length != 1) { // Converter should accept 1 argument. This skips the convert() method. continue; } CONVERTERS.put(method.getParameterTypes()[0].getName() + "_" + method.getReturnType().getName(), method); } } private ObjectConverter() { // Utility class, hide the constructor. } // Action ------------------------------------------------------------------------------------- /** * Convert the given object value to the given class. * @param from The object value to be converted. * @param to The type class where the given object should be converted to. * @return The converted object value. * @throws NullPointerException If one or both of the arguments is null. * @throws UnsupportedOperationException If no suitable converter can be found. * @throws RuntimeException If conversion failed somehow. This can be caused by at least * an ExceptionInInitializerError, IllegalAccessException or InvocationTargetException. */ @SuppressWarnings("unchecked") public static <T extends Object> T convert(Object from, Class<T> to) { // Precheck. if (from == null || to == null) { throw new NullPointerException("Cannot convert from " + from + " to " + to + ". One or both of the arguments is null."); } // Can we cast? Then just do it. if (to.isAssignableFrom(from.getClass())) { return (T) from; } // Lookup the suitable converter. String converterId = from.getClass().getName() + "_" + to.getName(); Method converter = CONVERTERS.get(converterId); if (converter == null) { throw new UnsupportedOperationException("Cannot convert from " + from.getClass().getName() + " to " + to.getName() + ". Requested converter does not exist."); } // Convert the value. try { return (T) converter.invoke(to, new Object[] { from }); } catch (Exception e) { throw new RuntimeException("Cannot convert from " + from.getClass().getName() + " to " + to.getName() + ". Conversion failed with " + e.getMessage(), e); } } // Converters --------------------------------------------------------------------------------- /** * Converts Integer to Long. * @param value The Integer to be converted. * @return The converted Long value. */ public static Long integerToLong(Integer value) { return new Long(value.longValue()); } /** * Converts Long to Integer. * @param value The Integer to be converted to Long. * @return The converted Long value of the Integer. */ public static Integer longToInteger(Long value) { return new Integer(value.intValue()); } /** * Converts Integer to Boolean. If integer value is 0, then return FALSE, else return TRUE. * @param value The Integer to be converted. * @return The converted Boolean value. */ public static Boolean integerToBoolean(Integer value) { return value.intValue() != 0 ? new Boolean(true) : new Boolean(false); } /** * Converts Boolean to Integer. If boolean value is TRUE, then return 1, else return 0. * @param value The Boolean to be converted. * @return The converted Integer value. */ public static Integer booleanToInteger(Boolean value) { return value.booleanValue() ? new Integer(1) : new Integer(0); } /** * Converts Double to BigDecimal. * @param value The Double to be converted. * @return The converted BigDecimal value. */ public static BigDecimal doubleToBigDecimal(Double value) { return new BigDecimal(value.doubleValue()); } /** * Converts BigDecimal to Double. * @param value The BigDecimal to be converted. * @return The converted Double value. */ public static Double bigDecimalToDouble(BigDecimal value) { return new Double(value.doubleValue()); } /** * Converts Integer to String. * @param value The Integer to be converted. * @return The converted String value. */ public static String integerToString(Integer value) { return value.toString(); } /** * Converts String to Integer. * @param value The String to be converted. * @return The converted Integer value. */ public static Integer stringToInteger(String value) { return new Integer(value); } /** * Converts Boolean to String. * @param value The Boolean to be converted. * @return The converted String value. */ public static String booleanToString(Boolean value) { return value.toString(); } /** * Converts String to Boolean. * @param value The String to be converted. * @return The converted Boolean value. */ public static Boolean stringToBoolean(String value) { return new Boolean(value); } /** * Converts char array to String. * @param value The char array to be converted. * @return The converted String. */ public static String charsToString(char[] value) { return new String(value); } /** * Converts String to char array. * @param value The String to be converted. * @return The converted char array. */ public static char[] stringToChars(String value) { return value.toCharArray(); } // You can implement more converter methods here. }
Copyright - GNU General Public License
(C) August 2007, BalusC
