Class DefaultConversionHandler
- All Implemented Interfaces:
ConversionHandler
- Direct Known Subclasses:
DataConfiguration.DataConversionHandler
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.
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringThe current date format.static final StringThe default format for dates.static final DefaultConversionHandlerA default instance of this class.(package private) static final ListDelimiterHandlerThe defaultListDelimiterHandlerused for extracting values from complex objects.private ListDelimiterHandlerThe defaultListDelimiterHandlerused for extracting values from complex objects.private static final ConfigurationInterpolatorConstant for a defaultConfigurationInterpolatorto be used if none is provided by the caller. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> Tconvert(Object src, Class<T> targetCls, ConfigurationInterpolator ci) Performs the conversion from the passed in source object to the specified target class.private <T> voidconvertToCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest) Helper method for converting all values of a source object and storing them in a collection.protected <T> TconvertValue(Object src, Class<T> targetCls, ConfigurationInterpolator ci) Performs a conversion of a single value to the specified target class.protected ObjectextractConversionValue(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.private static ConfigurationInterpolatorObtains aConfigurationInterpolator.Gets the date format used by this conversion handler.Gets theListDelimiterHandlerused for extracting values from complex objects.protected booleanisComplexObject(Object src) Tests whether the passed in object is complex (which means that it contains multiple values).protected booleanisEmptyElement(Object src) Tests whether the passed in object represents an empty element.voidsetDateFormat(String dateFormat) Sets the date format to be used by this conversion handler.voidsetListDelimiterHandler(ListDelimiterHandler listDelimiterHandler) Sets theListDelimiterHandlerused for extracting values from complex objects.<T> Tto(Object src, Class<T> targetCls, ConfigurationInterpolator ci) Converts a single object to the specified target type.toArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci) Converts the given object to an array of the specified element type.<T> voidtoCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest) Converts the given object to a collection of the specified type.private <T> T[]toObjectArray(Object src, Class<T> elemClass, ConfigurationInterpolator ci) Converts the given source object to an array of objects.private ObjecttoPrimitiveArray(Object src, Class<?> elemClass, ConfigurationInterpolator ci) Converts the given source object to an array of a primitive type.
-
Field Details
-
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
The default format for dates.- See Also:
-
NULL_INTERPOLATOR
Constant for a defaultConfigurationInterpolatorto be used if none is provided by the caller. -
LIST_DELIMITER_HANDLER
The defaultListDelimiterHandlerused for extracting values from complex objects. -
dateFormat
The current date format. -
listDelimiterHandler
The defaultListDelimiterHandlerused for extracting values from complex objects.
-
-
Constructor Details
-
DefaultConversionHandler
public DefaultConversionHandler()
-
-
Method Details
-
fetchInterpolator
Obtains aConfigurationInterpolator. If the passed in one is not null, it is used. Otherwise, a default one is returned.- Parameters:
ci- theConfigurationInterpolatorprovided by the caller- Returns:
- the
ConfigurationInterpolatorto be used
-
convert
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 inConfigurationInterpolatormay 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
-
convertToCollection
private <T> void convertToCollection(Object src, Class<T> elemClass, ConfigurationInterpolator ci, Collection<T> dest) Helper method for converting all values of a source object and storing them in a collection.- Type Parameters:
T- the target type of the conversion- Parameters:
src- the source objectelemClass- the target class of the conversionci- theConfigurationInterpolatordest- the collection in which to store the results- Throws:
ConversionException- if a conversion cannot be performed
-
convertValue
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 Object extractConversionValue(Object container, 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
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
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
Gets the date format used by this conversion handler.- Returns:
- the date format
-
getListDelimiterHandler
Gets theListDelimiterHandlerused for extracting values from complex objects.- Returns:
- the
ListDelimiterHandlerused for extracting values from complex objects, never null. - Since:
- 2.9.0
-
isComplexObject
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:IterableobjectsIteratorobjects- Arrays
- Parameters:
src- the source object- Returns:
- true if this is a complex object, false otherwise
-
isEmptyElement
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
Sets the date format to be used by this conversion handler. This format is applied by conversions toDateorCalendarobjects. The string is passed to theSimpleDateFormatclass, 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:
-
setListDelimiterHandler
Sets theListDelimiterHandlerused for extracting values from complex objects.- Parameters:
listDelimiterHandler- theListDelimiterHandlerused for extracting values from complex objects. Setting the value to null resets the value to its default.- Since:
- 2.9.0
-
to
Description copied from interface:ConversionHandlerConverts a single object to the specified target type. A concrete implementation has to attempt a conversion. If this is not possible, aConversionExceptionis 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:
toin 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
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, aConversionExceptionis 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:
toArrayin 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(Object src, Class<T> elemClass, ConfigurationInterpolator ci, 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, aConversionExceptionis 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:
toCollectionin 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:
IllegalArgumentException- if the target collection is null
-
toObjectArray
Converts the given source object to an array of objects.- Parameters:
src- the source objectelemClass- the element class of the arrayci- theConfigurationInterpolator- Returns:
- the result array
- Throws:
ConversionException- if a conversion cannot be performed
-
toPrimitiveArray
Converts the given source object to an array of a primitive type. This method performs some checks whether the source object is already an array of the correct type or a corresponding wrapper type. If not, all values are extracted, converted one by one, and stored in a newly created array.- Parameters:
src- the source objectelemClass- the element class of the arrayci- theConfigurationInterpolator- Returns:
- the result array
- Throws:
ConversionException- if a conversion cannot be performed
-