Package com.oorian.data.converter
Class StringToEnumConverter<E extends Enum<E>>
java.lang.Object
com.oorian.data.converter.StringToEnumConverter<E>
- Type Parameters:
E- the enum type
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 Summary
ConstructorsConstructorDescriptionStringToEnumConverter(Class<E> enumType) Creates a new converter for the specified enum type (case-sensitive).StringToEnumConverter(Class<E> enumType, boolean caseInsensitive) Creates a new converter for the specified enum type with optional case-insensitive matching.StringToEnumConverter(Class<E> enumType, boolean caseInsensitive, String errorMessage) Creates a new converter with a custom error message. -
Method Summary
Modifier and TypeMethodDescriptionconvertToModel(String value) Converts a presentation value to its model representation.convertToPresentation(E value) Converts a model value to its presentation representation.
-
Constructor Details
-
StringToEnumConverter
Creates a new converter for the specified enum type (case-sensitive).- Parameters:
enumType- the enum class
-
StringToEnumConverter
Creates a new converter for the specified enum type with optional case-insensitive matching.- Parameters:
enumType- the enum classcaseInsensitive-truefor case-insensitive matching
-
StringToEnumConverter
Creates a new converter with a custom error message.- Parameters:
enumType- the enum classcaseInsensitive-truefor case-insensitive matchingerrorMessage- the error message shown when conversion fails
-
-
Method Details
-
convertToModel
Description copied from interface:ConverterConverts a presentation value to its model representation.- Specified by:
convertToModelin interfaceConverter<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
Description copied from interface:ConverterConverts a model value to its presentation representation.
-