View Javadoc
1   package com.randomnoun.common.jexl.eval;
2   
3   /* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
4    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
5    */
6   
7   import java.util.*;
8   
9   import org.apache.log4j.Logger;
10  
11  
12  /**
13   * This class encapsulates a function, which is intended to be placed in an
14   * EvalContext (using EvalContext.setFunctions()). Any
15   * expression evaluated in that context will then be able to invoke developer-defined
16   * functions.
17   *
18   * <pre>
19      Map&lt;String, EvalFunction&gt; functions = new HashMap();
20      functions.put("length", new LengthFunction());
21      functions.put("like", new LikeFunction());
22      EvalContext evalContext = new EvalContext();
23      evalContext.setFunctions(functions);
24   * </pre>
25   *
26   * Some functions can also generate SQL, see
27   * {@link com.randomnoun.common.jexl.sql.SqlFunction}.
28   *
29   * 
30   * @author knoxg
31   */
32  public interface EvalFunction
33  {
34  	/** Logger for this class */
35  	public static Logger logger = Logger.getLogger(EvalFunction.class);
36  
37      /**
38       * This is the main entry point for classes that implement this interface.
39       * The evaluate method performs the evaluation of the function that this class
40       * implements (and is supplied the functionName, so that the same class may
41       * evaluate multiple functions). 
42       *
43       * @param functionName The name of the function to be evaluated (can be disregarded
44       *   if this class only performs a single function)
45       * @param context The context in which the function is being executed (could hold
46       *   things like local variable values, current line number, etc...)
47       * @param arguments The arguments passed to this function
48       *
49       * @return The result of the evaluation
50       *
51       * @throws EvalException An exception occurred during evaluation
52       */
53      public Object evaluate(String functionName, EvalContext context, List<Object> arguments)
54          throws EvalException;
55  }