|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.Dictionary java.util.Hashtable org.nfunk.jep.SymbolTable
public class SymbolTable
A Hashtable which holds a list of all variables.
Heavily changed from Jep-2.24 which was just a Hashtable which stored
the values of each variable. Here the Hashtable contains
elements of type Variable
which contain
information about that variable.
Rather than using get
the methods
getValue(String)
, getVar(String)
should be used to return the value or variable.
The put
method is deprecated and should be replace by one of
addVariable(String,Object)
adds a variable with a given name and value, returns null if variable already exists.
addConstant(String,Object)
adds a 'constant' variable whos value cannot be changed.
setVarValue(String,Object)
sets the value of an existing variable. Returns false if variable does not exist.
makeVarIfNeeded(String,Object)
if necessary creates a variable and set its value.
makeVarIfNeeded(String)
if necessary creates a variable. Does not change the value.
Variables which do not have a value set are deemed to be invalid.
When Variables need to be constructed then methods in the VariableFactory
should be called, which allows different types of variables to be used.
Both SymbolTable and Variable implement the Observer/Observable pattern. This allows objects to be informed whenever a new variable is created or when its value has been changed. The member class StObservable is used to implement Observer. An example of use is
public class MyObserver implements Observer { public void initialise() { SymbolTable st = j.getSymbolTable(); st.addObserver(this); st.addObserverToExistingVariables(this); } public void update(Observable arg0, Object arg1) { if(arg0 instanceof Variable) println("Var changed: "+arg0); else if(arg0 instanceof SymbolTable.StObservable) { println("New var: "+arg1); // This line is vital to ensure that // any new variable created will be observed. ((Variable) arg1).addObserver(this); } } }
Nested Class Summary | |
---|---|
class |
SymbolTable.StObservable
|
Field Summary | |
---|---|
protected SymbolTable.StObservable |
obeservable
|
Constructor Summary | |
---|---|
SymbolTable(VariableFactory varFac)
SymbolTable should always be constructed an associated variable factory. |
Method Summary | |
---|---|
Variable |
addConstant(java.lang.String name,
java.lang.Object val)
Create a constant variable with the given name and value. |
void |
addObserver(java.util.Observer arg)
Adds an observer which will be notified when a new variable is created. |
void |
addObserverToExistingVariables(java.util.Observer arg)
Adds an observer to all variables currently in the SymbolTable. |
Variable |
addVariable(java.lang.String name,
java.lang.Object val)
Creates a new variable with given value. |
void |
clearNonConstants()
Remove all non constant elements |
void |
clearValues()
Clears the values of all variables. |
int |
countObservers()
|
protected Variable |
createVariable(java.lang.String name)
|
protected Variable |
createVariable(java.lang.String name,
java.lang.Object val)
Returns a new variable fro the variable factory. |
void |
deleteObserver(java.util.Observer arg)
|
void |
deleteObservers()
|
java.lang.Object |
get(java.lang.Object key)
Deprecated. The getValue or getVar methods should be used instead. |
java.lang.Object |
getValue(java.lang.Object key)
Finds the value of the variable with the given name. |
Variable |
getVar(java.lang.String name)
Finds the variable with given name. |
VariableFactory |
getVariableFactory()
Returns the variable factory of this instance. |
boolean |
hasChanged()
|
Variable |
makeVarIfNeeded(java.lang.String name)
If necessary create a variable with the given name. |
Variable |
makeVarIfNeeded(java.lang.String name,
java.lang.Object val)
Create a variable with the given name and value. |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object val)
Deprecated. The setVarValue or makeVar methods should be used instead. |
void |
setVarValue(java.lang.String name,
java.lang.Object val)
Sets the value of variable with the given name. |
java.lang.String |
toString()
Returns a list of variables, one per line. |
Methods inherited from class java.util.Hashtable |
---|
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, hashCode, isEmpty, keys, keySet, putAll, rehash, remove, size, values |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected SymbolTable.StObservable obeservable
Constructor Detail |
---|
public SymbolTable(VariableFactory varFac)
Method Detail |
---|
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
get
in class java.util.Hashtable
public java.lang.Object getValue(java.lang.Object key)
public Variable getVar(java.lang.String name)
public java.lang.Object put(java.lang.Object key, java.lang.Object val)
put
in interface java.util.Map
put
in class java.util.Hashtable
public void setVarValue(java.lang.String name, java.lang.Object val)
java.lang.NullPointerException
- if the variable has not been previously created
with addVariable(String,Object)
first.protected Variable createVariable(java.lang.String name, java.lang.Object val)
name
- val
-
protected Variable createVariable(java.lang.String name)
public Variable addVariable(java.lang.String name, java.lang.Object val)
name
- name of variableval
- initial value of variable
public Variable addConstant(java.lang.String name, java.lang.Object val)
public Variable makeVarIfNeeded(java.lang.String name, java.lang.Object val)
public Variable makeVarIfNeeded(java.lang.String name)
public java.lang.String toString()
toString
in class java.util.Hashtable
public void clearValues()
Variable.setValidValue
method.
public VariableFactory getVariableFactory()
public void addObserver(java.util.Observer arg)
To find out if the values of variables are changed the Variable.addObserver method should be used.
arg
- the observerObservable.addObserver(Observer)
public int countObservers()
public void deleteObserver(java.util.Observer arg)
public void deleteObservers()
public boolean hasChanged()
public void addObserverToExistingVariables(java.util.Observer arg)
arg
- the object to be notified when a variable changes.public void clearNonConstants()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |