Package noaa.coastwatch.util.expression
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
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
The enumeration of the possible expression result types. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Adapts the parsed expression to a specific result type.evaluate
(EvaluateImp evalImp) Evaluates the expression to a primitive wrapper object.default boolean
evaluateToBoolean
(EvaluateImp evalImp) Evaluates the expression to a boolean value.default byte
evaluateToByte
(EvaluateImp evalImp) Evaluates the expression to a byte value.default double
evaluateToDouble
(EvaluateImp evalImp) Evaluates the expression to a double value.default float
evaluateToFloat
(EvaluateImp evalImp) Evaluates the expression to a float value.default int
evaluateToInt
(EvaluateImp evalImp) Evaluates the expression to an int value.default long
evaluateToLong
(EvaluateImp evalImp) Evaluates the expression to a long value.default short
evaluateToShort
(EvaluateImp evalImp) Evaluates the expression to a short value.default Document
Gets a document tree corresponding to the parsed expression.Gets the result data type.Gets the list of variables used in the expression.void
Initializes this parser with the specified implementation.default boolean
Determines if this parser instance is thread-safe.void
Parses the specified expression.default String
Translates the specified expression to Java Language syntax if possible.
-
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
Initializes this parser with the specified implementation.- Parameters:
parseImp
- the parser implementation to use for variable information during parsing.
-
parse
Parses the specified expression.- Parameters:
expr
- the expression to parse.- Throws:
RuntimeException
- if an error occurred parsing the expression.
-
adapt
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
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
Gets a document tree corresponding to the parsed expression.- Returns:
- the document tree.
-
getResultType
ExpressionParser.ResultType getResultType()Gets the result data type.- Returns:
- the result data type.
-
getVariables
Gets the list of variables used in the expression.- Returns:
- the list of variable names.
-
evaluate
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
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
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
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
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
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
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
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.
-