| java.lang.Object com.sun.rave.web.ui.util.TypeConverter
TypeConverter | public class TypeConverter extends Object (Code) | | Provides an efficient and robust mechanism for converting an object
to a different type. For example, one can convert a String to
an Integer using the TypeConverter like this:
Integer i = (Integer) TypeConverter.asType(Integer.class, "123");
or using the shortcut method:
int i = TypeConverter.asInt("123");
The TypeConverter comes ready to convert all the primitive
types, plus a few more like java.sql.Date and
java.math.BigDecimal .
The conversion process has been optimized so that it is now a constant
time operation (aside from the conversion itself, which may vary).
Because of this optimization, it is possible to register classes that
implement the new TypeConversion interface for conversion
to a custom type. For example, this means that you can define a class to
convert arbitrary objects to type Foo , and register it for
use throughout the VM:
TypeConversion fooConversion = new FooTypeConversion();
TypeConverter.registerTypeConversion(Foo.class, fooConversion);
...
Bar bar = new Bar();
Foo foo = (Foo) TypeConverter.asType(Foo.class, bar);
...
String s = "bar";
Foo foo = (Foo) TypeConverter.asType(Foo.class, s);
The TypeConverter allows specification of an arbitrary type key in
the registerTypeConversion() and asType() methods,
so one can simultaneously register a conversion object under a
Class object, a class name, and a logical type name. For example,
the following are valid ways of converting a string to an int
using TypeConverter :
Integer i = (Integer) TypeConverter.asType(Integer.class, "123");
Integer i = (Integer) TypeConverter.asType("java.lang.Integer", "123");
Integer i = (Integer) TypeConverter.asType(TypeConverter.TYPE_INT, "123");
Integer i = (Integer) TypeConverter.asType(TypeConverter.TYPE_INTEGER, "123");
Integer i = (Integer) TypeConverter.asType("int", "123");
Integer i = (Integer) TypeConverter.asType("integer", "123");
int i = TypeConverter.asInt("123");
Default type conversions have been registered under the following keys:
Classes:
java.lang.Object
java.lang.String
java.lang.Integer
java.lang.Integer.TYPE (int)
java.lang.Double
java.lang.Double.TYPE (double)
java.lang.Boolean
java.lang.Boolean.TYPE (boolean)
java.lang.Long
java.lang.Long.TYPE (long)
java.lang.Float
java.lang.Float.TYPE (float)
java.lang.Short
java.lang.Short.TYPE (short)
java.lang.Byte
java.lang.Byte.TYPE (byte)
java.lang.Character
java.lang.Character.TYPE (char)
java.math.BigDecimal
java.sql.Date
java.sql.Time
java.sql.Timestamp
Class name strings:
"java.lang.Object"
"java.lang.String"
"java.lang.Integer"
"java.lang.Double"
"java.lang.Boolean"
"java.lang.Long"
"java.lang.Float"
"java.lang.Short"
"java.lang.Byte"
"java.lang.Character"
"java.math.BigDecimal"
"java.sql.Date"
"java.sql.Time"
"java.sql.Timestamp"
Logical type name string constants:
TypeConverter.TYPE_UNKNOWN ("null")
TypeConverter.TYPE_OBJECT ("object")
TypeConverter.TYPE_STRING ("string")
TypeConverter.TYPE_INT ("int")
TypeConverter.TYPE_INTEGER ("integer")
TypeConverter.TYPE_DOUBLE ("double")
TypeConverter.TYPE_BOOLEAN ("boolean")
TypeConverter.TYPE_LONG ("long")
TypeConverter.TYPE_FLOAT ("float")
TypeConverter.TYPE_SHORT ("short")
TypeConverter.TYPE_BYTE ("byte")
TypeConverter.TYPE_CHAR ("char")
TypeConverter.TYPE_CHARACTER ("character")
TypeConverter.TYPE_BIG_DECIMAL ("bigdecimal")
TypeConverter.TYPE_SQL_DATE ("sqldate")
TypeConverter.TYPE_SQL_TIME ("sqltime")
TypeConverter.TYPE_SQL_TIMESTAMP ("sqltimestamp")
The TypeConverter treats type keys of type Class
slightly differently than other keys. If the provided value is already of
the type specified by the type key class, it is returned without a
conversion taking place. For example, a value of type MySub
that extends the class MySuper would not be converted in the
following situation because it is already of the necessary type:
MySub o = (MySub) TypeConverter.asType(MySuper.class, mySub);
Be warned that although the type conversion infrastructure in this class
is desgned to add only minimal overhead to the conversion process, conversion
of an object to another type is a potentially expensive operation and should
be used with discretion.
See Also: TypeConversion author: Todd Fast, todd.fast@sun.com author: Mike Frisino, michael.frisino@sun.com author: Ken Paulsen, ken.paulsen@sun.com (stripped down) |
Inner Class :public static class UnknownTypeConversion implements TypeConversion | |
Inner Class :public static class StringTypeConversion implements TypeConversion | |
Inner Class :public static class IntegerTypeConversion implements TypeConversion | |
Inner Class :public static class DoubleTypeConversion implements TypeConversion | |
Inner Class :public static class BooleanTypeConversion implements TypeConversion | |
Inner Class :public static class LongTypeConversion implements TypeConversion | |
Inner Class :public static class FloatTypeConversion implements TypeConversion | |
Inner Class :public static class ShortTypeConversion implements TypeConversion | |
Inner Class :public static class BigDecimalTypeConversion implements TypeConversion | |
Inner Class :public static class ByteTypeConversion implements TypeConversion | |
Inner Class :public static class CharacterTypeConversion implements TypeConversion | |
Inner Class :public static class SqlDateTypeConversion implements TypeConversion | |
Inner Class :public static class SqlTimeTypeConversion implements TypeConversion | |
Inner Class :public static class SqlTimestampTypeConversion implements TypeConversion | |
Inner Class :public static class ObjectTypeConversion implements TypeConversion | |
Method Summary | |
public static boolean | asBoolean(Object value) | public static boolean | asBoolean(Object value, boolean defaultValue) | public static byte | asByte(Object value) | public static byte | asByte(Object value, byte defaultValue) | public static char | asChar(Object value) | public static char | asChar(Object value, char defaultValue) | public static double | asDouble(Object value) | public static double | asDouble(Object value, double defaultValue) | public static float | asFloat(Object value) | public static float | asFloat(Object value, float defaultValue) | public static int | asInt(Object value) | public static int | asInt(Object value, int defaultValue) | public static long | asLong(Object value) | public static long | asLong(Object value, long defaultValue) | public static short | asShort(Object value) | public static short | asShort(Object value, short defaultValue) | public static String | asString(Object value) | public static String | asString(Object value, String defaultValue) | public static Object | asType(Object typeKey, Object value) Convert an object to the type specified by the provided type key.
A type conversion object must have been previously registered
under the provided key in order for the conversion to succeed (with
one exception, see below).
Note, this method treats type keys of type Class
differently than other type keys. | public static Map | getTypeConversions() Return the map of type conversion objects. | public static void | registerTypeConversion(Object key, TypeConversion conversion) Register a type conversion object under the specified key. |
TYPE_BIG_DECIMAL | final public static String TYPE_BIG_DECIMAL(Code) | | Logical type name "bigdecimal"
|
TYPE_BOOLEAN | final public static String TYPE_BOOLEAN(Code) | | Logical type name "boolean"
|
TYPE_BYTE | final public static String TYPE_BYTE(Code) | | Logical type name "byte"
|
TYPE_CHAR | final public static String TYPE_CHAR(Code) | | Logical type name "char"
|
TYPE_CHARACTER | final public static String TYPE_CHARACTER(Code) | | Logical type name "character"
|
TYPE_DOUBLE | final public static String TYPE_DOUBLE(Code) | | Logical type name "double"
|
TYPE_FLOAT | final public static String TYPE_FLOAT(Code) | | Logical type name "float"
|
TYPE_INT | final public static String TYPE_INT(Code) | | Logical type name "int"
|
TYPE_INTEGER | final public static String TYPE_INTEGER(Code) | | Logical type name "integer"
|
TYPE_LONG | final public static String TYPE_LONG(Code) | | Logical type name "long"
|
TYPE_OBJECT | final public static String TYPE_OBJECT(Code) | | Logical type name "object"
|
TYPE_SHORT | final public static String TYPE_SHORT(Code) | | Logical type name "short"
|
TYPE_SQL_DATE | final public static String TYPE_SQL_DATE(Code) | | Logical type name "sqldate"
|
TYPE_SQL_TIME | final public static String TYPE_SQL_TIME(Code) | | Logical type name "sqltime"
|
TYPE_SQL_TIMESTAMP | final public static String TYPE_SQL_TIMESTAMP(Code) | | Logical type name "sqltimestamp"
|
TYPE_STRING | final public static String TYPE_STRING(Code) | | Logical type name "string"
|
TYPE_UNKNOWN | final public static String TYPE_UNKNOWN(Code) | | Logical type name "null"
|
asBoolean | public static boolean asBoolean(Object value)(Code) | | |
asBoolean | public static boolean asBoolean(Object value, boolean defaultValue)(Code) | | |
asByte | public static byte asByte(Object value, byte defaultValue)(Code) | | |
asChar | public static char asChar(Object value, char defaultValue)(Code) | | |
asDouble | public static double asDouble(Object value, double defaultValue)(Code) | | |
asFloat | public static float asFloat(Object value, float defaultValue)(Code) | | |
asInt | public static int asInt(Object value, int defaultValue)(Code) | | |
asLong | public static long asLong(Object value, long defaultValue)(Code) | | |
asShort | public static short asShort(Object value, short defaultValue)(Code) | | |
asType | public static Object asType(Object typeKey, Object value)(Code) | | Convert an object to the type specified by the provided type key.
A type conversion object must have been previously registered
under the provided key in order for the conversion to succeed (with
one exception, see below).
Note, this method treats type keys of type Class
differently than other type keys. That is, this method will check
if the provided value is the same as or a subclass of the specified
class. If it is, this method returns the value object immediately
without attempting to convert its type. One exception to this
rule is if the provided type key is Object.class , in
which case the conversion is attempted anyway. The reason for this
deviation is that this key may have special meaning based on the
type of the provided value. For example, if the provided value is
a byte array, the ObjectTypeConversion class assumes
it is a serialized object and attempts to deserialize it. Because
all objects, including arrays, are of type Object ,
this conversion would never be attempted without this special
handling. (Note that the default conversion for type key
Object.class is to simply return the original object.)
Parameters: typeKey - The key under which the desired type conversion objecthas been previously registered. Most commonly, this keyshould be a Class object, a class namestring, or a logical type string represented by thevarious TYPE_* constants defined in thisclass. Parameters: value - The value to convert to the specified target type The converted value object, or null if theoriginal value is null |
getTypeConversions | public static Map getTypeConversions()(Code) | | Return the map of type conversion objects. The keys for the values
in this map may be arbitrary objects, but the values are of type
TypeConversion .
|
registerTypeConversion | public static void registerTypeConversion(Object key, TypeConversion conversion)(Code) | | Register a type conversion object under the specified key. This
method can be used by developers to register custom type conversion
objects.
|
|
|