public final class ConversionUtils extends Object
This is done via registered ObjectConverter
's. See
setObjectConverter(Class, ObjectConverter)
,
getObjectConverter(Class)
.
The convert(Object, Class)
method forwards the request to the
registered ObjectConverter
and returns the converted value, If the
conversion fails a ObjectConversionException
is thrown.
Alternatively you can use convert(Object, Class, Object)
wich uses a
fallback instead of the ObjectConversionException
.
Default converters are installed for the following types:
toint(Object)
or toInteger(Object)
.Modifier and Type | Method and Description |
---|---|
static <T> T |
convert(Object value,
Class<T> type)
|
static <T> T |
convert(Object value,
Class<T> type,
T fallback)
|
static <T> ObjectConverter<T> |
getObjectConverter(Class<T> type)
Returns the registered ObjectConverter for
type , or
null if none is registered. |
static <T> void |
setObjectConverter(Class<T> type,
ObjectConverter<T> converter)
Registeres an ObjectConverter for the type T.
|
static boolean |
toboolean(Object value)
Converts
value to a boolean primitive. |
static Boolean |
toBoolean(Object value)
Converts
value to a Boolean . |
static boolean |
toboolean(Object value,
boolean fallback)
Converts
value to a boolean primitive. |
static Boolean |
toBoolean(Object value,
Boolean fallback)
Converts
value to a Boolean . |
static byte |
tobyte(Object value)
Converts
value to a byte primitive. |
static Byte |
toByte(Object value)
Converts
value to a Byte . |
static byte |
tobyte(Object value,
byte fallback)
Converts
value to a byte primitive. |
static Byte |
toByte(Object value,
Byte fallback)
Converts
value to a Byte . |
static byte[] |
toBytes(Object value)
Converts
value to a byte[] array. |
static byte[] |
toBytes(Object value,
byte[] fallback)
Converts
value to a byte[] array. |
static char |
tochar(Object value)
Converts
value to a char primitive. |
static char |
tochar(Object value,
char fallback)
Converts
value to a char primitive. |
static Character |
toCharacter(Object value)
Converts
value to a Character . |
static Character |
toCharacter(Object value,
Character fallback)
Converts
value to a Character . |
static char[] |
toChars(Object value)
Converts
value to a char[] array. |
static char[] |
toChars(Object value,
char[] fallback)
Converts
value to a char[] array. |
static Date |
toDate(Object value)
Converts
value to a Date . |
static Date |
toDate(Object value,
Date fallback)
Converts
value to a Date . |
static double |
todouble(Object value)
Converts
value to a double primitive. |
static Double |
toDouble(Object value)
Converts
value to a Double . |
static double |
todouble(Object value,
double fallback)
Converts
value to a double primitive. |
static Double |
toDouble(Object value,
Double fallback)
Converts
value to a Double . |
static float |
tofloat(Object value)
Converts
value to a float primitive. |
static Float |
toFloat(Object value)
Converts
value to a Float . |
static float |
tofloat(Object value,
float fallback)
Converts
value to a float primitive. |
static Float |
toFloat(Object value,
Float fallback)
Converts
value to a Float . |
static int |
toint(Object value)
Converts
value to an int primitive. |
static int |
toint(Object value,
int fallback)
Converts
value to an int primitive. |
static Integer |
toInteger(Object value)
Converts
value to an Integer . |
static Integer |
toInteger(Object value,
Integer fallback)
Converts
value to an Integer . |
static long |
tolong(Object value)
Converts
value to a long primitive. |
static Long |
toLong(Object value)
Converts
value to a Long . |
static long |
tolong(Object value,
long fallback)
Converts
value to a long primitive. |
static Long |
toLong(Object value,
Long fallback)
Converts
value to a Long . |
static short |
toshort(Object value)
Converts
value to a short primitive. |
static Short |
toShort(Object value)
Converts
value to a Short . |
static short |
toshort(Object value,
short fallback)
Converts
value to a short primitive. |
static Short |
toShort(Object value,
Short fallback)
Converts
value to a Short . |
static String |
toString(Object value)
Converts
value to a String . |
static String |
toString(Object value,
String fallback)
Converts
value to a String . |
static XdevBlob |
toXdevBlob(Object value)
Converts
value to a XdevBlob . |
static XdevBlob |
toXdevBlob(Object value,
XdevBlob fallback)
Converts
value to a XdevBlob . |
static XdevClob |
toXdevClob(Object value)
Converts
value to a XdevClob . |
static XdevClob |
toXdevClob(Object value,
XdevClob fallback)
Converts
value to a XdevClob . |
static XdevDate |
toXdevDate(Object value)
Converts
value to a XdevDate . |
static XdevDate |
toXdevDate(Object value,
XdevDate fallback)
Converts
value to a XdevDate . |
static XdevFile |
toXdevFile(Object value)
Converts
value to a XdevFile . |
static XdevFile |
toXdevFile(Object value,
XdevFile fallback)
Converts
value to a XdevFile . |
static XdevHashtable |
toXdevHashtable(Object value)
Converts
value to a XdevHashtable . |
static XdevHashtable |
toXdevHashtable(Object value,
XdevHashtable fallback)
Converts
value to a XdevHashtable . |
static XdevImage |
toXdevImage(Object value)
Converts
value to a XdevImage . |
static XdevImage |
toXdevImage(Object value,
XdevImage fallback)
Converts
value to a XdevImage . |
static XdevList |
toXdevList(Object value)
Converts
value to a XdevList . |
static XdevList |
toXdevList(Object value,
XdevList fallback)
Converts
value to a XdevList . |
public static <T> void setObjectConverter(Class<T> type, ObjectConverter<T> converter)
T
- type
- the registered typeconverter
- the new converterpublic static <T> ObjectConverter<T> getObjectConverter(Class<T> type)
type
, or
null
if none is registered.T
- type
- the conversion typenull
public static <T> T convert(Object value, Class<T> type) throws ObjectConversionException
value
to type
via the registered
ObjectConverter
.
If no converter is registered for type
or the conversion
fails an ObjectConversionException
is thrown.
T
- value
- the value to converttype
- the target typeObjectConversionException
- if no converter is registered for type
or the
conversion failspublic static <T> T convert(Object value, Class<T> type, T fallback)
value
to type
via the registered
ObjectConverter
.
If no converter is registered for type
or the conversion
fails fallback
is returned.
T
- value
- the value to converttype
- the target typefallback
- the fallback valuefallback
public static byte tobyte(Object value) throws ObjectConversionException
value
to a byte primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static byte tobyte(Object value, byte fallback)
value
to a byte primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Byte toByte(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Byte toByte(Object value, Byte fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static short toshort(Object value) throws ObjectConversionException
value
to a short primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static short toshort(Object value, short fallback)
value
to a short primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Short toShort(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Short toShort(Object value, Short fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static int toint(Object value) throws ObjectConversionException
value
to an int primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static int toint(Object value, int fallback)
value
to an int primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Integer toInteger(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Integer toInteger(Object value, Integer fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static long tolong(Object value) throws ObjectConversionException
value
to a long primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static long tolong(Object value, long fallback)
value
to a long primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Long toLong(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Long toLong(Object value, Long fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static float tofloat(Object value) throws ObjectConversionException
value
to a float primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static float tofloat(Object value, float fallback)
value
to a float primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Float toFloat(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Float toFloat(Object value, Float fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static double todouble(Object value) throws ObjectConversionException
value
to a double primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static double todouble(Object value, double fallback)
value
to a double primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Double toDouble(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Double toDouble(Object value, Double fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static boolean toboolean(Object value) throws ObjectConversionException
value
to a boolean primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static boolean toboolean(Object value, boolean fallback)
value
to a boolean primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Boolean toBoolean(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Boolean toBoolean(Object value, Boolean fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static char tochar(Object value) throws ObjectConversionException
value
to a char primitive.
If value is null
or the conversion fails an
ObjectConversionException
is thrown.
value
- the value to convertObjectConversionException
- If value is null
or the conversion failspublic static char tochar(Object value, char fallback)
value
to a char primitive.
If value is null
or the conversion fails
fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
public static Character toCharacter(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Character toCharacter(Object value, Character fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static String toString(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static String toString(Object value, String fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static Date toDate(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static Date toDate(Object value, Date fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevDate toXdevDate(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevDate toXdevDate(Object value, XdevDate fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevBlob toXdevBlob(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevBlob toXdevBlob(Object value, XdevBlob fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static byte[] toBytes(Object value) throws ObjectConversionException
value
to a byte[] array.
If the conversion fails an ObjectConversionException
is thrown.
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static byte[] toBytes(Object value, byte[] fallback)
value
to a byte[] array.
If the conversion fails fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevClob toXdevClob(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevClob toXdevClob(Object value, XdevClob fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static char[] toChars(Object value) throws ObjectConversionException
value
to a char[] array.
If the conversion fails an ObjectConversionException
is thrown.
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static char[] toChars(Object value, char[] fallback)
value
to a char[] array.
If the conversion fails fallback
is returned.
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevList toXdevList(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevList toXdevList(Object value, XdevList fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevHashtable toXdevHashtable(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevHashtable toXdevHashtable(Object value, XdevHashtable fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevFile toXdevFile(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failspublic static XdevFile toXdevFile(Object value, XdevFile fallback)
value
- the value to convertfallback
- the fallback valuefallback
if the conversion failspublic static XdevImage toXdevImage(Object value) throws ObjectConversionException
value
- the value to convertnull
if value
is
null
ObjectConversionException
- If the conversion failsCopyright © 2003–2021 XDEV Software. All rights reserved.