de.unkrig.commons.text.expression
Class ExpressionUtil

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

public final class ExpressionUtil
extends java.lang.Object

Utility class for Expression.


Method Summary
static Expression constantExpression(java.lang.Object value)
           
static java.lang.String evaluateLeniently(Expression expression, Mapping<java.lang.String,?> variables)
           
static
<T> T
evaluateTo(Expression expression, Mapping<java.lang.String,?> variables, java.lang.Class<T> targetClass)
           
static Expression expand(java.lang.String s, Predicate<java.lang.String> isValidVariableName)
          Turns the given string into an expression.
static Expression expand(java.lang.String s, java.util.Set<java.lang.String> variableNames)
          Turns the given string into an expression.
static Expression fromPredicate(Predicate<java.lang.String> predicate, java.lang.String variableName)
           
static Expression logicalAnd(Expression operand1, Expression operand2)
           
static Expression logicalOr(Expression operand1, Expression operand2)
           
static
<T> Predicate<T>
toPredicate(Expression expression, java.lang.String parameterName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

constantExpression

public static Expression constantExpression(@Nullable
                                            java.lang.Object value)
Returns:
An Expression that evaluates to a constant value.

expand

public static Expression expand(java.lang.String s,
                                java.util.Set<java.lang.String> variableNames)
                         throws ParseException
Turns the given string into an expression. If the string contains '#' characters, then the text between two '#' characters is parsed as an expression.

Parameters:
s - The string to expand
variableNames - All contained variable names can be used in the expression
Returns:
The expanded string
Throws:
ParseException

expand

public static Expression expand(java.lang.String s,
                                Predicate<java.lang.String> isValidVariableName)
                         throws ParseException
Turns the given string into an expression. If the string contains '#' characters, then the text between two '#' characters is parsed as an expression.

Parameters:
s - The string to expand
isValidVariableName - Evalutaes to whether the subject is a valid variable name
Returns:
The expanded string
Throws:
ParseException

evaluateLeniently

public static java.lang.String evaluateLeniently(Expression expression,
                                                 Mapping<java.lang.String,?> variables)
Returns:
The result of the evaluation, or a verbose HTML comment reporting the error

evaluateTo

@Nullable
public static <T> T evaluateTo(Expression expression,
                                        Mapping<java.lang.String,?> variables,
                                        java.lang.Class<T> targetClass)
                    throws EvaluationException
Returns:
The evaluated value
Throws:
EvaluationException - The expression evaluates to null
EvaluationException - A problem occurred during evaluation, e.g. a class could not be loaded, or a type cast could not be performed

toPredicate

public static <T> Predicate<T> toPredicate(Expression expression,
                                           java.lang.String parameterName)
Parameters:
parameterName - The name under which the predicate subject is accessible for the expression
Returns:
A Predicate which evaluates to the value of the given expression

fromPredicate

public static Expression fromPredicate(Predicate<java.lang.String> predicate,
                                       java.lang.String variableName)
Returns:
An Expression which evaluates to the result of the given predicate, where the subject to the predicate is the value of the named variable of the expression

logicalAnd

public static Expression logicalAnd(Expression operand1,
                                    Expression operand2)
Returns:
An Expression which evaluates the operand1, and, if that is true the operand2

logicalOr

public static Expression logicalOr(Expression operand1,
                                   Expression operand2)
Returns:
An Expression which evaluates the operand1, and, if that is false the operand2