Class EnumStringParser<E extends java.lang.Enum<E>>


  • public class EnumStringParser<E extends java.lang.Enum<E>>
    extends com.martiansoftware.jsap.StringParser
    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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <E extends java.lang.Enum<E>>
      EnumStringParser<E>
      getParser​(java.lang.Class<E> enumClass)
      Returns an enum parser that does not normalize to upper case.
      static <E extends java.lang.Enum<E>>
      EnumStringParser<E>
      getParser​(java.lang.Class<E> enumClass, boolean toUpper)
      Returns an enum parser.
      E parse​(java.lang.String s)
      Returns the enum item obtained by passing the argument to Enum.valueOf(Class, String).
      • Methods inherited from class com.martiansoftware.jsap.StringParser

        setUp, tearDown
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • parse

        public E parse​(java.lang.String s)
                throws com.martiansoftware.jsap.ParseException
        Returns the enum item obtained by passing the argument to Enum.valueOf(Class, String).
        Specified by:
        parse in class com.martiansoftware.jsap.StringParser
        Parameters:
        s - an enum item name.
        Returns:
        the enum item returned by Enum.valueOf(Class, String) (possibly after upper casing s).
        Throws:
        com.martiansoftware.jsap.ParseException
      • getParser

        public static <E extends java.lang.Enum<E>> EnumStringParser<E> getParser​(java.lang.Class<E> enumClass,
                                                                                  boolean toUpper)
                                                                           throws java.lang.IllegalArgumentException
        Returns an enum parser.
        Parameters:
        enumClass - an enum class whose values
        toUpper - tells the parser to upper case the strings to be parsed.
        Throws:
        java.lang.IllegalArgumentException
      • getParser

        public static <E extends java.lang.Enum<E>> EnumStringParser<E> getParser​(java.lang.Class<E> enumClass)
                                                                           throws java.lang.IllegalArgumentException
        Returns an enum parser that does not normalize to upper case.
        Parameters:
        enumClass - an enum class whose values
        Throws:
        java.lang.IllegalArgumentException