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.

For extensible type conversion, use ConvertService.

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)
          Deprecated.  
static boolean canConvert(Class<?> src, Type dest)
          Deprecated.  
static boolean canConvert(Object src, Class<?> dest)
          Deprecated.  
static boolean canConvert(Object src, Type dest)
          Deprecated.  
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)
          Deprecated.  
static Object convert(Object src, Type dest)
          Deprecated.  
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)
          Deprecated. use GenericUtils.getClass(Type)
static Class<?> getComponentClass(Type type)
          Deprecated. use GenericUtils.getComponentClass(Type)
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.
static void setDelegateService(ConvertService convertService, double priority)
          Sets the ConvertService to use for handling conversion requests.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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.

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.

Returns:
true If the destination class is assignable from the source one, or if the source class is null and destination class is non-null.
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.

Returns:
true If the destination class is assignable from the source object's class, or if the source object is null and destionation class is non-null.
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.


setDelegateService

public static void setDelegateService(ConvertService convertService,
                                      double priority)
Sets the ConvertService to use for handling conversion requests.


convert

@Deprecated
public static Object convert(Object src,
                                        Type dest)
Deprecated. 

See Also:
Converter.convert(Object, Type)

convert

@Deprecated
public static <T> T convert(Object src,
                                       Class<T> dest)
Deprecated. 

See Also:
Converter.convert(Object, Class)

canConvert

@Deprecated
public static boolean canConvert(Class<?> src,
                                            Type dest)
Deprecated. 

See Also:
Converter.canConvert(Class, Type)

canConvert

@Deprecated
public static boolean canConvert(Class<?> src,
                                            Class<?> dest)
Deprecated. 

See Also:
Converter.canConvert(Class, Class)

canConvert

@Deprecated
public static boolean canConvert(Object src,
                                            Type dest)
Deprecated. 

See Also:
Converter.canConvert(Object, Type)

canConvert

@Deprecated
public static boolean canConvert(Object src,
                                            Class<?> dest)
Deprecated. 

See Also:
Converter.canConvert(Object, Class)

getClass

@Deprecated
public static Class<?> getClass(Type type)
Deprecated. use GenericUtils.getClass(Type)


getComponentClass

@Deprecated
public static Class<?> getComponentClass(Type type)
Deprecated. use GenericUtils.getComponentClass(Type)



Copyright © 2009–2015 SciJava. All rights reserved.