Class StringToEnumConverter<E extends Enum<E>>

java.lang.Object
com.oorian.data.converter.StringToEnumConverter<E>
Type Parameters:
E - the enum type
All Implemented Interfaces:
Converter<String,E>

public class StringToEnumConverter<E extends Enum<E>> extends Object implements Converter<String,E>
Converts between String and any Enum type.

Conversion to model uses Enum.valueOf(Class, String). Conversion to presentation uses Enum.name(). The comparison is case-sensitive by default; use the case-insensitive constructor for lenient matching.

Usage:


 Converter<String, Status> converter = new StringToEnumConverter<>(Status.class);
 Status value = converter.convertToModel("ACTIVE");    // returns Status.ACTIVE
 String text = converter.convertToPresentation(Status.ACTIVE); // returns "ACTIVE"
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
  • Constructor Details

    • StringToEnumConverter

      public StringToEnumConverter(Class<E> enumType)
      Creates a new converter for the specified enum type (case-sensitive).
      Parameters:
      enumType - the enum class
    • StringToEnumConverter

      public StringToEnumConverter(Class<E> enumType, boolean caseInsensitive)
      Creates a new converter for the specified enum type with optional case-insensitive matching.
      Parameters:
      enumType - the enum class
      caseInsensitive - true for case-insensitive matching
    • StringToEnumConverter

      public StringToEnumConverter(Class<E> enumType, boolean caseInsensitive, String errorMessage)
      Creates a new converter with a custom error message.
      Parameters:
      enumType - the enum class
      caseInsensitive - true for case-insensitive matching
      errorMessage - the error message shown when conversion fails
  • Method Details

    • convertToModel

      public E convertToModel(String value) throws ConversionException
      Description copied from interface: Converter
      Converts a presentation value to its model representation.
      Specified by:
      convertToModel in interface Converter<String,E extends Enum<E>>
      Parameters:
      value - the presentation value to convert (may be null)
      Returns:
      the model value
      Throws:
      ConversionException - if conversion fails
    • convertToPresentation

      public String convertToPresentation(E value)
      Description copied from interface: Converter
      Converts a model value to its presentation representation.
      Specified by:
      convertToPresentation in interface Converter<String,E extends Enum<E>>
      Parameters:
      value - the model value to convert (may be null)
      Returns:
      the presentation value