public class DefaultConversionHandler extends Object implements ConversionHandler
A default implementation of the ConversionHandler
interface.
This class implements the standard data type conversions as used by
AbstractConfiguration
and derived classes. There is a central
conversion method - convert()
- for converting a passed in object to
a given target class. The basic implementation already handles a bunch of
standard data type conversions. If other conversions are to be supported,
this method can be overridden.
The object passed to convert()
can be a single value or a complex
object (like an array, a collection, etc.) containing multiple values. It
lies in the responsibility of convert()
to deal with such complex
objects. The implementation provided by this class tries to extract the first
child element and then delegates to convertValue()
which does the
actual conversion.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_DATE_FORMAT
The default format for dates.
|
static DefaultConversionHandler |
INSTANCE
A default instance of this class.
|
Constructor and Description |
---|
DefaultConversionHandler() |
Modifier and Type | Method and Description |
---|---|
protected <T> T |
convert(Object src,
Class<T> targetCls,
ConfigurationInterpolator ci)
Performs the conversion from the passed in source object to the specified
target class.
|
protected <T> T |
convertValue(Object src,
Class<T> targetCls,
ConfigurationInterpolator ci)
Performs a conversion of a single value to the specified target class.
|
protected Object |
extractConversionValue(Object container,
Class<?> targetCls,
ConfigurationInterpolator ci)
Extracts a single value from a complex object.
|
protected Collection<?> |
extractValues(Object source)
Extracts all values contained in the given source object and returns them
as a flat collection.
|
protected Collection<?> |
extractValues(Object source,
int limit)
Extracts a maximum number of values contained in the given source object
and returns them as flat collection.
|
String |
getDateFormat()
Returns the date format used by this conversion handler.
|
protected boolean |
isComplexObject(Object src)
Tests whether the passed in object is complex (which means that it
contains multiple values).
|
protected boolean |
isEmptyElement(Object src)
Tests whether the passed in object represents an empty element.
|
void |
setDateFormat(String dateFormat)
Sets the date format to be used by this conversion handler.
|
<T> T |
to(Object src,
Class<T> targetCls,
ConfigurationInterpolator ci)
Converts a single object to the specified target type.
|
Object |
toArray(Object src,
Class<?> elemClass,
ConfigurationInterpolator ci)
Converts the given object to an array of the specified element type.
|
<T> void |
toCollection(Object src,
Class<T> elemClass,
ConfigurationInterpolator ci,
Collection<T> dest)
Converts the given object to a collection of the specified type.
|
public static final DefaultConversionHandler INSTANCE
public static final String DEFAULT_DATE_FORMAT
public String getDateFormat()
public void setDateFormat(String dateFormat)
Date
or Calendar
objects.
The string is passed to the java.text.SimpleDateFormat
class, so
it must be compatible with this class. If no date format has been set, a
default format is used.dateFormat
- the date format stringDEFAULT_DATE_FORMAT
public <T> T to(Object src, Class<T> targetCls, ConfigurationInterpolator ci)
ConversionHandler
ConversionException
is thrown. It is up to a concrete
implementation how null values are handled; a default strategy
would be to return null if the source object is null.to
in interface ConversionHandler
T
- the type of the desired resultsrc
- the object to be convertedtargetCls
- the target class of the conversionci
- an object for performing variable substitutionpublic Object toArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci)
ConversionException
is thrown. Note
that the result type of this method is Object
; because this
method can also produce arrays of a primitive type the return type
Object[]
cannot be used. This implementation extracts all values stored in the
passed in source object, converts them to the target type, and adds them
to a result array. Arrays of objects and of primitive types are
supported. If the source object is null, result is null,
too.toArray
in interface ConversionHandler
src
- the object to be convertedelemClass
- the element class of the resulting arrayci
- an object for performing variable substitutionpublic <T> void toCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest)
ConversionException
is thrown. This implementation extracts all values stored in the
passed in source object, converts them to the target type, and adds them
to the target collection. The target collection must not be null.
If the source object is null, nothing is added to the collection.toCollection
in interface ConversionHandler
T
- the type of the elements of the destination collectionsrc
- the object to be convertedelemClass
- the element class of the destination collectionci
- an object for performing variable substitutiondest
- the destination collectionIllegalArgumentException
- if the target collection is nullprotected boolean isComplexObject(Object src)
convert(Object, Class, ConfigurationInterpolator)
to figure out
whether a actions are required to extract a single value from a complex
source object. This implementation considers the following objects as
complex:
Iterable
objectsIterator
objectssrc
- the source objectprotected boolean isEmptyElement(Object src)
src
- the object to be testedprotected <T> T convert(Object src, Class<T> targetCls, ConfigurationInterpolator ci)
ConfigurationInterpolator
, so interpolation does not have to be
done again. (The passed in ConfigurationInterpolator
may still be
necessary for extracting values from complex objects; it is guaranteed to
be non null.) The source object may be a complex object, e.g. a
collection or an array. This base implementation checks whether the
source object is complex. If so, it delegates to
extractConversionValue(Object, Class, ConfigurationInterpolator)
to obtain a single value. Eventually,
convertValue(Object, Class, ConfigurationInterpolator)
is called
with the single value to be converted.T
- the desired target type of the conversionsrc
- the source object to be convertedtargetCls
- the desired target classci
- the ConfigurationInterpolator
(not null)ConversionException
- if conversion is not possibleprotected Collection<?> extractValues(Object source, int limit)
source
- the source object (may be a single value or a complex
object)limit
- the number of elements to extractprotected Collection<?> extractValues(Object source)
source
- the source object (may be a single value or a complex
object)protected Object extractConversionValue(Object container, Class<?> targetCls, ConfigurationInterpolator ci)
convert()
if the source object is complex. This implementation
extracts the first value from the complex object and returns it.container
- the complex objecttargetCls
- the target class of the conversionci
- the ConfigurationInterpolator
(not null)protected <T> T convertValue(Object src, Class<T> targetCls, ConfigurationInterpolator ci)
T
- the desired target type of the conversionsrc
- the source object (a single value)targetCls
- the target class of the conversionci
- the ConfigurationInterpolator
(not null)ConversionException
- if conversion is not possibleCopyright © 2001–2020 The Apache Software Foundation. All rights reserved.