org.scijava.sjep
Class Literals

java.lang.Object
  extended by org.scijava.sjep.Literals

public final class Literals
extends 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
static Number parseBinary(CharSequence s)
          Parses a binary literal (e.g., 0b010101000011).
static Number parseBinary(CharSequence s, Position pos)
          Parses a binary literal (e.g., 0b010101000011).
static Boolean parseBoolean(CharSequence s)
          Parses a boolean literal (i.e., true and false).
static Boolean parseBoolean(CharSequence s, Position pos)
          Parses a boolean literal (i.e., true and false).
static Number parseDecimal(CharSequence s)
          Parses a decimal literal (integer or otherwise; e.g., 1234567890, 1234.0987 or 1.2e34).
static Number parseDecimal(CharSequence s, Position pos)
          Parses a decimal literal (e.g., 1234.0987 or 1.2e34).
static Number parseHex(CharSequence s)
          Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
static Number parseHex(CharSequence s, Position pos)
          Parses a hexidecimal literal (e.g., 0xfedcba9876543210).
static Object parseLiteral(CharSequence s)
          Parses a literal of any known type (booleans, strings and numbers).
static Object parseLiteral(CharSequence s, Position pos)
          Parses a literal of any known type (booleans, strings and numbers).
static Number parseNumber(CharSequence s)
          Parses a numeric literal of any known type.
static Number parseNumber(CharSequence s, Position pos)
          Parses a numeric literal of any known type.
static Number parseOctal(CharSequence s)
          Parses an octal literal (e.g., 01234567).
static Number parseOctal(CharSequence s, Position pos)
          Parses an octal literal (e.g., 01234567).
static String parseString(CharSequence s)
          Parses a string literal which is enclosed in single or double quotes.
static String parseString(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 Boolean parseBoolean(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 String parseString(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 Number parseHex(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 Number parseBinary(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 Number parseOctal(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 Number parseDecimal(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 Number parseNumber(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 Object parseLiteral(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 Boolean parseBoolean(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 String parseString(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 Number parseHex(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 Number parseBinary(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 Number parseOctal(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 Number parseDecimal(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 Number parseNumber(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 Object parseLiteral(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)


Copyright © 2015 SciJava. All rights reserved.