Package it.unimi.dsi.lang
Class EnumStringParser<E extends Enum<E>>
- java.lang.Object
-
- com.martiansoftware.jsap.StringParser
-
- it.unimi.dsi.lang.EnumStringParser<E>
-
public class EnumStringParser<E extends Enum<E>> extends StringParser
AStringParserthat makes the user choose among items of a Javaenum.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
ExampleEnumwith an itemAthat 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 Type Method Description static <E extends Enum<E>>
EnumStringParser<E>getParser(Class<E> enumClass)Returns an enum parser that does not normalize to upper case.static <E extends Enum<E>>
EnumStringParser<E>getParser(Class<E> enumClass, boolean toUpper)Returns an enum parser.Eparse(String s)Returns the enum item obtained by passing the argument toEnum.valueOf(Class, String).-
Methods inherited from class com.martiansoftware.jsap.StringParser
setUp, tearDown
-
-
-
-
Method Detail
-
parse
public E parse(String s) throws ParseException
Returns the enum item obtained by passing the argument toEnum.valueOf(Class, String).- Specified by:
parsein 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- anenumclass 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- anenumclass whose values- Throws:
IllegalArgumentException
-
-