Class EnumParser<T extends Enum<T>>

java.lang.Object
org.klojang.convert.EnumParser<T>
Type Parameters:
T - The type of the enum

public final class EnumParser<T extends Enum<T>> extends Object
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
  • Field Details

    • DEFAULT_NORMALIZER

      public static final UnaryOperator<String> DEFAULT_NORMALIZER
      The default normalization function. Removes spaces, hyphens and underscores and returns an all-lowercase string.
  • Constructor Details

    • EnumParser

      public EnumParser(Class<T> enumClass)
      Creates an EnumParser for the specified enum class, using the DEFAULT_NORMALIZER.
      Parameters:
      enumClass - The enum class
    • EnumParser

      public EnumParser(Class<T> enumClass, UnaryOperator<String> normalizer)
      Creates an EnumParser for the specified enum class, using the specified normalizer to normalize the strings to be parsed.
      Parameters:
      enumClass - the enum class managed by this EnumParser
      normalizer - the normalization function
    • EnumParser

      public EnumParser(Class<T> enumClass, UnaryOperator<String> normalizer, Set<EnumParser.ParseTarget> parseTargets)
      Creates an EnumParser for the specified enum class, using the specified normalizer to normalize the strings to be parsed.
      Parameters:
      enumClass - the enum class managed by this EnumParser
      normalizer - the normalization function
      parseTargets - 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

      public T parse(Object value) throws TypeConversionException
      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 was null or could not be mapped to one of the enum's constants.