org.sadun.util
Class TypeWrapper

java.lang.Object
  extended byorg.sadun.util.TypeWrapper

public class TypeWrapper
extends java.lang.Object

An helper class to automatically wrap primitive types into their corresponding object types.

Author:
Cristiano Sadun

Constructor Summary
TypeWrapper()
           
 
Method Summary
static java.lang.Object convertValue(java.lang.Object value, java.lang.Class targetType)
          Convert the given value to the given target type, or throws a IllegalArgumentException if no conversion is possible
static java.lang.Class getUnwrappedPrimitiveType(java.lang.Class wrapperCls)
          Return the primitive type class for a wrapper type.
static java.lang.Class getWrapperClass(java.lang.Class primitiveCls)
          Return the class of the wrapper object for the given primitive type.
static boolean isConvertible(java.lang.Class srcType, java.lang.Class destType)
          Verifies if a source type can be legally promoted to a destination type.
static boolean isPrimitiveOrWrapper(java.lang.Class cls)
           
static boolean isPrimitiveWrapper(java.lang.Class cls)
           
static void main(java.lang.String[] args)
           
static java.lang.Object wrap(boolean v)
           
static java.lang.Object wrap(byte v)
           
static java.lang.Object wrap(char v)
           
static java.lang.Object wrap(double v)
           
static java.lang.Object wrap(float v)
           
static java.lang.Object wrap(int v)
           
static java.lang.Object wrap(long v)
           
static java.lang.Object wrap(java.lang.Object obj)
           
static java.lang.Object wrap(short v)
           
static java.lang.Object wrapFromString(java.lang.Class primitiveCls, java.lang.String value)
          Return a wrapper object corresponding to passed value, interpreted according to the given primitive (or primtitive wrapper) class .
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeWrapper

public TypeWrapper()
Method Detail

wrap

public static java.lang.Object wrap(byte v)

wrap

public static java.lang.Object wrap(short v)

wrap

public static java.lang.Object wrap(int v)

wrap

public static java.lang.Object wrap(long v)

wrap

public static java.lang.Object wrap(float v)

wrap

public static java.lang.Object wrap(double v)

wrap

public static java.lang.Object wrap(char v)

wrap

public static java.lang.Object wrap(boolean v)

wrap

public static java.lang.Object wrap(java.lang.Object obj)

getWrapperClass

public static java.lang.Class getWrapperClass(java.lang.Class primitiveCls)
Return the class of the wrapper object for the given primitive type.

Parameters:
primitiveCls - the Class of the primitive type
Returns:
Class the wrapper class

wrapFromString

public static java.lang.Object wrapFromString(java.lang.Class primitiveCls,
                                              java.lang.String value)
Return a wrapper object corresponding to passed value, interpreted according to the given primitive (or primtitive wrapper) class .

Parameters:
primitiveCls - the Class of the primitive (or wrapper) type
value - the value to intepret

getUnwrappedPrimitiveType

public static java.lang.Class getUnwrappedPrimitiveType(java.lang.Class wrapperCls)
Return the primitive type class for a wrapper type. If the given type is not a wrapper, an IllegalArgumentExcepion is thrown.

Parameters:
wrapperCls -
Returns:

isPrimitiveWrapper

public static boolean isPrimitiveWrapper(java.lang.Class cls)

isPrimitiveOrWrapper

public static boolean isPrimitiveOrWrapper(java.lang.Class cls)

main

public static void main(java.lang.String[] args)

isConvertible

public static boolean isConvertible(java.lang.Class srcType,
                                    java.lang.Class destType)
Verifies if a source type can be legally promoted to a destination type. For example, an int can be promoted to long, but not vice versa.

Returns:

convertValue

public static java.lang.Object convertValue(java.lang.Object value,
                                            java.lang.Class targetType)
Convert the given value to the given target type, or throws a IllegalArgumentException if no conversion is possible

Parameters:
value - the value to convert
targetType - the type to convert to