Package dev.orne.beans.converters
Class EnumTypedConverter<E extends Enum<E>>
java.lang.Object
org.apache.commons.beanutils.converters.AbstractConverter
dev.orne.beans.converters.EnumTypedConverter<E>
- Type Parameters:
E
- The type of enumeration this instance converts
- All Implemented Interfaces:
org.apache.commons.beanutils.Converter
@API(status=STABLE,
since="0.1")
public class EnumTypedConverter<E extends Enum<E>>
extends org.apache.commons.beanutils.converters.AbstractConverter
Implementation of
Converter
that converts Enum
instances
to and from String
using value name as String
representation.- Since:
- 0.1
- Version:
- 1.1, 2022-10
- Author:
- (w) Iker Hernaez
-
Constructor Summary
ConstructorDescriptionEnumTypedConverter
(@NotNull Class<E> enumType) Creates a new instance that throws aConversionException
if an error occurs.EnumTypedConverter
(@NotNull Class<E> enumType, @NotNull BiFunction<Class<E>, String, E> stringToEnum, @NotNull Function<E, String> enumToString) Creates a new instance that throws aConversionException
if an error occurs.EnumTypedConverter
(@NotNull Class<E> enumType, @NotNull BiFunction<Class<E>, String, E> stringToEnum, @NotNull Function<E, String> enumToString, E defaultValue) Creates a new instance that returns a default value if an error occurs.EnumTypedConverter
(@NotNull Class<E> enumType, E defaultValue) Creates a new instance that returns a default value if an error occurs. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
convertToString
(Object value) protected <T> T
convertToType
(@NotNull Class<T> type, Object value) protected @NotNull Class<?>
static <T extends Enum<T>>
EnumTypedConverter<T>Creates a new instance that throws aConversionException
if an error occurs.static <T extends Enum<T>>
EnumTypedConverter<T>Creates a new instance that returns a default value if an error occurs.static <T extends Enum<T>>
voidregisterFor
(Class<T> enumType) Registers a newEnumTypedConverter
for the specified enumeration type that throws aConversionException
if an error occurs to the singleton Apache commons bean utils converter.static <T extends Enum<T>>
voidregisterFor
(Class<T> enumType, T defaultValue) Registers a newEnumTypedConverter
for the specified enumeration type that returns a default value if an error occurs to the singleton Apache commons bean utils converter.static <T extends Enum<T>>
voidregisterFor
(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType) Registers a newEnumTypedConverter
for the specified enumeration type that throws aConversionException
if an error occurs to the specified Apache commons bean utils converter.static <T extends Enum<T>>
voidregisterFor
(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType, T defaultValue) Registers a newEnumTypedConverter
for the specified enumeration type that returns a default value if an error occurs to the specified Apache commons beanutils2 converter.Methods inherited from class org.apache.commons.beanutils.converters.AbstractConverter
conversionException, convert, convertArray, getDefault, handleError, handleMissing, isUseDefault, setDefaultValue, toString
-
Constructor Details
-
EnumTypedConverter
Creates a new instance that throws aConversionException
if an error occurs.- Parameters:
enumType
- The type of enumeration this instance converts
-
EnumTypedConverter
Creates a new instance that returns a default value if an error occurs.- Parameters:
enumType
- The type of enumeration this instance convertsdefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value
-
EnumTypedConverter
public EnumTypedConverter(@NotNull @NotNull Class<E> enumType, @NotNull @NotNull BiFunction<Class<E>, String, E> stringToEnum, @NotNull @NotNull Function<E, String> enumToString) Creates a new instance that throws aConversionException
if an error occurs.- Parameters:
enumType
- The type of enumeration this instance convertsstringToEnum
- TheString
toEnum
conversion functionenumToString
- TheEnum
toString
conversion function
-
EnumTypedConverter
public EnumTypedConverter(@NotNull @NotNull Class<E> enumType, @NotNull @NotNull BiFunction<Class<E>, String, E> stringToEnum, @NotNull @NotNull Function<E, String> enumToString, E defaultValue) Creates a new instance that returns a default value if an error occurs.- Parameters:
enumType
- The type of enumeration this instance convertsstringToEnum
- TheString
toEnum
conversion functionenumToString
- TheEnum
toString
conversion functiondefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value
-
-
Method Details
-
of
Creates a new instance that throws aConversionException
if an error occurs.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
enumType
- The type of enumeration this instance converts- Returns:
- The created converter
-
of
Creates a new instance that returns a default value if an error occurs.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
enumType
- The type of enumeration this instance convertsdefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value- Returns:
- The created converter
-
registerFor
Registers a newEnumTypedConverter
for the specified enumeration type that throws aConversionException
if an error occurs to the singleton Apache commons bean utils converter.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
enumType
- The type of enumeration this instance converts
-
registerFor
public static <T extends Enum<T>> void registerFor(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType) Registers a newEnumTypedConverter
for the specified enumeration type that throws aConversionException
if an error occurs to the specified Apache commons bean utils converter.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
converter
- The Apache commons bean utils converter to register toenumType
- The type of enumeration this instance converts
-
registerFor
Registers a newEnumTypedConverter
for the specified enumeration type that returns a default value if an error occurs to the singleton Apache commons bean utils converter.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
enumType
- The type of enumeration this instance convertsdefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value
-
registerFor
public static <T extends Enum<T>> void registerFor(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType, T defaultValue) Registers a newEnumTypedConverter
for the specified enumeration type that returns a default value if an error occurs to the specified Apache commons beanutils2 converter.- Type Parameters:
T
- The type of enumeration this instance converts- Parameters:
converter
- The Apache commons beanutils2 converter to register toenumType
- The type of enumeration this instance convertsdefaultValue
- The default value to be returned if the value to be converted is missing or an error occurs converting the value
-
getDefaultType
- Specified by:
getDefaultType
in classorg.apache.commons.beanutils.converters.AbstractConverter
-
convertToType
- Specified by:
convertToType
in classorg.apache.commons.beanutils.converters.AbstractConverter
- Throws:
Throwable
-
convertToString
- Overrides:
convertToString
in classorg.apache.commons.beanutils.converters.AbstractConverter
- Throws:
Throwable
-