Class DefaultConversionHandler
- java.lang.Object
-
- org.apache.commons.configuration2.convert.DefaultConversionHandler
-
- All Implemented Interfaces:
ConversionHandler
public class DefaultConversionHandler extends java.lang.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 ofconvert()
to deal with such complex objects. The implementation provided by this class tries to extract the first child element and then delegates toconvertValue()
which does the actual conversion.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_DATE_FORMAT
The default format for dates.static DefaultConversionHandler
INSTANCE
A default instance of this class.
-
Constructor Summary
Constructors Constructor Description DefaultConversionHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> T
convert(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Performs the conversion from the passed in source object to the specified target class.protected <T> T
convertValue(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Performs a conversion of a single value to the specified target class.protected java.lang.Object
extractConversionValue(java.lang.Object container, java.lang.Class<?> targetCls, ConfigurationInterpolator ci)
Extracts a single value from a complex object.protected java.util.Collection<?>
extractValues(java.lang.Object source)
Extracts all values contained in the given source object and returns them as a flat collection.protected java.util.Collection<?>
extractValues(java.lang.Object source, int limit)
Extracts a maximum number of values contained in the given source object and returns them as flat collection.java.lang.String
getDateFormat()
Gets the date format used by this conversion handler.ListDelimiterHandler
getListDelimiterHandler()
Gets theListDelimiterHandler
used for extracting values from complex objects.protected boolean
isComplexObject(java.lang.Object src)
Tests whether the passed in object is complex (which means that it contains multiple values).protected boolean
isEmptyElement(java.lang.Object src)
Tests whether the passed in object represents an empty element.void
setDateFormat(java.lang.String dateFormat)
Sets the date format to be used by this conversion handler.void
setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets theListDelimiterHandler
used for extracting values from complex objects.<T> T
to(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Converts a single object to the specified target type.java.lang.Object
toArray(java.lang.Object src, java.lang.Class<?> elemClass, ConfigurationInterpolator ci)
Converts the given object to an array of the specified element type.<T> void
toCollection(java.lang.Object src, java.lang.Class<T> elemClass, ConfigurationInterpolator ci, java.util.Collection<T> dest)
Converts the given object to a collection of the specified type.
-
-
-
Field Detail
-
INSTANCE
public static final DefaultConversionHandler INSTANCE
A default instance of this class. Because an instance of this class can be shared between arbitrary objects it is possible to make use of this default instance anywhere.
-
DEFAULT_DATE_FORMAT
public static final java.lang.String DEFAULT_DATE_FORMAT
The default format for dates.- See Also:
- Constant Field Values
-
-
Method Detail
-
convert
protected <T> T convert(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Performs the conversion from the passed in source object to the specified target class. This method is called for each conversion to be done. The source object has already been passed to theConfigurationInterpolator
, so interpolation does not have to be done again. (The passed inConfigurationInterpolator
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 toextractConversionValue(Object, Class, ConfigurationInterpolator)
to obtain a single value. Eventually,convertValue(Object, Class, ConfigurationInterpolator)
is called with the single value to be converted.- Type Parameters:
T
- the desired target type of the conversion- Parameters:
src
- the source object to be convertedtargetCls
- the desired target classci
- theConfigurationInterpolator
(not null)- Returns:
- the converted value
- Throws:
ConversionException
- if conversion is not possible
-
convertValue
protected <T> T convertValue(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Performs a conversion of a single value to the specified target class. The passed in source object is guaranteed to be a single value, but it can be null. Derived classes that want to extend the available conversions, but are happy with the handling of complex objects, just need to override this method.- Type Parameters:
T
- the desired target type of the conversion- Parameters:
src
- the source object (a single value)targetCls
- the target class of the conversionci
- theConfigurationInterpolator
(not null)- Returns:
- the converted value
- Throws:
ConversionException
- if conversion is not possible
-
extractConversionValue
protected java.lang.Object extractConversionValue(java.lang.Object container, java.lang.Class<?> targetCls, ConfigurationInterpolator ci)
Extracts a single value from a complex object. This method is called byconvert()
if the source object is complex. This implementation extracts the first value from the complex object and returns it.- Parameters:
container
- the complex objecttargetCls
- the target class of the conversionci
- theConfigurationInterpolator
(not null)- Returns:
- the value to be converted (may be null if no values are found)
-
extractValues
protected java.util.Collection<?> extractValues(java.lang.Object source)
Extracts all values contained in the given source object and returns them as a flat collection.- Parameters:
source
- the source object (may be a single value or a complex object)- Returns:
- a collection with all extracted values
-
extractValues
protected java.util.Collection<?> extractValues(java.lang.Object source, int limit)
Extracts a maximum number of values contained in the given source object and returns them as flat collection. This method is useful if the caller only needs a subset of values, e.g. only the first one.- Parameters:
source
- the source object (may be a single value or a complex object)limit
- the number of elements to extract- Returns:
- a collection with all extracted values
-
getDateFormat
public java.lang.String getDateFormat()
Gets the date format used by this conversion handler.- Returns:
- the date format
-
getListDelimiterHandler
public ListDelimiterHandler getListDelimiterHandler()
Gets theListDelimiterHandler
used for extracting values from complex objects.- Returns:
- the
ListDelimiterHandler
used for extracting values from complex objects, never null. - Since:
- 2.9.0
-
isComplexObject
protected boolean isComplexObject(java.lang.Object src)
Tests whether the passed in object is complex (which means that it contains multiple values). This method is called byconvert(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
objects- Arrays
- Parameters:
src
- the source object- Returns:
- true if this is a complex object, false otherwise
-
isEmptyElement
protected boolean isEmptyElement(java.lang.Object src)
Tests whether the passed in object represents an empty element. This method is called by conversion methods to arrays or collections. If it returns true, the resulting array or collection will be empty. This implementation returns true if and only if the passed in object is an empty string. With this method it can be controlled if and how empty elements in configurations are handled.- Parameters:
src
- the object to be tested- Returns:
- a flag whether this object is an empty element
-
setDateFormat
public void setDateFormat(java.lang.String dateFormat)
Sets the date format to be used by this conversion handler. This format is applied by conversions toDate
orCalendar
objects. The string is passed to thejava.text.SimpleDateFormat
class, so it must be compatible with this class. If no date format has been set, a default format is used.- Parameters:
dateFormat
- the date format string- See Also:
DEFAULT_DATE_FORMAT
-
setListDelimiterHandler
public void setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets theListDelimiterHandler
used for extracting values from complex objects.- Parameters:
listDelimiterHandler
- theListDelimiterHandler
used for extracting values from complex objects. Setting the value to null resets the value to its default.- Since:
- 2.9.0
-
to
public <T> T to(java.lang.Object src, java.lang.Class<T> targetCls, ConfigurationInterpolator ci)
Description copied from interface:ConversionHandler
Converts a single object to the specified target type. A concrete implementation has to attempt a conversion. If this is not possible, aConversionException
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.- Specified by:
to
in interfaceConversionHandler
- Type Parameters:
T
- the type of the desired result- Parameters:
src
- the object to be convertedtargetCls
- the target class of the conversionci
- an object for performing variable substitution- Returns:
- the converted object
-
toArray
public java.lang.Object toArray(java.lang.Object src, java.lang.Class<?> elemClass, ConfigurationInterpolator ci)
Converts the given object to an array of the specified element type. The object can be a single value (e.g. a String, a primitive, etc.) or a complex object containing multiple values (like a collection or another array). In the latter case all elements contained in the complex object are converted to the target type. If the value(s) cannot be converted to the desired target class, aConversionException
is thrown. Note that the result type of this method isObject
; because this method can also produce arrays of a primitive type the return typeObject[]
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.- Specified by:
toArray
in interfaceConversionHandler
- Parameters:
src
- the object to be convertedelemClass
- the element class of the resulting arrayci
- an object for performing variable substitution- Returns:
- the array with the converted values
-
toCollection
public <T> void toCollection(java.lang.Object src, java.lang.Class<T> elemClass, ConfigurationInterpolator ci, java.util.Collection<T> dest)
Converts the given object to a collection of the specified type. The target collection must be provided (here callers have the option to specify different types of collections like lists or sets). All values contained in the specified source object (or the source object itself if it is a single value) are converted to the desired target class and added to the destination collection. If the conversion of an element is not possible, aConversionException
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.- Specified by:
toCollection
in interfaceConversionHandler
- Type Parameters:
T
- the type of the elements of the destination collection- Parameters:
src
- the object to be convertedelemClass
- the element class of the destination collectionci
- an object for performing variable substitutiondest
- the destination collection- Throws:
java.lang.IllegalArgumentException
- if the target collection is null
-
-