Interface ExpressionParser

All Known Implementing Classes:
JELParser, JEPEmulationParser, JEPParser

public interface ExpressionParser

The ExpressionParser class defines an interface for all classes that parse and evaluate mathematical expressions. The expression parser must initialize, parse, and then evaluate in that order. The list of variables is only available via getVariables() after parsing is complete.

Expressions evaluate to a primitive type given by the getResultType() method. The result can be retrieved using the evaluate(noaa.coastwatch.util.expression.EvaluateImp) method to a primitive wrapper, or the extra overhead of creating a wrapped primitive can be avoided by using one of the special primitive evaluate methods to directly retrieve the result. If the result type does not match the method called, zero is returned (or false for a boolean).

Since:
3.4.0
Author:
Peter Hollemans
  • Method Details

    • isThreadSafe

      default boolean isThreadSafe()
      Determines if this parser instance is thread-safe.
      Returns:
      true is this parser instance is thread-safe, or false if not.
    • init

      void init(ParseImp parseImp)
      Initializes this parser with the specified implementation.
      Parameters:
      parseImp - the parser implementation to use for variable information during parsing.
    • parse

      void parse(String expr)
      Parses the specified expression.
      Parameters:
      expr - the expression to parse.
      Throws:
      RuntimeException - if an error occurred parsing the expression.
    • adapt

      default void adapt(ExpressionParser.ResultType type)
      Adapts the parsed expression to a specific result type.
      Parameters:
      type - the result type to adapt the expression. The result type of the parser is set to the specified type.
      Throws:
      UnsupportedOperationException - if the result type of the parser cannot be adapted.
    • translate

      default String translate(String expr)
      Translates the specified expression to Java Language syntax if possible.
      Parameters:
      expr - the expression to translate.
      Throws:
      UnsupportedOperationException - if the expression cannot be translated, or expression translation is not supported.
    • getParseTree

      default Document getParseTree()
      Gets a document tree corresponding to the parsed expression.
      Returns:
      the document tree.
    • getResultType

      Gets the result data type.
      Returns:
      the result data type.
    • getVariables

      List<String> getVariables()
      Gets the list of variables used in the expression.
      Returns:
      the list of variable names.
    • getVariableType

      String getVariableType(String name)
      Gets the variable type used in the expression.
      Parameters:
      name - the variable name from the list returned by getVariables().
      Returns:
      the variable class name: Byte, Short, Integer, Long, Float, or Double. The returned value is null if the variable type is unknown.
      Since:
      3.8.1
    • evaluate

      Object evaluate(EvaluateImp evalImp)
      Evaluates the expression to a primitive wrapper object.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the result value in a wrapper.
    • evaluateToBoolean

      default boolean evaluateToBoolean(EvaluateImp evalImp)
      Evaluates the expression to a boolean value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the boolean valued result, or false if the result type is not a boolean.
    • evaluateToByte

      default byte evaluateToByte(EvaluateImp evalImp)
      Evaluates the expression to a byte value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the byte valued result, or zero if the result is not a byte.
    • evaluateToShort

      default short evaluateToShort(EvaluateImp evalImp)
      Evaluates the expression to a short value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the short valued result, or zero if the result is not a short.
    • evaluateToInt

      default int evaluateToInt(EvaluateImp evalImp)
      Evaluates the expression to an int value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the int valued result, or zero if the result is not an int.
    • evaluateToLong

      default long evaluateToLong(EvaluateImp evalImp)
      Evaluates the expression to a long value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the long valued result, or zero if the result is not a long.
    • evaluateToFloat

      default float evaluateToFloat(EvaluateImp evalImp)
      Evaluates the expression to a float value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the float valued result, or zero if the result is not a float.
    • evaluateToDouble

      default double evaluateToDouble(EvaluateImp evalImp)
      Evaluates the expression to a double value.
      Parameters:
      evalImp - the evalutation implementation that provides variable values.
      Returns:
      the double valued result, or zero if the result is not a double.