net.balusc.util
Class ObjectConverter

java.lang.Object
  extended by net.balusc.util.ObjectConverter

public final class ObjectConverter
extends Object

Generic object converter.

Use examples

 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
 
Not all possible conversions are implemented. You can extend the ObjectConverter easily by just adding a new method to it, with the appropriate logic. For example:
 public static ToObject fromObjectToObject(FromObject fromObject) {
   // Implement.
 }
 
The method name doesn't matter. It's all about the parameter type and the return type.

Author:
BalusC

Method Summary
static Double bigDecimalToDouble(BigDecimal value)
          Converts BigDecimal to Double.
static Integer booleanToInteger(Boolean value)
          Converts Boolean to Integer.
static String booleanToString(Boolean value)
          Converts Boolean to String.
static
<T> T
convert(Object from, Class<T> to)
          Convert the given object value to the given class.
static BigDecimal doubleToBigDecimal(Double value)
          Converts Double to BigDecimal.
static Float doubleToFloat(Double value)
          Converts Double to Float.
static double doubleToPrimitive(Double value)
          Converts Long to primitive.
static double floatToPrimitive(Float value)
          Converts Float to primitive.
static Boolean integerToBoolean(Integer value)
          Converts Integer to Boolean.
static int integerToPrimitive(Integer value)
          Converts Integer to primitive.
static String integerToString(Integer value)
          Converts Integer to String.
static Integer longToInteger(Long value)
          Converts Long to Integer.
static long longToPrimitive(Long value)
          Converts Long to primitive.
static String longToString(Date value)
          Converts Date to String.
static String longToString(Double value)
          Converts Double to String.
static String longToString(Long value)
          Converts Long to String.
static Boolean stringToBoolean(String value)
          Converts String to Boolean.
static Double stringToDouble(String value)
          Converts String to Float.
static Float stringToFloat(String value)
          Converts String to Float.
static Integer stringToInteger(String value)
          Converts String to Integer.
static Long stringToLong(String value)
          Converts String to Long.
static long stringToLongPrimitive(String value)
          Converts String to long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convert

public static <T> T convert(Object from,
                            Class<T> to)
Convert the given object value to the given class.

Parameters:
from - The object value to be converted.
to - The type class which the given object should be converted to.
Returns:
The converted object value.
Throws:
NullPointerException - If 'to' is null.
UnsupportedOperationException - If no suitable converter can be found.
RuntimeException - If conversion failed somehow. This can be caused by at least an ExceptionInInitializerError, IllegalAccessException or InvocationTargetException.

integerToBoolean

public static Boolean integerToBoolean(Integer value)
Converts Integer to Boolean. If integer value is 0, then return FALSE, else return TRUE.

Parameters:
value - The Integer to be converted.
Returns:
The converted Boolean value.

booleanToInteger

public static Integer booleanToInteger(Boolean value)
Converts Boolean to Integer. If boolean value is TRUE, then return 1, else return 0.

Parameters:
value - The Boolean to be converted.
Returns:
The converted Integer value.

doubleToBigDecimal

public static BigDecimal doubleToBigDecimal(Double value)
Converts Double to BigDecimal.

Parameters:
value - The Double to be converted.
Returns:
The converted BigDecimal value.

bigDecimalToDouble

public static Double bigDecimalToDouble(BigDecimal value)
Converts BigDecimal to Double.

Parameters:
value - The BigDecimal to be converted.
Returns:
The converted Double value.

integerToString

public static String integerToString(Integer value)
Converts Integer to String.

Parameters:
value - The Integer to be converted.
Returns:
The converted String value.

stringToInteger

public static Integer stringToInteger(String value)
Converts String to Integer.

Parameters:
value - The String to be converted.
Returns:
The converted Integer value.

stringToLong

public static Long stringToLong(String value)
Converts String to Long.

Parameters:
value - The String to be converted.
Returns:
The converted Long value.

stringToLongPrimitive

public static long stringToLongPrimitive(String value)
Converts String to long.

Parameters:
value - The String to be converted.
Returns:
The converted long value.

booleanToString

public static String booleanToString(Boolean value)
Converts Boolean to String.

Parameters:
value - The Boolean to be converted.
Returns:
The converted String value.

stringToBoolean

public static Boolean stringToBoolean(String value)
Converts String to Boolean.

Parameters:
value - The String to be converted.
Returns:
The converted Boolean value.

longToInteger

public static Integer longToInteger(Long value)
Converts Long to Integer.

Parameters:
value - The Long value to be convered.
Returns:
Converted Integer value.

doubleToFloat

public static Float doubleToFloat(Double value)
Converts Double to Float.

Parameters:
value - The Double value to be convered.
Returns:
Converted Float value.

stringToFloat

public static Float stringToFloat(String value)
Converts String to Float.

Parameters:
value - The Float value to be convered.
Returns:
Converted String value.

stringToDouble

public static Double stringToDouble(String value)
Converts String to Float.

Parameters:
value - The Float value to be convered.
Returns:
Converted String value.

longToPrimitive

public static long longToPrimitive(Long value)
Converts Long to primitive.

Parameters:
value - The Long value to be convered.
Returns:
Converted primitive value.

longToString

public static String longToString(Long value)
Converts Long to String.

Parameters:
value - The Long value to be converted.
Returns:
Converted String value.

longToString

public static String longToString(Double value)
Converts Double to String.

Parameters:
value - The Double value to be converted.
Returns:
Converted String value.

longToString

public static String longToString(Date value)
Converts Date to String.

Parameters:
value - The value to be converted.
Returns:
Converted value.

integerToPrimitive

public static int integerToPrimitive(Integer value)
Converts Integer to primitive.

Parameters:
value - The Integer value to be convered.
Returns:
Converted primitive value.

doubleToPrimitive

public static double doubleToPrimitive(Double value)
Converts Long to primitive.

Parameters:
value - The Long value to be convered.
Returns:
Converted primitive value.

floatToPrimitive

public static double floatToPrimitive(Float value)
Converts Float to primitive.

Parameters:
value - The Float value to be convered.
Returns:
Converted primitive value.


Copyright © 2012. All Rights Reserved.