Class ConverterManager
This class enables additional conversion classes to be added via
addInstantConverter(InstantConverter)
, which may replace an
existing converter. Similar methods exist for duration, time period and
interval converters.
This class is threadsafe, so adding/removing converters can be done at any time. Updating the set of converters is relatively expensive, and so should not be performed often.
The default instant converters are:
- ReadableInstant
- String
- Calendar
- Date (includes sql package subclasses)
- Long (milliseconds)
- null (now)
- ReadablePartial
- ReadableInstant
- String
- Calendar
- Date (includes sql package subclasses)
- Long (milliseconds)
- null (now)
- ReadableDuration
- ReadableInterval
- String
- Long (milliseconds)
- null (zero ms)
- ReadablePeriod
- ReadableInterval
- String
- null (zero)
- ReadableInterval
- String
- null (zero-length from now to now)
- Since:
- 1.0
- Author:
- Stephen Colebourne, Brian S O'Neill
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddDurationConverter
(DurationConverter converter) Adds a converter to the set of converters.addInstantConverter
(InstantConverter converter) Adds a converter to the set of converters.addIntervalConverter
(IntervalConverter converter) Adds a converter to the set of converters.addPartialConverter
(PartialConverter converter) Adds a converter to the set of converters.addPeriodConverter
(PeriodConverter converter) Adds a converter to the set of converters.getDurationConverter
(Object object) Gets the best converter for the object specified.Gets a copy of the list of converters.static ConverterManager
getInstantConverter
(Object object) Gets the best converter for the object specified.Gets a copy of the set of converters.getIntervalConverter
(Object object) Gets the best converter for the object specified.Gets a copy of the list of converters.getPartialConverter
(Object object) Gets the best converter for the object specified.Gets a copy of the set of converters.getPeriodConverter
(Object object) Gets the best converter for the object specified.Gets a copy of the list of converters.removeDurationConverter
(DurationConverter converter) Removes a converter from the set of converters.removeInstantConverter
(InstantConverter converter) Removes a converter from the set of converters.removeIntervalConverter
(IntervalConverter converter) Removes a converter from the set of converters.removePartialConverter
(PartialConverter converter) Removes a converter from the set of converters.removePeriodConverter
(PeriodConverter converter) Removes a converter from the set of converters.toString()
Gets a debug representation of the object.
-
Constructor Details
-
ConverterManager
protected ConverterManager()Restricted constructor.
-
-
Method Details
-
getInstance
-
getInstantConverter
Gets the best converter for the object specified.- Parameters:
object
- the object to convert- Returns:
- the converter to use
- Throws:
IllegalArgumentException
- if no suitable converterIllegalStateException
- if multiple converters match the type equally well
-
getInstantConverters
Gets a copy of the set of converters.- Returns:
- the converters, a copy of the real data, never null
-
addInstantConverter
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.The order in which converters are added is not relevant. The best converter is selected by examining the object hierarchy.
- Parameters:
converter
- the converter to add, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
removeInstantConverter
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.- Parameters:
converter
- the converter to remove, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
getPartialConverter
Gets the best converter for the object specified.- Parameters:
object
- the object to convert- Returns:
- the converter to use
- Throws:
IllegalArgumentException
- if no suitable converterIllegalStateException
- if multiple converters match the type equally well
-
getPartialConverters
Gets a copy of the set of converters.- Returns:
- the converters, a copy of the real data, never null
-
addPartialConverter
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.The order in which converters are added is not relevant. The best converter is selected by examining the object hierarchy.
- Parameters:
converter
- the converter to add, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
removePartialConverter
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.- Parameters:
converter
- the converter to remove, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
getDurationConverter
Gets the best converter for the object specified.- Parameters:
object
- the object to convert- Returns:
- the converter to use
- Throws:
IllegalArgumentException
- if no suitable converterIllegalStateException
- if multiple converters match the type equally well
-
getDurationConverters
Gets a copy of the list of converters.- Returns:
- the converters, a copy of the real data, never null
-
addDurationConverter
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.The order in which converters are added is not relevant. The best converter is selected by examining the object hierarchy.
- Parameters:
converter
- the converter to add, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
removeDurationConverter
public DurationConverter removeDurationConverter(DurationConverter converter) throws SecurityException Removes a converter from the set of converters. If the converter was not in the set, no changes are made.- Parameters:
converter
- the converter to remove, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
getPeriodConverter
Gets the best converter for the object specified.- Parameters:
object
- the object to convert- Returns:
- the converter to use
- Throws:
IllegalArgumentException
- if no suitable converterIllegalStateException
- if multiple converters match the type equally well
-
getPeriodConverters
Gets a copy of the list of converters.- Returns:
- the converters, a copy of the real data, never null
-
addPeriodConverter
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.The order in which converters are added is not relevant. The best converter is selected by examining the object hierarchy.
- Parameters:
converter
- the converter to add, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
removePeriodConverter
Removes a converter from the set of converters. If the converter was not in the set, no changes are made.- Parameters:
converter
- the converter to remove, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
getIntervalConverter
Gets the best converter for the object specified.- Parameters:
object
- the object to convert- Returns:
- the converter to use
- Throws:
IllegalArgumentException
- if no suitable converterIllegalStateException
- if multiple converters match the type equally well
-
getIntervalConverters
Gets a copy of the list of converters.- Returns:
- the converters, a copy of the real data, never null
-
addIntervalConverter
Adds a converter to the set of converters. If a matching converter is already in the set, the given converter replaces it. If the converter is exactly the same as one already in the set, no changes are made.The order in which converters are added is not relevant. The best converter is selected by examining the object hierarchy.
- Parameters:
converter
- the converter to add, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
removeIntervalConverter
public IntervalConverter removeIntervalConverter(IntervalConverter converter) throws SecurityException Removes a converter from the set of converters. If the converter was not in the set, no changes are made.- Parameters:
converter
- the converter to remove, null ignored- Returns:
- replaced converter, or null
- Throws:
SecurityException
-
toString
Gets a debug representation of the object.
-