Class LongSizeStringParser

java.lang.Object
com.martiansoftware.jsap.StringParser
com.martiansoftware.jsap.stringparsers.LongSizeStringParser

public class LongSizeStringParser extends StringParser
A StringParser that lets the user specify sizes with an optional unit.

This parser will parse its argument using parseSize(CharSequence).

Author:
Sebastiano Vigna
See Also:
  • Method Details

    • getParser

      public static LongSizeStringParser getParser()
      Returns the only instance of a LongSizeStringParser.

      Convenient access to the only instance returned by this method is available through JSAP.LONGSIZE_PARSER.

      Returns:
      the only instance of a LongSizeStringParser.
    • parse

      public Object parse(String arg) throws ParseException
      Description copied from class: StringParser
      Parses the specified argument into an Object of the appropriate type. If the specified argument cannot be converted into the desired Object, a ParseException should be thrown.

      Note: this method MAY BE CALLED with a null argument. Take this into consideration when subclassing!
      Specified by:
      parse in class StringParser
      Parameters:
      arg - the argument to convert to an Object of class appropriate to the StringParser subclass.
      Returns:
      the Object resulting from the parsed argument.
      Throws:
      ParseException - if the specified argument cannot be parsed.
    • parseSize

      public static long parseSize(CharSequence s) throws ParseException
      Parses a size specified using units (e.g., K, Ki, M, Mi,…).

      The argument must be in the form number [unit] (with no space inbetween). number is anything accepted by Long.decode(java.lang.String), which allows, besides decimal numbers, hexadecimal numbers prefixed by 0x, 0X or #, and octal numbers prefixed by 0. unit may be one of K (103), Ki (210), M (106), Mi (220), G (109), Gi (230), T (1012), Ti (240), P (1015), Pi (250). Thus, for instance, 1Ki is 1024, whereas 9M is nine millions and 0x10Pi is 18014398509481984. Note that in the number part case does not matter, but in the unit part it does.

      Parameters:
      s - a size specified as above.
      Returns:
      the corresponding unitless size.
      Throws:
      ParseException - if s is malformed, number is negative or the resulting size is larger than Long.MAX_VALUE.