public class IlvConvert extends Object implements Serializable
IlvConvert
is used to convert a value from one type to
another. All methods are static.
Conversions are handled by a table of IlvConverter
objects. Each
converter handles one or several "from" types and one or several "to" types.
The conversion is actually performed by the convert()
method of
IlvConverter.
A number of predefined filters are provided:
String
to/from localized Number, Byte, Short, Integer, Long,
Float, Double, BigDecimal, BigInteger, and BooleanString
to/from Color
String
to/from Font
float
to int
)
String
to Date
, using one of two localized
formats, or two locale-independent formats:
SimpleDateFormat
pattern for the
locale.SimpleDateFormat
string is prepended to the date,
separated by a comma. For example: "mm/dd/yy,02/21/03".String
to/from localized Currency/Percent (use
IlvLocalizedConverter.CURRENCY_AS_NUMBER_CLASS
and
IlvLocalizedConverter.PERCENT_AS_NUMBER_CLASS
as type.More filters supplied by other packages or by the application can be added.
It is also possible to register a PropertyEditor
which
converts value from/to String through its get/setAsText() methods.
This is a bit different from
IlvConvertForSetters
, which converts a value
from a PropertyDescriptor.
The code comes mainly from BGO (IlvValueConverter)
IlvConverter
,
IlvLocalizedConverter
,
IlvConvertForSetters
,
Serialized FormModifier and Type | Method and Description |
---|---|
static void |
addConverter(IlvConverter converter)
Adds a converter to the global list of converters used to convert values.
|
static void |
addConverter(IlvLocalizedPropertyEditorFactory factory,
Class<?> type)
Adds a converter to the global list of converters used to convert values.
|
static void |
addConverter(PropertyEditor pe,
Class<?> type)
Adds a converter to the global list of converters used to convert values.
|
static Object |
convert(Object value,
Class<?> toType)
Converts the type of a value to a new type.
|
static Object |
convert(Object value,
Class<?> toType,
Locale locale)
Deprecated.
This method ignores the
numbers and
calendar specifications in the
ULocale . Better use
convert(Object, Class, ULocale) instead. |
static Object |
convert(Object value,
Class<?> toType,
com.ibm.icu.util.ULocale locale)
Converts the type of a value to a new type.
|
static boolean |
convertToBoolean(Object value)
Converts a value from its current type to a boolean value.
|
static byte |
convertToByte(Object value)
Converts a value from its current type to a byte.
|
static double |
convertToDouble(Object value)
Converts a value from its current type to a double.
|
static float |
convertToFloat(Object value)
Converts a value from its current type to a float.
|
static int |
convertToInt(Object value)
Converts a value from its current type to an integer.
|
static long |
convertToLong(Object value)
Converts a value from its current type to a long integer.
|
static short |
convertToShort(Object value)
Converts a value from its current type to a short.
|
static String |
convertToString(Object value)
Converts a value from its current type to a string.
|
static IlvConverter |
getConverter(Class<?> from,
Class<?> to)
Returns the converter which can handle given parameters
|
static boolean |
isConverterLocalized(Class<?> from,
Class<?> to)
Returns true whether the converter which can handle given parameters is
localized.
|
static boolean |
isConvertible(Class<?> fromType,
Class<?> toType)
Returns
true if a values from type fromType can
be converted to a value of type toType . |
static void |
removeConverter(Class<?> from,
Class<?> to)
Removes the converter between given types.
|
public static final void addConverter(IlvConverter converter)
converter
- the new value converter. The source and target types handled by
the converter are determined by the result of the methods
fromTypes
and toTypes
of the converter.public static final void addConverter(PropertyEditor pe, Class<?> type)
pe
- the new value converter which must implement its getAsTex() and
setAsText() methods.type
- the type of the property associated to the given property editor.public static final void addConverter(IlvLocalizedPropertyEditorFactory factory, Class<?> type)
factory
- The property editor factory that must produce property editors for
given locales.type
- the type of the property associated to the given property editor.public static IlvConverter getConverter(Class<?> from, Class<?> to)
from
- the source typeto
- the target typepublic static boolean isConverterLocalized(Class<?> from, Class<?> to)
from
- the source typeto
- the target typepublic static void removeConverter(Class<?> from, Class<?> to)
from
- the source typeto
- the target typepublic static final Object convert(Object value, Class<?> toType) throws IlvConvertException
value
- the value to be converted.toType
- the type to which the value must be converted.toType
.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.public static boolean isConvertible(Class<?> fromType, Class<?> toType)
true
if a values from type fromType
can
be converted to a value of type toType
.fromType
- the source typetoType
- the destination type@Deprecated public static final Object convert(Object value, Class<?> toType, Locale locale) throws IlvConvertException
numbers
and
calendar
specifications in the
ULocale
. Better use
convert(Object, Class, ULocale)
instead.value
- the value to be converted.toType
- the type to which the value must be converted.locale
- the locale used for the conversiontoType
.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.public static final Object convert(Object value, Class<?> toType, com.ibm.icu.util.ULocale locale) throws IlvConvertException
value
- the value to be converted.toType
- the type to which the value must be converted.locale
- the locale used for the conversiontoType
.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.public static final String convertToString(Object value) throws IlvConvertException
(String) convert(value, String.class)
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final int convertToInt(Object value) throws IlvConvertException
((Integer) convert(value, Integer.class)).intValue()
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final long convertToLong(Object value) throws IlvConvertException
((Long) convert(value, Long.class)).longValue()
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final boolean convertToBoolean(Object value) throws IlvConvertException
((Boolean) convert(value, Boolean.class)).booleanValue()
value
- the value to convertIlvConvertException
- if not converter was registered for the requested conversion
or if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final float convertToFloat(Object value) throws IlvConvertException
((Float) convert(value, Float.class)).floatValue()
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final double convertToDouble(Object value) throws IlvConvertException
((Double) convert(value, Double.class)).doubleValue()
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final short convertToShort(Object value) throws IlvConvertException
((Short) convert(value, Short.class)).shortValue()
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
public static final byte convertToByte(Object value) throws IlvConvertException
value
- the value to be converted.IlvConvertException
- if no converter was registered for the requested conversion or
if the value could not be converted to the requested type.convert(java.lang.Object, java.lang.Class<?>)
© Copyright 2024 Rogue Wave Software, Inc., a Perforce company.. All Rights Reserved.