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

    Constructors
    Constructor
    Description
    EnumTypedConverter(@NotNull Class<E> enumType)
    Creates a new instance that throws a ConversionException 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 a ConversionException 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 Type
    Method
    Description
    protected String
    protected <T> T
    convertToType(@NotNull Class<T> type, Object value)
    protected @NotNull Class<?>
    static <T extends Enum<T>>
    EnumTypedConverter<T>
    of(Class<T> enumType)
    Creates a new instance that throws a ConversionException if an error occurs.
    static <T extends Enum<T>>
    EnumTypedConverter<T>
    of(Class<T> enumType, T defaultValue)
    Creates a new instance that returns a default value if an error occurs.
    static <T extends Enum<T>>
    void
    registerFor(Class<T> enumType)
    Registers a new EnumTypedConverter for the specified enumeration type that throws a ConversionException if an error occurs to the singleton Apache commons bean utils converter.
    static <T extends Enum<T>>
    void
    registerFor(Class<T> enumType, T defaultValue)
    Registers a new EnumTypedConverter 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>>
    void
    registerFor(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType)
    Registers a new EnumTypedConverter for the specified enumeration type that throws a ConversionException if an error occurs to the specified Apache commons bean utils converter.
    static <T extends Enum<T>>
    void
    registerFor(org.apache.commons.beanutils.ConvertUtilsBean converter, Class<T> enumType, T defaultValue)
    Registers a new EnumTypedConverter 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • EnumTypedConverter

      public EnumTypedConverter(@NotNull @NotNull Class<E> enumType)
      Creates a new instance that throws a ConversionException if an error occurs.
      Parameters:
      enumType - The type of enumeration this instance converts
    • EnumTypedConverter

      public EnumTypedConverter(@NotNull @NotNull Class<E> enumType, E defaultValue)
      Creates a new instance that returns a default value if an error occurs.
      Parameters:
      enumType - The type of enumeration this instance converts
      defaultValue - 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 a ConversionException if an error occurs.
      Parameters:
      enumType - The type of enumeration this instance converts
      stringToEnum - The String to Enum conversion function
      enumToString - The Enum to String 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 converts
      stringToEnum - The String to Enum conversion function
      enumToString - The Enum to String conversion function
      defaultValue - The default value to be returned if the value to be converted is missing or an error occurs converting the value
  • Method Details

    • of

      public static <T extends Enum<T>> EnumTypedConverter<T> of(Class<T> enumType)
      Creates a new instance that throws a ConversionException 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

      public static <T extends Enum<T>> EnumTypedConverter<T> of(Class<T> enumType, T defaultValue)
      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 converts
      defaultValue - 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

      public static <T extends Enum<T>> void registerFor(Class<T> enumType)
      Registers a new EnumTypedConverter for the specified enumeration type that throws a ConversionException 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 new EnumTypedConverter for the specified enumeration type that throws a ConversionException 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 to
      enumType - The type of enumeration this instance converts
    • registerFor

      public static <T extends Enum<T>> void registerFor(Class<T> enumType, T defaultValue)
      Registers a new EnumTypedConverter 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 converts
      defaultValue - 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 new EnumTypedConverter 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 to
      enumType - The type of enumeration this instance converts
      defaultValue - The default value to be returned if the value to be converted is missing or an error occurs converting the value
    • getDefaultType

      @NotNull protected @NotNull Class<?> getDefaultType()
      Specified by:
      getDefaultType in class org.apache.commons.beanutils.converters.AbstractConverter
    • convertToType

      protected <T> T convertToType(@NotNull @NotNull Class<T> type, Object value) throws Throwable
      Specified by:
      convertToType in class org.apache.commons.beanutils.converters.AbstractConverter
      Throws:
      Throwable
    • convertToString

      protected String convertToString(Object value) throws Throwable
      Overrides:
      convertToString in class org.apache.commons.beanutils.converters.AbstractConverter
      Throws:
      Throwable