public class ExpressionEvaluator
extends java.lang.Object
evaluate(String, Mapping))parse(String)) into an Expression object for repeated
evaluation (see Expression.evaluate(Mapping)).
| Modifier and Type | Field and Description |
|---|---|
static java.util.Set<java.lang.Object> |
FALSES
All values the are implicitly regarded als
false:
null
""
Boolean.FALSE
(byte) 0
(short) 0
(integer) 0
(long) 0
|
| Constructor and Description |
|---|
ExpressionEvaluator(java.util.Collection<java.lang.String> variableNames) |
ExpressionEvaluator(Predicate<? super java.lang.String> isValidVariableName) |
ExpressionEvaluator(java.lang.String... variableNames) |
| Modifier and Type | Method and Description |
|---|---|
protected static java.lang.Object |
binaryNumericPromotion(java.lang.Object value,
java.lang.Object other)
Converts and returns the
value to Integer, Long, Float, Double or String as appropriate for comparison with other. |
java.lang.Object |
evaluate(java.lang.String spec,
Mapping<java.lang.String,?> variables)
Scans, parses and evaluates an expression.
|
<T> T |
evaluateTo(java.lang.String spec,
Mapping<java.lang.String,?> variables,
java.lang.Class<T> targetType)
Scans, parses, evaluates and returns an expression.
|
boolean |
evaluateToBoolean(java.lang.String spec,
Mapping<java.lang.String,?> variables)
Scans, parses and evaluates an expression.
|
java.lang.Object |
evaluateToPrimitive(java.lang.String spec,
Mapping<java.lang.String,?> variables,
java.lang.Class<?> targetType)
Scans, parses and evaluates an expression.
|
java.lang.ClassLoader |
getClassLoader() |
java.lang.String[] |
getImports() |
Expression |
parse(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer)
Parses an expression from a
tokenProducer. |
Expression |
parse(java.lang.String spec)
Parses an expression.
|
<EX extends java.lang.Exception> |
parser(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer) |
ExpressionEvaluator |
setClassLoader(java.lang.ClassLoader classLoader) |
ExpressionEvaluator |
setImports(java.lang.String[] imports) |
static <T> T |
to(java.lang.Object subject,
java.lang.Class<T> targetType)
Converts the given
subject to the given targetType. |
static boolean |
toBoolean(java.lang.Object subject) |
static <T> T |
toPrimitive(java.lang.Object subject,
java.lang.Class<T> targetType)
Converts the given
subject to the given primitive target type. |
static java.lang.String |
toString(java.lang.Object subject) |
public static final java.util.Set<java.lang.Object> FALSES
false:
null
""
Boolean.FALSE
(byte) 0
(short) 0
(integer) 0
(long) 0
public ExpressionEvaluator(Predicate<? super java.lang.String> isValidVariableName)
isValidVariableName - Evaluates whether a string is a valid variable name; if not, then the parser will
throw a ParseExceptionpublic ExpressionEvaluator(java.util.Collection<java.lang.String> variableNames)
variableNames - Contains all valid variable namespublic ExpressionEvaluator(java.lang.String... variableNames)
variableNames - The names of the variables that can be referred to in an expressionpublic java.lang.String[] getImports()
public ExpressionEvaluator setImports(java.lang.String[] imports)
imports - Names of imprted packagespublic java.lang.ClassLoader getClassLoader()
ClassLoaderpublic ExpressionEvaluator setClassLoader(java.lang.ClassLoader classLoader)
classLoader - Used to load classes named in the expression; by default the ClassLoader which loaded
the ExpressionEvaluator class.public Expression parse(java.lang.String spec) throws ParseException
ParseExceptionpublic Expression parse(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer) throws ParseException
tokenProducer.ParseExceptionpublic <EX extends java.lang.Exception> Parser<Expression,EX> parser(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer)
tokenProducer - The source of tokens to be parsed, e.g. Scanner.stringScanner()Parser for expression parsing@Nullable public java.lang.Object evaluate(java.lang.String spec, Mapping<java.lang.String,?> variables) throws ParseException, EvaluationException
ParseException - spec refers to a variable which is not contained in variablesParseException - Any other parse errorEvaluationExceptionpublic static java.lang.String toString(@Nullable java.lang.Object subject)
subject.toString() or null iff subject == null@Nullable protected static java.lang.Object binaryNumericPromotion(@Nullable java.lang.Object value, @Nullable java.lang.Object other)
value to Integer, Long, Float, Double or String as appropriate for comparison with other.
Returns the original value iff the value is incomparable with other.
Examples:
value | other | Result |
|---|---|---|
| null | any | null |
| any | null | value |
| non-primitive | any | value |
| any | non-primitive | value |
| Byte | Short | Integer |
| Long | Short | Long |
| Byte | String | String |
public <T> T evaluateTo(java.lang.String spec,
Mapping<java.lang.String,?> variables,
java.lang.Class<T> targetType)
throws ParseException,
EvaluationException
TEvaluationException - The expression evaluates to nullEvaluationException - The value is not assignable to TParseExceptionpublic java.lang.Object evaluateToPrimitive(java.lang.String spec,
Mapping<java.lang.String,?> variables,
java.lang.Class<?> targetType)
throws ParseException,
EvaluationException
EvaluationException - The expression evaluates to null (and the targetType is not boolean.class)EvaluationException - The expression value cannot be converted to the given targetTypeParseExceptionpublic boolean evaluateToBoolean(@Nullable java.lang.String spec, Mapping<java.lang.String,?> variables) throws ParseException, EvaluationException
null iff the spec is null; otherwise the expresasion valueParseExceptionEvaluationException@Nullable public static <T> T to(@Nullable java.lang.Object subject, java.lang.Class<T> targetType) throws EvaluationException
subject to the given targetType.
Special processing applies for target types String and Boolean.EvaluationExceptiontoBoolean(Object),
toString(Object)public static <T> T toPrimitive(@Nullable java.lang.Object subject, java.lang.Class<T> targetType) throws EvaluationException
subject to the given primitive target type. Special processing applies for target
type boolean.class, see toBoolean(Object).EvaluationException - The subject is null (and the targetType is not boolean.class)EvaluationException - The subject cannot be converted to the given targetType