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   import java.util.HashMap;
7   import java.util.Map;
8   
9   /**
10   * Provides variables and functions for an expression to be evaluated with
11   *
12   * 
13   * @author knoxg
14   */
15  public class EvalContext
16  {
17      /** Contains a map of variable names to variable values */
18      private Map<String, Object> variables = new HashMap<String, Object>();
19  
20      /** Contains a map of function names to EvalFunction values */
21      private Map<String, EvalFunction> functions = new HashMap<String, EvalFunction>();
22  
23      /** Sets all variables accessable to an expression. */
24      public void setVariables(Map<String, Object> variables)
25      {
26          this.variables = variables;
27      }
28  
29      /** Set a specific variable binding */
30      public void setVariable(String name, Object value)
31      {
32          variables.put(name, value);
33      }
34  
35      /** Removes a specific variable binding */
36      public void unsetVariable(String name)
37      {
38          variables.remove(name);
39      }
40  
41      /** Returns the value of a variable (used in the Evaluator class). */
42      public Object getVariable(String varName)
43      {
44          return variables.get(varName);
45      }
46  
47      /** Returns true if the variable exists. */
48      public boolean hasVariable(String varName)
49      {
50          return variables.containsKey(varName);
51      }
52  
53      /** Sets all variables accessible to an expression. */
54      public void setFunctions(Map<String, EvalFunction> functions)
55      {
56          this.functions = functions;
57      }
58  
59      /** Set a specific function */
60      public void setFunction(String name, EvalFunction function)
61      {
62          functions.put(name, function);
63      }
64  
65      /** Returns the value of a variable (used in the Evaluator class). */
66      public Object getFunction(String function)
67      {
68          return functions.get(function);
69      }
70  
71      /** Returns a component of a compound variable (which must currently be a Map object).
72       * The component name is the key of the map. 
73       * e.g. if a Map variable called 'x' contains a key 'y' with the value
74       * 'z', then the following code could be used to retrieve this value:
75       *
76       * <pre>
77       *   x = evalContext.getVariable("x")
78       *   value = evalContext.getVariableComponent(x, "x", "y")  // evaluates to 'z'
79       * </pre>
80       *
81       * <p>Note that this is only useful in the Evaluator to retrieve compound variables,
82       * outside the evaluator you should be just be evaluating the value "x.y" directly.
83       *
84       * @param varObj the compound variable value that is being evaluated (as returned
85       *   by the getVariable() method)
86       * @param baseName the name of the variable
87       * @param componentName the name of the component in varObj to return.
88       *
89       * */
90      public Object getVariableComponent(Object varObj, String baseName,
91          String componentName)
92          throws EvalException
93      {
94          if (varObj == null) {
95              throw new EvalException("Can not retrieve component '" + componentName +
96                  "' from '" + baseName + "'; base is null");
97          }
98  
99          if (varObj instanceof Map) {
100             return ((Map<?, ?>) varObj).get(componentName);
101         } else {
102             throw new EvalException("Can not retrieve component '" + componentName +
103                 "' from '" + baseName + "'; base is of type '" +
104                 varObj.getClass().getName() + "'");
105         }
106     }
107 
108     /**
109      * DOCUMENT ME!
110      *
111      * @param varObj DOCUMENT ME!
112      * @param baseName DOCUMENT ME!
113      * @param componentName DOCUMENT ME!
114      *
115      * @return DOCUMENT ME!
116      */
117     public boolean hasVariableComponent(Object varObj, String baseName,
118         String componentName)
119     {
120         if (varObj == null) {
121             throw new EvalException("Can not retrieve component '" + componentName +
122                 "' from '" + baseName + "'; base is null");
123         }
124 
125         if (varObj instanceof Map) {
126             return ((Map<?, ?>) varObj).containsKey(componentName);
127         } else {
128             throw new EvalException("Can not retrieve component '" + componentName +
129                 "' from '" + baseName + "'; base is of type '" +
130                 varObj.getClass().getName() + "'");
131         }
132     }
133 
134 }