Class EnumParser<T extends Enum<T>>
java.lang.Object
org.klojang.convert.EnumParser<T>
- Type Parameters:
T
- The type of theenum
Parses strings into enum constants. Internally
EnumParser
maintains a
string-to-enum map containing normalized versions of Enum.name()
and
Enum.toString()
as keys. The strings to be parsed are normalized using the same
normalization function, and then looked up in the map. The normalizer function is
customizable. Note that the parse
method takes an argument of
type Object
(rather than String
). You can, in fact, instruct the parser
to be prepared for receiving the ordinal value of an enum constant. You can even
instruct it to be prepared for simply receiving an enum constant itself. This is may be
useful in dynamic contexts where it is not known beforehand whether the incoming value
perhaps already is (or has been converted to) an enum constant. By default, the parser
will be on the lookout for the name, the ordinal value and the string representation of
the enum constants.
enum TransportType { CAR, BIKE, TRAIN; private static EnumParser<TransportType> parser = new EnumParser(TransportType.class); @JsonCreator public static TransportType parse(String input) { return parser.parse(input); } }
- Author:
- Ayco Holleman
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Symbolic constants for what the value to be converted represents. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final UnaryOperator
<String> The default normalization function. -
Constructor Summary
ConstructorsConstructorDescriptionEnumParser
(Class<T> enumClass) Creates anEnumParser
for the specified enum class, using theDEFAULT_NORMALIZER
.EnumParser
(Class<T> enumClass, UnaryOperator<String> normalizer) Creates anEnumParser
for the specified enum class, using the specifiednormalizer
to normalize the strings to be parsed.EnumParser
(Class<T> enumClass, UnaryOperator<String> normalizer, Set<EnumParser.ParseTarget> parseTargets) Creates anEnumParser
for the specified enum class, using the specifiednormalizer
to normalize the strings to be parsed. -
Method Summary
-
Field Details
-
DEFAULT_NORMALIZER
The default normalization function. Removes spaces, hyphens and underscores and returns an all-lowercase string.
-
-
Constructor Details
-
EnumParser
Creates anEnumParser
for the specified enum class, using theDEFAULT_NORMALIZER
.- Parameters:
enumClass
- The enum class
-
EnumParser
Creates anEnumParser
for the specified enum class, using the specifiednormalizer
to normalize the strings to be parsed.- Parameters:
enumClass
- the enum class managed by thisEnumParser
normalizer
- the normalization function
-
EnumParser
public EnumParser(Class<T> enumClass, UnaryOperator<String> normalizer, Set<EnumParser.ParseTarget> parseTargets) Creates anEnumParser
for the specified enum class, using the specifiednormalizer
to normalize the strings to be parsed.- Parameters:
enumClass
- the enum class managed by thisEnumParser
normalizer
- the normalization functionparseTargets
- the aspects of an enum constant that the values to be converted may represent (the constant's name, ordinal value, string representation, or the constant itself).
-
-
Method Details
-
parse
Parses the specified value into an enum constant.- Parameters:
value
- The value to be mapped an enum constant.- Returns:
- The enum constant
- Throws:
TypeConversionException
- If the value wasnull
or could not be mapped to one of the enum's constants.
-