public class ExpressionEvaluator extends Object
evaluate(String, Mapping)
)parse(String)
) into an Expression
object for repeated
evaluation (see Expression.evaluate(Object...)
).
Modifier and Type | Field and Description |
---|---|
static Set<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(Collection<String> variableNames) |
ExpressionEvaluator(PredicateWhichThrows<? super String,? extends RuntimeException> isValidVariableName) |
ExpressionEvaluator(String... variableNames) |
Modifier and Type | Method and Description |
---|---|
protected static Object |
binaryNumericPromotion(Object value,
Object other)
|
Object |
evaluate(String spec,
Mapping<String,?> variables)
Scans, parses and evaluates an expression.
|
Object |
evaluate(String spec,
Object... variableNamesAndValues)
Scans, parses and evaluates an expression.
|
<T> T |
evaluateTo(String spec,
Class<T> targetType,
Mapping<String,?> variables)
Scans, parses, evaluates and returns an expression.
|
<T> T |
evaluateTo(String spec,
Class<T> targetType,
Object... variableNamesAndValues)
Scans, parses, evaluates and returns an expression.
|
boolean |
evaluateToBoolean(String spec,
Mapping<String,?> variables)
Scans, parses and evaluates an expression.
|
boolean |
evaluateToBoolean(String spec,
Object... variableNamesAndValues)
Scans, parses and evaluates an expression.
|
Object |
evaluateToPrimitive(String spec,
Class<?> targetType,
Mapping<String,?> variables)
Scans, parses and evaluates an expression.
|
Object |
evaluateToPrimitive(String spec,
Class<?> targetType,
Object... variableNamesAndValues)
Scans, parses and evaluates an expression.
|
ClassLoader |
getClassLoader() |
String[] |
getImports() |
Expression |
parse(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer)
Parses an expression from a tokenProducer.
|
Expression |
parse(String spec)
Parses an expression.
|
Expression |
parsePart(CharSequence spec,
int[] offset)
Parses an expression from the spec, but only as far as it is possible without a parse error.
|
Parser<Expression,RuntimeException> |
parser(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer) |
Parser<Expression,RuntimeException> |
parser(String spec) |
ExpressionEvaluator |
setClassLoader(ClassLoader classLoader) |
ExpressionEvaluator |
setImports(String[] imports) |
static <T> T |
to(Object subject,
Class<T> targetType)
Converts the given subject to the given targetType.
|
static boolean |
toBoolean(Object subject) |
static <T> T |
toPrimitive(Object subject,
Class<T> targetType)
Converts the given subject to the given primitive target type.
|
static String |
toString(Object subject) |
public static final Set<Object> FALSES
false
:
null
""
Boolean.FALSE
(byte) 0
(short) 0
(integer) 0
(long) 0
public ExpressionEvaluator(PredicateWhichThrows<? super String,? extends RuntimeException> isValidVariableName)
isValidVariableName
- Evaluates whether a string is a valid variable name; if not, then the parser will
throw a ParseException
public ExpressionEvaluator(Collection<String> variableNames)
variableNames
- Contains all valid variable namespublic ExpressionEvaluator(String... variableNames)
variableNames
- The names of the variables that can be referred to in an expressionpublic String[] getImports()
public ExpressionEvaluator setImports(String[] imports)
imports
- Names of imported packagespublic ClassLoader getClassLoader()
ClassLoader
public ExpressionEvaluator setClassLoader(ClassLoader classLoader)
classLoader
- Used to load classes named in the expression; by default the ClassLoader which loaded
the ExpressionEvaluator
class.public Expression parse(String spec) throws ParseException
spec
- The text to be parsedExpression.TRUE
iff the expression is constant and evaluates to true
;
Expression.FALSE
iff the expression is constant and evaluates to false
;
Expression.NULL
iff the expression is constant and evaluates to null
ParseException
The expression syntax
public Expression parsePart(CharSequence spec, int[] offset) throws ParseException
"a + b)"
is parsed up to and including "b".spec
- The text to be parsedoffset
- Returns the position of the first character within the space that could not be
parsedParseException
- The expression cannot be parsed, e.g. "a +)"
(the second operand of the "+"
operator is missing)The expression syntax
public Expression parse(ProducerWhichThrows<? extends AbstractScanner.Token<Scanner.TokenType>,? extends ScanException> tokenProducer) throws ParseException
ParseException
The expression syntax
public Parser<Expression,RuntimeException> parser(String spec)
spec
- The text to be parsedParser
for expression parsingThe expression syntax
public Parser<Expression,RuntimeException> 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 parsingThe expression syntax
@Nullable public Object evaluate(String spec, Object... variableNamesAndValues) throws ParseException, EvaluationException
ParseException
- spec refers to a variable which is not contained in variablesParseException
- Any other parse errorEvaluationException
The expression syntax
@Nullable public Object evaluate(String spec, Mapping<String,?> variables) throws ParseException, EvaluationException
ParseException
- spec refers to a variable which is not contained in variablesParseException
- Any other parse errorEvaluationException
The expression syntax
public static String toString(@Nullable Object subject)
subject.toString()
or null
iff subject == null
@Nullable protected static Object binaryNumericPromotion(@Nullable Object value, @Nullable Object other)
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 |
@Nullable public <T> T evaluateTo(String spec, Class<T> targetType, Object... variableNamesAndValues) throws ParseException, EvaluationException
T
, or null
EvaluationException
- The value is not assignable to T
ParseException
The expression syntax
@Nullable public <T> T evaluateTo(String spec, Class<T> targetType, Mapping<String,?> variables) throws ParseException, EvaluationException
T
, or null
EvaluationException
- The value is not assignable to T
ParseException
The expression syntax
public Object evaluateToPrimitive(String spec, Class<?> targetType, Object... variableNamesAndValues) 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 targetTypeParseException
The expression syntax
public Object evaluateToPrimitive(String spec, Class<?> targetType, Mapping<String,?> variables) 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 targetTypeParseException
The expression syntax
public boolean evaluateToBoolean(@Nullable String spec, Object... variableNamesAndValues) throws ParseException, EvaluationException
null
iff the spec is null
; otherwise the expresasion valueParseException
EvaluationException
The expression syntax
public boolean evaluateToBoolean(@Nullable String spec, Mapping<String,?> variables) throws ParseException, EvaluationException
null
iff the spec is null
; otherwise the expresasion valueParseException
EvaluationException
The expression syntax
@Nullable public static <T> T to(@Nullable Object subject, Class<T> targetType) throws EvaluationException
String
and Boolean
.EvaluationException
toBoolean(Object)
,
toString(Object)
public static <T> T toPrimitive(@Nullable Object subject, Class<T> targetType) throws EvaluationException
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 targetTypeCopyright © 2018 Arno Unkrig. All rights reserved.