Class EnumConversion<T extends Enum<T>>

Type Parameters:
T - the enumeration type whose constants will be converted from/to String
All Implemented Interfaces:
Conversion<String,T>

public class EnumConversion<T extends Enum<T>> extends ObjectConversion<T>
Converts Strings to enumeration constants and vice versa.

This class supports multiple types of identification of enumeration constants. For example, you can match the literal (Enum.name() the ordinal (Enum.ordinal() or the result of a method defined in your enumeration.

The reverse conversion from an enumeration to String (in revert(Enum) will return a String using the first EnumSelector provided in the constructor of this class.

See Also:
  • Field Details

    • enumType

      private final Class<T extends Enum<T>> enumType
    • customEnumField

      private final Field customEnumField
    • customEnumMethod

      private final Method customEnumMethod
    • selectors

      private final EnumSelector[] selectors
    • conversions

      private final Map<String,T extends Enum<T>>[] conversions
  • Constructor Details

    • EnumConversion

      public EnumConversion(Class<T> enumType)
      Defines a conversion for an enumeration type that will attempt to match Strings against the results of the output produced by (Enum.name(), (Enum.ordinal() and (Enum.toString() of each constant of the given enumeration (@link Class.getEnumConstants()).
      Parameters:
      enumType - the enumeration type to be converted from/to String
    • EnumConversion

      public EnumConversion(Class<T> enumType, EnumSelector... selectors)
      Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class.getEnumConstants() should be used to match equivalent Strings.
      Parameters:
      enumType - the enumeration type to be converted from/to String
      selectors - the selection elements of the enumeration to use for matching Strings.
    • EnumConversion

      public EnumConversion(Class<T> enumType, String customEnumElement, EnumSelector... selectors)
      Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class.getEnumConstants() should be used to match equivalent Strings.
      Parameters:
      enumType - the enumeration type to be converted from/to String
      customEnumElement - name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
      selectors - the selection elements of the enumeration to use for matching Strings.
    • EnumConversion

      public EnumConversion(Class<T> enumType, T valueIfStringIsNull, String valueIfEnumIsNull, String customEnumElement, EnumSelector... selectors)
      Defines a conversion for an enumeration type that will attempt to match Strings the list of EnumSelectors, in the specified order. Each EnumSelector identifies which element of each constant of the enumeration class (@link Class.getEnumConstants() should be used to match equivalent Strings.
      Parameters:
      enumType - the enumeration type to be converted from/to String
      valueIfStringIsNull - the default enumeration constant to use if the input String is null
      valueIfEnumIsNull - the default String value to use if the input enum constant is null
      customEnumElement - name of custom element of the enumeration (attribute or method) whose values should be used to match equivalent Strings.
      selectors - the selection elements of the enumeration to use for matching Strings.
  • Method Details

    • initializeMappings

      private void initializeMappings(Set<EnumSelector> conversionTypes)
    • getKey

      private String getKey(T constant, EnumSelector conversionType)
    • revert

      public String revert(T input)
      Description copied from class: ObjectConversion
      Converts a value of type T back to a String

      This is a general implementation that simply returns the result of input.toString()

      Specified by:
      revert in interface Conversion<String,T extends Enum<T>>
      Overrides:
      revert in class ObjectConversion<T extends Enum<T>>
      Parameters:
      input - the input of type T to be converted to a String
      Returns:
      the conversion result, or the value of ObjectConversion.getValueIfObjectIsNull() if the input object is null.
    • fromString

      protected T fromString(String input)
      Description copied from class: ObjectConversion
      Creates an instance of T from a String representation.
      Specified by:
      fromString in class ObjectConversion<T extends Enum<T>>
      Parameters:
      input - The String to be converted to T
      Returns:
      an instance of T, converted from the String input.