Class Literals


  • public final class Literals
    extends java.lang.Object
    Utility methods for parsing literals from strings. These methods largely conform to the Java specification's ideas of what constitutes a numeric or string literal.
    Author:
    Curtis Rueden
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Number parseBinary​(java.lang.CharSequence s)
      Parses a binary literal (e.g., 0b010101000011).
      static java.lang.Number parseBinary​(java.lang.CharSequence s, Position pos)
      Parses a binary literal (e.g., 0b010101000011).
      static java.lang.Boolean parseBoolean​(java.lang.CharSequence s)
      Parses a boolean literal (i.e., true and false).
      static java.lang.Boolean parseBoolean​(java.lang.CharSequence s, Position pos)
      Parses a boolean literal (i.e., true and false).
      static java.lang.Number parseDecimal​(java.lang.CharSequence s)
      Parses a decimal literal (integer or otherwise; e.g., 1234567890, 1234.0987 or 1.2e34).
      static java.lang.Number parseDecimal​(java.lang.CharSequence s, Position pos)
      Parses a decimal literal (e.g., 1234.0987 or 1.2e34).
      static java.lang.Number parseHex​(java.lang.CharSequence s)
      Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
      static java.lang.Number parseHex​(java.lang.CharSequence s, Position pos)
      Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
      static java.lang.Object parseLiteral​(java.lang.CharSequence s)
      Parses a literal of any known type (booleans, strings and numbers).
      static java.lang.Object parseLiteral​(java.lang.CharSequence s, Position pos)
      Parses a literal of any known type (booleans, strings and numbers).
      static java.lang.Number parseNumber​(java.lang.CharSequence s)
      Parses a numeric literal of any known type.
      static java.lang.Number parseNumber​(java.lang.CharSequence s, Position pos)
      Parses a numeric literal of any known type.
      static java.lang.Number parseOctal​(java.lang.CharSequence s)
      Parses an octal literal (e.g., 01234567).
      static java.lang.Number parseOctal​(java.lang.CharSequence s, Position pos)
      Parses an octal literal (e.g., 01234567).
      static java.lang.String parseString​(java.lang.CharSequence s)
      Parses a string literal which is enclosed in single or double quotes.
      static java.lang.String parseString​(java.lang.CharSequence s, Position pos)
      Parses a string literal which is enclosed in single or double quotes.
      • Methods inherited from class java.lang.Object

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

      • parseBoolean

        public static java.lang.Boolean parseBoolean​(java.lang.CharSequence s)
        Parses a boolean literal (i.e., true and false).
        Parameters:
        s - The string from which the boolean literal should be parsed.
        Returns:
        The parsed boolean value—either Boolean.TRUE or Boolean.FALSE— or null if the string does not begin with a boolean literal.
      • parseString

        public static java.lang.String parseString​(java.lang.CharSequence s)
        Parses a string literal which is enclosed in single or double quotes.

        For literals in double quotes, this parsing mechanism is intended to be as close as possible to the numeric literals supported by the Java programming language itself. Literals in single quotes are completely verbatim, with no escaping performed.

        Parameters:
        s - The string from which the string literal should be parsed.
        Returns:
        The parsed string value, unescaped according to Java conventions. Returns null if the string does not begin with a single or double quote.
      • parseHex

        public static java.lang.Number parseHex​(java.lang.CharSequence s)
        Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long.
      • parseBinary

        public static java.lang.Number parseBinary​(java.lang.CharSequence s)
        Parses a binary literal (e.g., 0b010101000011).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long.
      • parseOctal

        public static java.lang.Number parseOctal​(java.lang.CharSequence s)
        Parses an octal literal (e.g., 01234567).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long.
      • parseDecimal

        public static java.lang.Number parseDecimal​(java.lang.CharSequence s)
        Parses a decimal literal (integer or otherwise; e.g., 1234567890, 1234.0987 or 1.2e34).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        Returns:
        The parsed numeric value, of a type consistent with Java's support for numeric primitives—or for values outside the normal range of Java primitives, BigInteger or BigDecimal as appropriate. Returns null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseNumber

        public static java.lang.Number parseNumber​(java.lang.CharSequence s)
        Parses a numeric literal of any known type.

        This parsing mechanism is intended to be as close as possible to the numeric literals supported by the Java programming language itself.

        Parameters:
        s - The string from which the numeric literal should be parsed.
        Returns:
        The parsed numeric value, of a type consistent with Java's support for numeric primitives—or for values outside the normal range of Java primitives, BigInteger or BigDecimal as appropriate. Returns null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseLiteral

        public static java.lang.Object parseLiteral​(java.lang.CharSequence s)
        Parses a literal of any known type (booleans, strings and numbers).
        Parameters:
        s - The string from which the literal should be parsed.
        Returns:
        The parsed value, of a type consistent with Java's support for literals: either Boolean, String or a concrete Number subclass. Returns null if the string does not match the syntax of a known literal.
        See Also:
        parseBoolean(CharSequence), parseString(CharSequence), parseNumber(CharSequence)
      • parseBoolean

        public static java.lang.Boolean parseBoolean​(java.lang.CharSequence s,
                                                     Position pos)
        Parses a boolean literal (i.e., true and false).
        Parameters:
        s - The string from which the boolean literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed boolean value—either Boolean.TRUE or Boolean.FALSE— or null if the string does not begin with a boolean literal.
      • parseString

        public static java.lang.String parseString​(java.lang.CharSequence s,
                                                   Position pos)
        Parses a string literal which is enclosed in single or double quotes.

        For literals in double quotes, this parsing mechanism is intended to be as close as possible to the numeric literals supported by the Java programming language itself. Literals in single quotes are completely verbatim, with no escaping performed.

        Parameters:
        s - The string from which the string literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed string value, unescaped according to Java conventions. Returns null if the string does not begin with a single or double quote.
      • parseHex

        public static java.lang.Number parseHex​(java.lang.CharSequence s,
                                                Position pos)
        Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long; or null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseBinary

        public static java.lang.Number parseBinary​(java.lang.CharSequence s,
                                                   Position pos)
        Parses a binary literal (e.g., 0b010101000011).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long; or null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseOctal

        public static java.lang.Number parseOctal​(java.lang.CharSequence s,
                                                  Position pos)
        Parses an octal literal (e.g., 01234567).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed numeric value—an Integer if sufficiently small, or a Long if needed or if the L suffix is given; or a BigInteger if the value is too large even for long; or null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseDecimal

        public static java.lang.Number parseDecimal​(java.lang.CharSequence s,
                                                    Position pos)
        Parses a decimal literal (e.g., 1234.0987 or 1.2e34).
        Parameters:
        s - The string from which the numeric literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed numeric value, of a type consistent with Java's support for numeric primitives—or for values outside the normal range of Java primitives, BigInteger or BigDecimal as appropriate. Returns null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseNumber

        public static java.lang.Number parseNumber​(java.lang.CharSequence s,
                                                   Position pos)
        Parses a numeric literal of any known type.

        This parsing mechanism is intended to be as close as possible to the numeric literals supported by the Java programming language itself.

        Parameters:
        s - The string from which the numeric literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed numeric value, of a type consistent with Java's support for numeric primitives—or for values outside the normal range of Java primitives, BigInteger or BigDecimal as appropriate. Returns null if the string does not begin with the numeric literal telltale of a 0-9 digit with optional minus.
      • parseLiteral

        public static java.lang.Object parseLiteral​(java.lang.CharSequence s,
                                                    Position pos)
        Parses a literal of any known type (booleans, strings and numbers).
        Parameters:
        s - The string from which the literal should be parsed.
        pos - The offset from which the literal should be parsed. If parsing is successful, the position will be advanced to the next index after the parsed literal.
        Returns:
        The parsed value, of a type consistent with Java's support for literals: either Boolean, String or a concrete Number subclass. Returns null if the string does not match the syntax of a known literal.
        See Also:
        parseBoolean(CharSequence, Position), parseString(CharSequence, Position), parseNumber(CharSequence, Position)