org.appng.el

## Class ExpressionEvaluator

- java.lang.Object
- - org.appng.el.ExpressionEvaluator

-     public final class ExpressionEvaluator
      extends Object

  Used for evaluating expressions that conform the Expression Language 2.2 syntax.
  A valid expression must start with ${ and end with }. A list of the allowed operators an be found here.

  Example:

       Map<String, Object> parameters = new HashMap<String, Object>();
       parameters.put("intValue", 5);
       parameters.put("stringValue", "foobar");
       ExpressionEvaluator ee = new ExpressionEvaluator(parameters);
       org.junit.Assert.assertEquals("foobar", ee.evaluate("${stringValue}", String.class));
       org.junit.Assert.assertEquals(Integer.valueOf(5), ee.evaluate("${intValue}", Integer.class));
       org.junit.Assert.assertTrue(ee.evaluate("${intValue eq (3+2)}"));
       org.junit.Assert.assertTrue(ee.evaluate("${(1 eq (2/2)) && (2<3)}"));
       org.junit.Assert.assertTrue(ee.evaluate("${stringValue.length() == 6}"));

  Author:
    Matthias Müller

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ExpressionEvaluator(Map<String,?> variables)` Creates a new `ExpressionEvaluator` using the given variables.
    `ExpressionEvaluator(javax.el.VariableMapper variableMapper)` Creates a new `ExpressionEvaluator` using the given `VariableMapper`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addFunction(String name, Method method)` Adds a function to this `ExpressionEvaluator`.
    `void` | `addFunction(String prefix, String name, Method method)` Adds a prefixed function to this `ExpressionEvaluator`.
    `boolean` | `evaluate(String expression)` Evaluates the given expression to a `boolean`.
    `<T> T` | `evaluate(String expression, Class<T> targetType)` Evaluates the given expression to an object of the given targetType
    `Boolean` | `getBoolean(String expression)` Evaluates the given expression to a `Boolean`, if possible.
    `String` | `getString(String expression)` Evaluates the given expression to a `String`, if possible.
    `boolean` | `isExpression(String value)` Checks whether the given `String` is a valid expression (starts with ${ and ends with }).
    `void` | `setVariable(String name, Object value)` Sets the variable with the given name to the given value.
    `void` | `setVariables(Map<String,?> variables)` Calls `setVariable(String, Object)` for each entry of the given map.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ExpressionEvaluator

          public ExpressionEvaluator(Map<String,?> variables)

      Creates a new `ExpressionEvaluator` using the given variables.
      Parameters:
        `variables` - a `Map` of variables to use

    - #### ExpressionEvaluator

          public ExpressionEvaluator(javax.el.VariableMapper variableMapper)

      Creates a new `ExpressionEvaluator` using the given `VariableMapper`.
      Parameters:
        `variableMapper` - the `VariableMapper` to use

  - ### Method Detail

    - #### evaluate

          public final <T> T evaluate(String expression,
                                      Class<T> targetType)

      Evaluates the given expression to an object of the given targetType
      Parameters:
        `expression` - the expression to parse
        `targetType` - the expected target type
      Returns:
        an object of the given type
      Throws:
        `NullPointerException` - if targetType is `null`
        `javax.el.ELException` - if the expression has syntactical errors

    - #### evaluate

          public final boolean evaluate(String expression)

      Evaluates the given expression to a `boolean`.
      Parameters:
        `expression` - the expression to evaluate
      Returns:
        evaluation result

    - #### addFunction

          public final void addFunction(String prefix,
                                        String name,
                                        Method method)

      Adds a prefixed function to this `ExpressionEvaluator`. The given `Method` must be `static`.
      Example:

           ExpressionEvaluator ee = ...;
           ee.addFunction("math","max", Math.class.getMethod("max", int.class, int.class));
           org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${math:max(47,11)}", Integer.class));

      Parameters:
        `prefix` - the prefix for the method
        `name` - the name of the method
        `method` - the `Method` to add as a function

    - #### addFunction

          public final void addFunction(String name,
                                        Method method)

      Adds a function to this `ExpressionEvaluator`. The given `Method` must be `static`.
      `static`.
      Example:

           ExpressionEvaluator ee = ...;
           ee.addFunction("max", Math.class.getMethod("max", int.class, int.class));
           org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${max(47,11)}", Integer.class));

      Parameters:
        `name` - the name of the method
        `method` - the `Method` to add as a function

    - #### setVariable

          public final void setVariable(String name,
                                        Object value)

      Sets the variable with the given name to the given value.
      Parameters:
        `name` - the name of the variable
        `value` - the value of the variable (must not be `null`)

    - #### setVariables

          public final void setVariables(Map<String,?> variables)

      Calls `setVariable(String, Object)` for each entry of the given map.
      Parameters:
        `variables` - a `Map` of variables

    - #### isExpression

          public boolean isExpression(String value)

      Checks whether the given `String` is a valid expression (starts with ${ and ends with }).
      Parameters:
        `value` - the String to check
      Returns:
        `true` if the given value is a valid expression, `false` otherwise

    - #### getString

          public String getString(String expression)

      Evaluates the given expression to a `String`, if possible.
      Parameters:
        `expression` - the expression to evaluate
      Returns:
        - the `String` evaluated from the expression, in cases it's a valid expression
        - the given expression itself, otherwise
      See Also:
        `isExpression(String)`

    - #### getBoolean

          public Boolean getBoolean(String expression)

      Evaluates the given expression to a `Boolean`, if possible.
      Parameters:
        `expression` - the expression to evaluate
      Returns:
        - the `Boolean` evaluated from the expression, in cases it's a valid expression
        - `Boolean.valueOf(expression)` if the expression is not `null` but is not a valid expression
        - `null` if the expression is `null`
      See Also:
        `isExpression(String)`

Copyright © 2011–2023 aiticon GmbH. All rights reserved.