org.norther.tammi.acorn.type
Class Types

java.lang.Object
  extended by org.norther.tammi.acorn.type.Types

public abstract class Types
extends Object

Converts between primitive types and lang classes.

Version:
$Id: Types.java,v 1.34 2010-08-24 12:50:11 cvsimp Exp $
Author:
Ilkka Priha

Field Summary
static String BOOLEAN_CLASS_NAME
          Boolean class name.
static String BOOLEAN_TYPE
          boolean type.
static String BYTE_CLASS_NAME
          Byte class name.
static String BYTE_TYPE
          byte type.
static String CHAR_TYPE
          char type.
static String CHARACTER_CLASS_NAME
          Character class name.
static String DOUBLE_CLASS_NAME
          Double class name.
static String DOUBLE_TYPE
          double type.
static String ENUM_CLASS_NAME
          Enum class name.
static String FLOAT_CLASS_NAME
          Float class name.
static String FLOAT_TYPE
          float type.
static int INT_SIGN
          The int sign bit.
static String INT_TYPE
          int type.
static String INTEGER_CLASS_NAME
          Integer class name.
static String LONG_CLASS_NAME
          Long class name.
static long LONG_SIGN
          The long sign bit.
static String LONG_TYPE
          long type.
static Integer MINUS
          Minus.
static Double NAN
          NaN.
static String OBJECT_CLASS_NAME
          Object class name.
static Integer PLUS
          Plus.
static String SHORT_CLASS_NAME
          Short class name.
static String SHORT_TYPE
          short type.
static Integer ZERO
          Zero.
 
Constructor Summary
Types()
           
 
Method Summary
static boolean equals(Object o1, Object o2)
          Checks whether two objects are equal.
static int getArrayDepth(String type)
          Gets the depth of a multi-dimensional array type.
static ClassLoader getContextClassLoader(Object... objects)
          Gets the context class loader with privileges enabled.
static boolean isArrayType(String className)
          Checks whether the specified class type is an array.
static boolean isBooleanType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is boolean, i.e.
static boolean isDecimalType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is decimal.
static boolean isEnumType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is enum, i.e.
static boolean isIntegralType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is integral.
static boolean isNaN(Number number)
          Checks whether the specified number is a NaN.
static boolean isNumericType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is numeric, i.e.
static boolean isPrimitiveType(String className, boolean arrayp)
          Checks whether the specified class type is primitive.
static String toArrayType(String className, int... depth)
          Converts the specified component type to the corresponding array type.
static boolean toBoolean(String str)
          Converts a string to a boolean.
static byte toByte(String str)
          Converts a string to a byte.
static Class<?> toComponentClass(Class<?> clazz)
          Converts the specified array class to the corresponding component class.
static String toComponentType(String className)
          Converts the specified array type to the corresponding component type.
static Enum<?>[] toEnums(Class<?> clazz, int mask)
          Converts a mask of ordinals of enums to an array of enum instances.
static Enum<?>[] toEnums(Enum<?>[] enums, int mask)
          Converts a mask of ordinals of enums to an array of enum instances.
static Enum<?>[] toEnums(String className, int mask)
          Converts a mask of ordinals of enums to an array of enum instances.
static int toInt(String str)
          Converts a string to an int.
static long toLong(String str)
          Converts a string to a long.
static int toMask(Enum<?>[] enums)
          Converts an array of enum instances to a mask of enum ordinals.
static Number toNumber(Number num, String type)
          Converts a number to the specified number.
static Number toNumber(String str)
          Converts a typed string to a number.
static Number toNumber(String str, String type)
          Converts a string to the specified number.
static Class<?> toObjectClass(Class<?> type)
          Converts a primitive type to its corresponding object class.
static Class<?> toObjectClass(String primitive)
          Converts a named primitive type to its corresponding object class.
static String toObjectType(String primitive)
          Converts a primitive type to the corresponding object type.
static String toPackageName(String className)
          Converts a class name to a package name.
static String toPlainType(Class<?> clazz)
          Converts a class to its plain non-qualified component type.
static String toPlainType(String className)
          Converts a class name to its plain non-qualified component type.
static String toPrimitiveArrayType(String className)
          Converts an object type to the corresponding primitive array type.
static Class<?> toPrimitiveClass(Class<?> clazz)
          Converts a class to its corresponding primitive type.
static Class<?> toPrimitiveClass(String className)
          Converts a named class to its corresponding primitive type.
static String toPrimitiveType(String className)
          Converts an object type to the corresponding primitive type.
static short toShort(String str)
          Converts a string to a short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INT_SIGN

public static final int INT_SIGN
The int sign bit.

See Also:
Constant Field Values

LONG_SIGN

public static final long LONG_SIGN
The long sign bit.

