public class SymbolTable extends Hashtable implements Serializable
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); } } }
Modifier and Type | Class and Description |
---|---|
class |
SymbolTable.StObservable |
Constructor and Description |
---|
SymbolTable(VariableFactory varFac)
SymbolTable should always be constructed an associated variable factory.
|
Modifier and Type | Method and Description |
---|---|
Variable |
addConstant(String name,
Object val)
Create a constant variable with the given name and value.
|
void |
addObserver(Observer arg)
Adds an observer which will be notified when a new variable is created.
|
void |
addObserverToExistingVariables(Observer arg)
Adds an observer to all variables currently in the SymbolTable.
|
Variable |
addVariable(String name,
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() |
void |
deleteObserver(Observer arg) |
void |
deleteObservers() |
Object |
get(Object key)
Deprecated.
The getValue or getVar methods should be used instead.
|
Object |
getValue(Object key)
Finds the value of the variable with the given name.
|
Variable |
getVar(String name)
Finds the variable with given name.
|
VariableFactory |
getVariableFactory()
Returns the variable factory of this instance.
|
boolean |
hasChanged() |
Variable |
makeVarIfNeeded(String name)
If necessary create a variable with the given name.
|
Variable |
makeVarIfNeeded(String name,
Object val)
Create a variable with the given name and value.
|
Object |
put(Object key,
Object val)
Deprecated.
The setVarValue or makeVar methods should be used instead.
|
void |
setVarValue(String name,
Object val)
Sets the value of variable with the given name.
|
String |
toString()
Returns a list of variables, one per line.
|
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keys, keySet, merge, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
public SymbolTable(VariableFactory varFac)
public Object get(Object key)
public Object getValue(Object key)
public Variable getVar(String name)
public Object put(Object key, Object val)
public void setVarValue(String name, Object val)
NullPointerException
- if the variable has not been previously created
with addVariable(String,Object)
first.public Variable addVariable(String name, Object val)
name
- name of variableval
- initial value of variablepublic Variable addConstant(String name, Object val)
public Variable makeVarIfNeeded(String name, Object val)
public Variable makeVarIfNeeded(String name)
public String toString()
public void clearValues()
Variable.setValidValue
method.public VariableFactory getVariableFactory()
public void addObserver(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(Observer arg)
public void deleteObservers()
public boolean hasChanged()
public void addObserverToExistingVariables(Observer arg)
arg
- the object to be notified when a variable changes.public void clearNonConstants()
Jas4pp 1.5 © Java Analysis Studio for Particle Physics