org.scijava.util
Class ConversionUtils

java.lang.Object
  extended by org.scijava.util.ConversionUtils

public class ConversionUtils
extends Object

Useful methods for converting and casting between classes and types.

Author:
Curtis Rueden, Mark Hiner

Method Summary
static boolean canCast(Class<?> src, Class<?> dest)
          Checks whether objects of the given class can be cast to the specified type.
static boolean canCast(Object src, Class<?> dest)
          Checks whether the given object can be cast to the specified type.
static boolean canConvert(Class<?> src, Class<?> dest)
          Checks whether objects of the given class can be converted to the specified type.
static boolean canConvert(Class<?> src, Type dest)
          Checks whether objects of the given class can be converted to the specified type.
static boolean canConvert(Object src, Class<?> dest)
          Checks whether the given object can be converted to the specified type.
static boolean canConvert(Object src, Type dest)
          Checks whether the given object can be converted to the specified type.
static
<T> T
cast(Object src, Class<T> dest)
          Casts the given object to the specified type, or null if the types are incompatible.
static
<T> T
convert(Object src, Class<T> dest)
          Converts the given object to an object of the specified type.
static Object convert(Object src, Type dest)
          As convert(Object, Class) but capable of creating and populating multi-element objects (Collections and array types).
static
<T> T
convertToEnum(String src, Class<T> dest)
          Converts the given string value to an enumeration constant of the specified type.
static Class<?> getClass(Type type)
          Gets the raw class corresponding to the given type.
static Class<?> getComponentClass(Type type)
          Gets the component type of the given array type, or null if not an array.
static
<T> Class<T>
getNonprimitiveType(Class<T> type)
          Returns the non-primitive Class closest to the given type.
static
<T> T
getNullValue(Class<T> type)
          Gets the "null" value for the given type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convert

public static Object convert(Object src,
                             Type dest)
As convert(Object, Class) but capable of creating and populating multi-element objects (Collections and array types). If a single element type is provided, it will be converted the same as convert(Object, Class). If a multi-element type is detected, then the value parameter will be interpreted as potential collection of values. An appropriate container will be created, and the full set of values will be type converted and added.

NB: This method should be capable of creating any array type, but if a Collection interface or abstract class is provided we can only make a best guess as to what container type to instantiate. Defaults are provided for Set and List subclasses.

Parameters:
src - The object to convert.
dest - Type to which the object should be converted.

convert

public static <T> T convert(Object src,
                            Class<T> dest)
Converts the given object to an object of the specified type. The object is casted directly if possible, or else a new object is created using the destination type's public constructor that takes the original object as input (except when converting to String, which uses the Object.toString() method instead). In the case of primitive types, returns an object of the corresponding wrapped type. If the destination type does not have an appropriate constructor, returns null.

Type Parameters:
T - Type to which the object should be converted.
Parameters:
src - The object to convert.
dest - Type to which the object should be converted.

convertToEnum

public static <T> T convertToEnum(String src,
                                  Class<T> dest)
Converts the given string value to an enumeration constant of the specified type.

Parameters:
src - The value to convert.
dest - The type of the enumeration constant.
Returns:
The converted enumeration constant, or null if the type is not an enumeration type or has no such constant.

canConvert

public static boolean canConvert(Class<?> src,
                                 Type dest)
Checks whether objects of the given class can be converted to the specified type.

See Also:
convert(Object, Type)

canConvert

public static boolean canConvert(Class<?> src,
                                 Class<?> dest)
Checks whether objects of the given class can be converted to the specified type.

See Also:
convert(Object, Class)

canConvert

public static boolean canConvert(Object src,
                                 Type dest)
Checks whether the given object can be converted to the specified type.

See Also:
convert(Object, Type)

canConvert

public static boolean canConvert(Object src,
                                 Class<?> dest)
Checks whether the given object can be converted to the specified type.

See Also:
convert(Object, Class)

cast

public static <T> T cast(Object src,
                         Class<T> dest)
Casts the given object to the specified type, or null if the types are incompatible.


canCast

public static boolean canCast(Class<?> src,
                              Class<?> dest)
Checks whether objects of the given class can be cast to the specified type.

See Also:
cast(Object, Class)

canCast

public static boolean canCast(Object src,
                              Class<?> dest)
Checks whether the given object can be cast to the specified type.

See Also:
cast(Object, Class)

getNonprimitiveType

public static <T> Class<T> getNonprimitiveType(Class<T> type)
Returns the non-primitive Class closest to the given type.

Specifically, the following type conversions are done:

All other types are unchanged.


getNullValue

public static <T> T getNullValue(Class<T> type)
Gets the "null" value for the given type. For non-primitives, this will actually be null. For primitives, it will be zero for numeric types, false for boolean, and the null character for char.


getClass

public static Class<?> getClass(Type type)
Gets the raw class corresponding to the given type.

If the type is a Class it is simply casted. In the case of a ParameterizedType, then ParameterizedType.getRawType() is returned. Otherwise, returns null.


getComponentClass

public static Class<?> getComponentClass(Type type)
Gets the component type of the given array type, or null if not an array. Supports both regular array types (i.e., Class.getComponentType() if type is a Class) and generic array types (i.e., GenericArrayType.getGenericComponentType() if type is a GenericArrayType).



Copyright © 2009–2014 SciJava. All rights reserved.