See Also:
Constant Field Values

NAN

public static final Double NAN
NaN.


ZERO

public static final Integer ZERO
Zero.


PLUS

public static final Integer PLUS
Plus.


MINUS

public static final Integer MINUS
Minus.


BYTE_TYPE

public static final String BYTE_TYPE
byte type.


CHAR_TYPE

public static final String CHAR_TYPE
char type.


SHORT_TYPE

public static final String SHORT_TYPE
short type.


INT_TYPE

public static final String INT_TYPE
int type.


LONG_TYPE

public static final String LONG_TYPE
long type.


FLOAT_TYPE

public static final String FLOAT_TYPE
float type.


DOUBLE_TYPE

public static final String DOUBLE_TYPE
double type.


BOOLEAN_TYPE

public static final String BOOLEAN_TYPE
boolean type.


BYTE_CLASS_NAME

public static final String BYTE_CLASS_NAME
Byte class name.


CHARACTER_CLASS_NAME

public static final String CHARACTER_CLASS_NAME
Character class name.


SHORT_CLASS_NAME

public static final String SHORT_CLASS_NAME
Short class name.


INTEGER_CLASS_NAME

public static final String INTEGER_CLASS_NAME
Integer class name.


LONG_CLASS_NAME

public static final String LONG_CLASS_NAME
Long class name.


FLOAT_CLASS_NAME

public static final String FLOAT_CLASS_NAME
Float class name.


DOUBLE_CLASS_NAME

public static final String DOUBLE_CLASS_NAME
Double class name.


BOOLEAN_CLASS_NAME

public static final String BOOLEAN_CLASS_NAME
Boolean class name.


ENUM_CLASS_NAME

public static final String ENUM_CLASS_NAME
Enum class name.


OBJECT_CLASS_NAME

public static final String OBJECT_CLASS_NAME
Object class name.

Constructor Detail

Types

public Types()
Method Detail

toBoolean

public static boolean toBoolean(String str)
Converts a string to a boolean.

Parameters:
str - the string to convert.
Returns:
the converted string.

toByte

public static byte toByte(String str)
Converts a string to a byte.

Parameters:
str - the string to convert.
Returns:
the converted string.

toShort

public static short toShort(String str)
Converts a string to a short.

Parameters:
str - the string to convert.
Returns:
the converted string.

toInt

public static int toInt(String str)
Converts a string to an int.

Parameters:
str - the string to convert.
Returns:
the converted string.

toLong

public static long toLong(String str)
Converts a string to a long.

Parameters:
str - the string to convert.
Returns:
the converted string.

toNumber

public static Number toNumber(String str)
Converts a typed string to a number.

Parameters:
str - the string to convert.
Returns:
the converted string or null.
Throws:
NumberFormatException - for an invalid string.

toNumber

public static Number toNumber(Number num,
                              String type)
Converts a number to the specified number.

Parameters:
num - the number to convert.
type - the requested type.
Returns:
the converted number or null.

toNumber

public static Number toNumber(String str,
                              String type)
Converts a string to the specified number.

Parameters:
str - the string to convert.
type - the requested type.
Returns:
the converted string or null.
Throws:
NumberFormatException - for an invalid string.

equals

public static boolean equals(Object o1,
                             Object o2)
Checks whether two objects are equal. Numbers are compared by their primitive values.

Parameters:
o1 - the first object.
o2 - the second object.
Returns:
true if the two objects are equal.

isNaN

public static boolean isNaN(Number number)
Checks whether the specified number is a NaN.

Parameters:
number - the number to check.
Returns:
true for a NaN, otherwise false.

isNumericType

public static boolean isNumericType(String className,
                                    boolean arrayp,
                                    Class<?>... loaded)
Checks whether the specified class type is numeric, i.e. a subclass of java.lang.Number or any of the corresponding primitive types.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a numeric type, otherwise false.

isDecimalType

public static boolean isDecimalType(String className,
                                    boolean arrayp,
                                    Class<?>... loaded)
Checks whether the specified class type is decimal.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a decimal type, otherwise false.

isIntegralType

public static boolean isIntegralType(String className,
                                     boolean arrayp,
                                     Class<?>... loaded)
Checks whether the specified class type is integral.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a integral type, otherwise false.

isBooleanType

public static boolean isBooleanType(String className,
                                    boolean arrayp,
                                    Class<?>... loaded)
Checks whether the specified class type is boolean, i.e. a subclass of java.lang.Boolean or the corresponding primitive type.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a boolean type, otherwise false.

isEnumType

public static boolean isEnumType(String className,
                                 boolean arrayp,
                                 Class<?>... loaded)
Checks whether the specified class type is enum, i.e. a subclass of java.lang.Enum.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a boolean type, otherwise false.

isPrimitiveType

