de.unkrig.commons.text.expression
Class ExpressionEvaluator

java.lang.Object
  extended by de.unkrig.commons.text.expression.ExpressionEvaluator

public class ExpressionEvaluator
extends java.lang.Object

Supports two operation modes:


Field Summary
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 Summary
ExpressionEvaluator(java.util.Collection<java.lang.String> variableNames)
           
ExpressionEvaluator(Predicate<java.lang.String> isValidVariableName)
           
ExpressionEvaluator(java.lang.String... variableNames)
           
 
Method Summary
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<AbstractScanner.Token<Scanner.TokenType>,ScanException> tokenProducer)
          Parses an expression from a tokenProducer.
 Expression parse(java.lang.String spec)
          Parses an expression.
<E extends java.lang.Exception>
Parser<Expression,E>
parser(ProducerWhichThrows<AbstractScanner.Token<Scanner.TokenType>,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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FALSES

public static final java.util.Set<java.lang.Object> FALSES
All values the are implicitly regarded als false:

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator(Predicate<java.lang.String> isValidVariableName)
Parameters:
isValidVariableName - Evaluates whether a string is a valid variable name; if not, then the parser will throw a ParseException

ExpressionEvaluator

public ExpressionEvaluator(java.util.Collection<java.lang.String> variableNames)
Parameters:
variableNames - Contains all valid variable names

ExpressionEvaluator

public ExpressionEvaluator(java.lang.String... variableNames)
Parameters:
variableNames - The names of the variables that can be referred to in an expression
Method Detail

getImports

public java.lang.String[] getImports()
Returns:
The currently configured imports

setImports

public ExpressionEvaluator setImports(java.lang.String[] imports)
Parameters:
imports - Names of imprted packages

getClassLoader

public java.lang.ClassLoader getClassLoader()
Returns:
The currently configured ClassLoader

setClassLoader

public ExpressionEvaluator setClassLoader(java.lang.ClassLoader classLoader)
Parameters:
classLoader - Used to load classes named in the expression; by default the ClassLoader which loaded the ExpressionEvaluator class.

parse

public Expression parse(java.lang.String spec)
                 throws ParseException
Parses an expression.

Throws:
ParseException

parse

public Expression parse(ProducerWhichThrows<AbstractScanner.Token<Scanner.TokenType>,ScanException> tokenProducer)
                 throws ParseException
Parses an expression from a tokenProducer.

Throws:
ParseException

parser

public <E extends java.lang.Exception> Parser<Expression,E> parser(ProducerWhichThrows<AbstractScanner.Token<Scanner.TokenType>,ScanException> tokenProducer)
Parameters:
tokenProducer - The source of tokens to be parsed, e.g. Scanner.stringScanner()
Returns:
A Parser for expression parsing

evaluate

@Nullable
public java.lang.Object evaluate(java.lang.String spec,
                                          Mapping<java.lang.String,?> variables)
                          throws ParseException,
                                 EvaluationException
Scans, parses and evaluates an expression.

Throws:
ParseException - spec refers to a variable which is not contained in variables
ParseException - Any other parse error
EvaluationException

toString

public static java.lang.String toString(@Nullable
                                        java.lang.Object subject)
Returns:
subject.toString() or null iff subject == null

binaryNumericPromotion

@Nullable
protected static java.lang.Object binaryNumericPromotion(@Nullable
                                                                  java.lang.Object value,
                                                                  @Nullable
                                                                  java.lang.Object other)
Converts and returns the 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:

valueotherResult
nullanynull
anynullvalue
non-primitiveanyvalue
anynon-primitivevalue
ByteShortInteger
LongShortLong
ByteStringString


evaluateTo

public <T> T evaluateTo(java.lang.String spec,
                        Mapping<java.lang.String,?> variables,
                        java.lang.Class<T> targetType)
             throws ParseException,
                    EvaluationException
Scans, parses, evaluates and returns an expression.

Returns:
An object of type T
Throws:
EvaluationException - The expression evaluates to null
EvaluationException - The value is not assignable to T
ParseException

evaluateToPrimitive

public java.lang.Object evaluateToPrimitive(java.lang.String spec,
                                            Mapping<java.lang.String,?> variables,
                                            java.lang.Class<?> targetType)
                                     throws ParseException,
                                            EvaluationException
Scans, parses and evaluates an expression.

Returns:
The (wrapped) expression value
Throws:
EvaluationException - The expression evaluates to null (and the targetType is not boolean.class)
EvaluationException - The expression value cannot be converted to the given targetType
ParseException

evaluateToBoolean

public boolean evaluateToBoolean(@Nullable
                                 java.lang.String spec,
                                 Mapping<java.lang.String,?> variables)
                          throws ParseException,
                                 EvaluationException
Scans, parses and evaluates an expression.

Returns:
null iff the spec is null; otherwise the expresasion value
Throws:
ParseException
EvaluationException

to

@Nullable
public static <T> T to(@Nullable
                                java.lang.Object subject,
                                java.lang.Class<T> targetType)
            throws EvaluationException
Converts the given subject to the given targetType. Special processing applies for target types String and Boolean.

Throws:
EvaluationException
See Also:
toBoolean(Object), toString(Object)

toPrimitive

public static <T> T toPrimitive(@Nullable
                                java.lang.Object subject,
                                java.lang.Class<T> targetType)
                     throws EvaluationException
Converts the given subject to the given primitive target type. Special processing applies for target type boolean.class, see toBoolean(Object).

Throws:
EvaluationException - The subject is null (and the targetType is not boolean.class)
EvaluationException - The subject cannot be converted to the given targetType

toBoolean

public static boolean toBoolean(@Nullable
                                java.lang.Object subject)
Returns:
Whether the subject equals on of the FALSES