Package it.unimi.dsi.lang
Class EnumStringParser<E extends Enum<E>>
java.lang.Object
com.martiansoftware.jsap.StringParser
it.unimi.dsi.lang.EnumStringParser<E>
A
StringParser
that makes the user choose among
items of a Java enum
.
Optionally, parsed strings can be normalized to upper case. Thus, if the enum elements are defined in uppercase, the parser will be in practice case-independent.
A typical usage example for an ExampleEnum
with an item A
that is going to be the default:
new FlaggedOption("example", EnumStringParser.getParser(ExampleEnum.class, true), ExampleEnum.A.name(), JSAP.NOT_REQUIRED, 'e', "example", Arrays.toString(ExampleEnum.values()))
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E extends Enum<E>>
EnumStringParser<E>Returns an enum parser that does not normalize to upper case.static <E extends Enum<E>>
EnumStringParser<E>Returns an enum parser.Returns the enum item obtained by passing the argument toEnum.valueOf(Class, String)
.Methods inherited from class com.martiansoftware.jsap.StringParser
setUp, tearDown
-
Method Details
-
parse
Returns the enum item obtained by passing the argument toEnum.valueOf(Class, String)
.- Specified by:
parse
in classStringParser
- Parameters:
s
- an enum item name.- Returns:
- the enum item returned by
Enum.valueOf(Class, String)
(possibly after upper casings
). - Throws:
ParseException
-
getParser
public static <E extends Enum<E>> EnumStringParser<E> getParser(Class<E> enumClass, boolean toUpper) throws IllegalArgumentException Returns an enum parser.- Parameters:
enumClass
- anenum
class whose valuestoUpper
- tells the parser to upper case the strings to be parsed.- Throws:
IllegalArgumentException
-
getParser
public static <E extends Enum<E>> EnumStringParser<E> getParser(Class<E> enumClass) throws IllegalArgumentException Returns an enum parser that does not normalize to upper case.- Parameters:
enumClass
- anenum
class whose values- Throws:
IllegalArgumentException
-