public static boolean isPrimitiveType(String className,
                                      boolean arrayp)
Checks whether the specified class type is primitive.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
Returns:
true for a primitive type, otherwise false.

isArrayType

public static boolean isArrayType(String className)
Checks whether the specified class type is an array.

Parameters:
className - the class type to check.
Returns:
true for an aray type, otherwise false.

toPrimitiveClass

public static Class<?> toPrimitiveClass(Class<?> clazz)
Converts a class to its corresponding primitive type. The component type is returned for arrays.

Parameters:
clazz - the class to convert.
Returns:
the corresponding primitive type or null if not applicable.

toPrimitiveClass

public static Class<?> toPrimitiveClass(String className)
Converts a named class to its corresponding primitive type. The component type is returned for arrays.

Parameters:
className - the class name to convert.
Returns:
the corresponding primitive type or null if not applicable.

toObjectClass

public static Class<?> toObjectClass(Class<?> type)
Converts a primitive type to its corresponding object class. The component type is returned for arrays.

Parameters:
type - the primitive type to convert.
Returns:
the corresponding object class or the original one.

toObjectClass

public static Class<?> toObjectClass(String primitive)
Converts a named primitive type to its corresponding object class. The component type is returned for arrays.

Parameters:
primitive - the primitive type to convert.
Returns:
the corresponding object class or null if not applicable.

toComponentType

public static String toComponentType(String className)
Converts the specified array type to the corresponding component type.

Note that the return type is a non-array component type also for multidimensional arrays.

Parameters:
className - the array type to convert.
Returns:
the corresponding component type.

toArrayType

public static String toArrayType(String className,
                                 int... depth)
Converts the specified component type to the corresponding array type.

Parameters:
className - the component type to convert.
depth - the array depth to add.
Returns:
the corresponding array type.

toPrimitiveType

public static String toPrimitiveType(String className)
Converts an object type to the corresponding primitive type.

Parameters:
className - the object type to convert.
Returns:
the corresponding primitive type or null if not applicable.

toComponentClass

public static Class<?> toComponentClass(Class<?> clazz)
Converts the specified array class to the corresponding component class.

Note that the return type is a non-array component class also for multidimensional arrays.

Parameters:
clazz - the array class to convert.
Returns:
the corresponding component class.

toPrimitiveArrayType

public static String toPrimitiveArrayType(String className)
Converts an object type to the corresponding primitive array type.

Parameters:
className - the object type to convert.
Returns:
the corresponding primitive array type or null if not applicable.

toObjectType

public static String toObjectType(String primitive)
Converts a primitive type to the corresponding object type.

Parameters:
primitive - the primitive type to convert.
Returns:
the corresponding object type or the original one.

getArrayDepth

public static int getArrayDepth(String type)
Gets the depth of a multi-dimensional array type.

Parameters:
type - the array type.
Returns:
the array depth

toPlainType

public static String toPlainType(Class<?> clazz)
Converts a class to its plain non-qualified component type.

Parameters:
clazz - the class.
Returns:
the plain type.

toPlainType

public static String toPlainType(String className)
Converts a class name to its plain non-qualified component type.

Parameters:
className - the qualified class name.
Returns:
the plain type.

toPackageName

public static String toPackageName(String className)
Converts a class name to a package name.

Parameters:
className - the class name to convert.
Returns:
the corresponding package name or null if not valid.

toMask

public static int toMask(Enum<?>[] enums)
Converts an array of enum instances to a mask of enum ordinals.

Parameters:
enums - an array of enum instances.
Returns:
the enum mask or 0.

toEnums

public static Enum<?>[] toEnums(String className,
                                int mask)
Converts a mask of ordinals of enums to an array of enum instances.

Parameters:
className - the enum class name.
mask - the enum mask to convert.
Returns:
an array of enum instances or null.

toEnums

public static Enum<?>[] toEnums(Class<?> clazz,
                                int mask)
Converts a mask of ordinals of enums to an array of enum instances.

Parameters:
clazz - the enum class.
mask - the enum mask to convert.
Returns:
an array of enum instances or null.

toEnums

public static Enum<?>[] toEnums(Enum<?>[] enums,
                                int mask)
Converts a mask of ordinals of enums to an array of enum instances.

Parameters:
enums - an array of enums constants.
mask - the enum mask to convert.
Returns:
an array of enum instances or null.

getContextClassLoader

public static ClassLoader getContextClassLoader(Object... objects)
Gets the context class loader with privileges enabled. If the class loader is not available, the first non-null loader of default objects is returned. If default loaders are not available, the system class loader is returned.

Parameters:
objects - optional default objects, classes and loaders.
Returns:
the context class loader, the default one or the system class loader.


Copyright © 2004 The Norther Organization. All rights reserved.