Interface ObjectParser<T>

All Known Implementing Classes:
ExpressionParser, ObjectParser.SourceParser

public interface ObjectParser<T>
Parser that turns a string into an object. Several static fields provide default parsers for various types (strings, booleans, integers, and doubles), and the value null.

In one use case, the caller will maintain a list of parsers and call appliesTo(String) to determine if a parser in the list should be used. If so, the caller will then call matches(String) to check for syntax errors and then call parse(String) to obtain the value. parse(String) may throw an exception of the string cannot be parsed (for example, when the return value is an Integer whose value is below Integer.MIN_VALUE or above Integer.MAX_VALUE).

This interface was created to support the class JSUtilities.YAML and is public because it may have other uses.

  • Field Details

    • STRING

      static final ObjectParser<? super String> STRING
      A parser for strings. This merely returns the string.
    • NULL

      static final ObjectParser<Object> NULL
      A parser for 'null'. The allowed value is just 'null', and the parsed value is null.
    • BOOLEAN

      static final ObjectParser<? super Boolean> BOOLEAN
      A parser for booleans. The allowable values are true and false, and are case sensitive.
    • LONG

      static final ObjectParser<? super Long> LONG
      A parser for long integers.
    • INTEGER

      static final ObjectParser<? super Integer> INTEGER
      A parser for integers.
    • INTLONG

      static final ObjectParser<? super Number> INTLONG
      A parser for integers or longs. An Integer will be returned unless the value cannot be represented as an Integer, in which case a Long will be returned.
    • DOUBLE

      static final ObjectParser<? super Double> DOUBLE
      A parser for double-precision numbers.
    • NUMBER

      static final ObjectParser<? super Number> NUMBER
      A parser for numbers. The number that the parse(String) method returns will be a subclass of Number: either an Integer, a Long, or a Double, whichever is the most specific class that will represent the number provided.
  • Method Details

    • appliesTo

      default boolean appliesTo(String s)
      Determine this parser applies to a given string.

      The default implementation simply calls matches(String) and returns the results of calling that method. This method should be overridden when less restrictive criteria can be used. For example, a parser that provides a Color might accept strings that start with 'rgb(', 'rgba(', etc. Then a syntax error can be reported if matches(String) returns false.

      Parameters:
      s - a string to parse
      Returns:
      true if this string should be used with this parser; false otherwise
    • matches

      boolean matches(String s)
      Determine if a string is syntactically valid.
      Parameters:
      s - the string
      Returns:
      true if the test succeeds; false if it fails
    • parse

      T parse(String s) throws ObjectParser.Exception
      Parse a string and return the corresponding object.
      Parameters:
      s - the string
      Returns:
      the corresponding object
      Throws:
      ObjectParser.Exception - if the string could not be successfully parsed or the object could not be created