KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > SymbolTable


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.nfunk.jep;
10 import java.util.*;
11
12 /** A Hashtable which holds a list of all variables.
13  * Hevily changed from Jep-2.24 which was just a Hashtable which stored
14  * the values of each variable. Here the Hashtable contains
15  * elements of type {@link Variable Variable} which contain
16  * information about that variable.
17  * Rather than using {@link #get get} the methods
18  * {@link #getValue getValue(String)}, {@link #getVar getVar(String)}
19  * should be used to return the value or variable.
20  * The {@link #put put} method is deprecated and should be replace by one of
21  * <ul>
22  * <li>{@link #addVariable addVariable(String,Object)} adds a variable with a given name and value, returns null if variable already exists.
23  * <li>{@link #addConstant addConstant(String,Object)} adds a 'constant' variable whos value cannot be changed.
24  * <li>{@link #setVarValue setVarValue(String,Object)} sets the value of an existing variable. Returns false if variable does not exist.
25  * <li>{@link #makeVarIfNeeded(String,Object)} if necessary creates a variable and set its value.
26  * <li>{@link #makeVarIfNeeded(String)} if necessary creates a variable. Does not change the value.
27  * </ul>
28  * <p>
29  * Variables which do not have a value set are deamed to be invalid.
30  * When Variables need to be constructed then methods in the {@link VariableFactory}
31  * should be called, which allows different types of variables to be used.
32  *
33  * @author Rich Morris
34  * Created on 28-Feb-2004
35  */

36 public class SymbolTable extends Hashtable
37 {
38     protected VariableFactory vf;
39     /** SymbolTable should always be constructed an associated variable factory. */
40     public SymbolTable(VariableFactory varFac)
41     {
42         vf = varFac;
43     }
44     /** Private default constructors, SymbolTable should always be constructed with an explicit variable factory. */
45     private SymbolTable() {}
46     
47     /**
48      * @deprecated The getValue or getVar methods should be used instead.
49      */

50     public Object JavaDoc get(Object JavaDoc key) { return getValue(key); }
51     
52
53     /** Finds the value of the variable with the given name.
54      * Returns null if variable does not exist. */

55     public Object JavaDoc getValue(Object JavaDoc key)
56     {
57         Variable var = (Variable) super.get(key);
58         if(var==null) return null;
59         return var.getValue();
60     }
61     
62     /** Finds the variable with given name.
63     * Returns null if variable does not exist. */

64     public Variable getVar(String JavaDoc name)
65     {
66         return (Variable) super.get(name);
67     }
68
69     /**
70      * @deprecated The setVarValue or makeVar methods should be used instead.
71      */

72     public Object JavaDoc put(Object JavaDoc key,Object JavaDoc val)
73     {
74         return makeVarIfNeeded((String JavaDoc) key,val);
75     }
76
77     /**
78      * Sets the value of variable with the given name.
79      * Returns false if variable does not exist or if its value cannot be set.
80      */

81     public boolean setVarValue(String JavaDoc name,Object JavaDoc val)
82     {
83         Variable var = (Variable) super.get(name);
84         if(var != null)
85         {
86             return var.setValue(val);
87         }
88         else return false;
89     }
90
91     /** Creates a variable with given value.
92      * Returns null if variable already exists.
93      */

94     public Variable addVariable(String JavaDoc name,Object JavaDoc val)
95     {
96         Variable var = (Variable) super.get(name);
97         if(var != null) return null;
98         else
99         {
100             var = vf.createVariable(name,val);
101             super.put(name,var);
102         }
103         var.setValidValue(true);
104         return var;
105     }
106
107     /** Create a constant variable with the given name and value.
108      * Returns null if variable already exists.
109      */

110     public Variable addConstant(String JavaDoc name,Object JavaDoc val)
111     {
112         Variable var = addVariable(name,val);
113         if(var != null) var.setIsConstant(true);
114         return var;
115     }
116
117     /** Create a variable with the given name and value.
118      * It siliently does nothing if the value cannot be set.
119      * @return the Variable.
120      */

121     public Variable makeVarIfNeeded(String JavaDoc name,Object JavaDoc val)
122     {
123         Variable var = (Variable) super.get(name);
124         if(var != null)
125         {
126             var.setValue(val);
127             return var;
128         }
129         else
130         {
131             var = vf.createVariable(name,val);
132             super.put(name,var);
133             return var;
134         }
135     }
136
137     /** If necessary create a variable with the given name.
138      * If the variable exists its value will not be changed.
139      * @return the Variable.
140      */

141     public Variable makeVarIfNeeded(String JavaDoc name)
142     {
143         Variable var = (Variable) super.get(name);
144         if(var != null) return var;
145
146         var = vf.createVariable(name,null);
147         super.put(name,var);
148         return var;
149     }
150
151     /**
152      * Returns a list of variables, one per line.
153      */

154     public String JavaDoc toString()
155     {
156         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
157         for(Enumeration e = this.elements(); e.hasMoreElements(); )
158         {
159             Variable var = (Variable) e.nextElement();
160             sb.append(var.toString());
161             sb.append("\n");
162         }
163         return sb.toString();
164     }
165
166     /**
167      * Clears the values of all variables.
168      * Finer control is available through the
169      * {@link Variable#setValidValue Variable.setValidValue} method.
170      */

171     public void clearValues()
172     {
173         for(Enumeration e = this.elements(); e.hasMoreElements(); )
174         {
175             Variable var = (Variable) e.nextElement();
176             if(!var.isConstant()) var.setValidValue(false);
177         }
178     }
179     /**
180      * Returns the variable factory of this instance.
181      */

182     public VariableFactory getVariableFactory() {
183         return vf;
184     }
185
186 }
187
Popular Tags