Package dev.orne.beans.converters
Class AbstractDateTimeConverter
java.lang.Object
org.apache.commons.beanutils.converters.AbstractConverter
dev.orne.beans.converters.AbstractDateTimeConverter
- All Implemented Interfaces:
org.apache.commons.beanutils.Converter
- Direct Known Subclasses:
DayOfWeekConverter
,InstantConverter
,LocalDateConverter
,LocalDateTimeConverter
,LocalTimeConverter
,MonthConverter
,MonthDayConverter
,OffsetDateTimeConverter
,OffsetTimeConverter
,YearConverter
,YearMonthConverter
,ZonedDateTimeConverter
,ZoneOffsetConverter
@API(status=STABLE,
since="0.1")
public abstract class AbstractDateTimeConverter
extends org.apache.commons.beanutils.converters.AbstractConverter
Implementation of
Converter
that converts LocalDate
instances
to and from String
using ISO-8601 as String
representation by default.- Since:
- 0.1
- Version:
- 1.0, 2020-05
- Author:
- (w) Iker Hernaez
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractDateTimeConverter
(@NotNull DateTimeFormatter formatter) Creates a new instance that throws aConversionException
if an error occurs.protected
AbstractDateTimeConverter
(@NotNull DateTimeFormatter formatter, TemporalAccessor defaultValue) Creates a new instance that returns a default value if an error occurs. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addParsers
(@NotNull DateTimeFormatter... parsers) Adds specified parsers to the temporal value parsers.void
addParsers
(@NotNull Collection<DateTimeFormatter> parsers) Adds specified parsers to the temporal value parsers.protected String
convertToString
(Object value) protected <T> T
convertToType
(@NotNull Class<T> type, Object value) protected abstract <T extends TemporalAccessor>
TfromTemporalAccessor
(@NotNull Class<T> type, @NotNull TemporalAccessor value) Converts theTemporalAccessor
passed as argument to the specified type.protected abstract Class<?>
@NotNull DateTimeFormatter
Returns the temporal value formatter and default parser.protected @NotNull org.slf4j.Logger
Returns the logger for this instance@NotNull List<DateTimeFormatter>
Returns the extra temporal value parsers.protected <T extends TemporalAccessor>
Tparse
(@NotNull Class<T> type, @NotNull DateTimeFormatter parser, @NotNull String value) Tries to parse aTemporalAccessor
of the specified type from the specified value.protected <T extends TemporalAccessor>
TparseString
(@NotNull Class<T> type, @NotNull String value) Tries to parse aTemporalAccessor
of the specified type from the specifiedString
value.void
setParsers
(@NotNull DateTimeFormatter... parsers) Sets the temporal value parsers.void
setParsers
(@NotNull Collection<DateTimeFormatter> parsers) Sets the temporal value parsers.Methods inherited from class org.apache.commons.beanutils.converters.AbstractConverter
conversionException, convert, convertArray, getDefault, handleError, handleMissing, isUseDefault, setDefaultValue, toString
-
Constructor Details
-
AbstractDateTimeConverter
Creates a new instance that throws aConversionException
if an error occurs.- Parameters:
formatter
- The temporal value formatter and default parser
-
AbstractDateTimeConverter
protected AbstractDateTimeConverter(@NotNull @NotNull DateTimeFormatter formatter, TemporalAccessor defaultValue) Creates a new instance that returns a default value if an error occurs.- Parameters:
formatter
- The temporal value formatter and default parserdefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value
-
-
Method Details
-
getFormatter
Returns the temporal value formatter and default parser.- Returns:
- The temporal value formatter and default parser
-
getParsers
Returns the extra temporal value parsers.- Returns:
- The extra temporal value parsers
-
setParsers
Sets the temporal value parsers.- Parameters:
parsers
- The temporal value parsers
-
setParsers
Sets the temporal value parsers.- Parameters:
parsers
- The temporal value parsers
-
addParsers
Adds specified parsers to the temporal value parsers.- Parameters:
parsers
- The temporal value parsers to add
-
addParsers
Adds specified parsers to the temporal value parsers.- Parameters:
parsers
- The temporal value parsers to add
-
getDefaultType
- Specified by:
getDefaultType
in classorg.apache.commons.beanutils.converters.AbstractConverter
-
convertToType
- Specified by:
convertToType
in classorg.apache.commons.beanutils.converters.AbstractConverter
-
parseString
protected <T extends TemporalAccessor> T parseString(@NotNull @NotNull Class<T> type, @NotNull @NotNull String value) Tries to parse aTemporalAccessor
of the specified type from the specifiedString
value. Tries the parses in order callingparse(Class, DateTimeFormatter, String)
. If all parsers fail throws exception thrown by first parser.- Type Parameters:
T
- The expected type ofTemporalAccessor
- Parameters:
type
- The expected type ofTemporalAccessor
value
- The value to parse- Returns:
- The parsed
TemporalAccessor
of the expected type - Throws:
DateTimeException
- If the value cannot be parsed
-
parse
protected <T extends TemporalAccessor> T parse(@NotNull @NotNull Class<T> type, @NotNull @NotNull DateTimeFormatter parser, @NotNull @NotNull String value) Tries to parse aTemporalAccessor
of the specified type from the specified value.- Type Parameters:
T
- The expected type ofTemporalAccessor
- Parameters:
type
- The expected type ofTemporalAccessor
parser
- The parser to use for parsing the valuevalue
- The value to parse- Returns:
- The parsed
TemporalAccessor
of the expected type - Throws:
DateTimeException
- If the value cannot be parsed
-
fromTemporalAccessor
protected abstract <T extends TemporalAccessor> T fromTemporalAccessor(@NotNull @NotNull Class<T> type, @NotNull @NotNull TemporalAccessor value) Converts theTemporalAccessor
passed as argument to the specified type.- Type Parameters:
T
- The expected type ofTemporalAccessor
- Parameters:
type
- The expected type ofTemporalAccessor
value
- TheTemporalAccessor
to convert to the expected type- Returns:
- The converted
TemporalAccessor
of the expected type - Throws:
DateTimeException
- If the value cannot be converted
-
convertToString
- Overrides:
convertToString
in classorg.apache.commons.beanutils.converters.AbstractConverter
-
getLogger
@NotNull protected @NotNull org.slf4j.Logger getLogger()Returns the logger for this instance- Returns:
- The logger for this instance
-