1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jxpath;
17
18 import java.util.HashMap;
19
20 /***
21 * A basic implementation of the Variables interface that uses a HashMap.
22 *
23 * @author Dmitri Plotnikov
24 * @version $Revision: 1.7 $ $Date: 2004/02/29 14:17:42 $
25 */
26 public class BasicVariables implements Variables {
27
28 /***
29 * Contains the values of declared variables
30 */
31 private HashMap vars = new HashMap();
32
33 /***
34 * Returns true if the variable has been defined, even if the
35 * value of the variable is null.
36 *
37 * @param varName is a variable name without the "$" sign
38 *
39 * @return true if the variable is declared
40 */
41 public boolean isDeclaredVariable(String varName) {
42 return vars.containsKey(varName);
43 }
44
45 /***
46 * Returns the value of the variable if it is defined,
47 * otherwise, throws IllegalArgumentException
48 *
49 * @param varName is a variable name without the "$" sign
50 *
51 * @return the value of the variable
52 */
53 public Object getVariable(String varName) {
54
55
56 if (vars.containsKey(varName)) {
57 return vars.get(varName);
58 }
59
60 throw new IllegalArgumentException(
61 "No such variable: '" + varName + "'");
62 }
63
64 /***
65 * Defines a new variable with the specified value or modifies
66 * the value of an existing variable.
67 *
68 * @param varName is a variable name without the "$" sign
69 * @param value is the new value for the variable, which can be null
70 */
71 public void declareVariable(String varName, Object value) {
72 vars.put(varName, value);
73 }
74
75 /***
76 * Removes an existing variable. May throw UnsupportedOperationException.
77 *
78 * @param varName is a variable name without the "$" sign
79 */
80 public void undeclareVariable(String varName) {
81 vars.remove(varName);
82 }
83
84 public String toString() {
85 return vars.toString();
86 }
87 }