org.nfunk.jep
Class SymbolTable

java.lang.Object
  extended by java.util.Dictionary
      extended by java.util.Hashtable
          extended by org.nfunk.jep.SymbolTable
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map
Direct Known Subclasses:
XSymbolTable

public class SymbolTable
extends java.util.Hashtable

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

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);
                }
        }
 }
 

Author:
Rich Morris Created on 28-Feb-2004
See Also:
Serialized Form

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

obeservable

protected SymbolTable.StObservable obeservable
Constructor Detail

SymbolTable

public SymbolTable(VariableFactory varFac)
SymbolTable should always be constructed an associated variable factory.

Method Detail

get

public java.lang.Object get(java.lang.Object key)
Deprecated. The getValue or getVar methods should be used instead.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.Hashtable

getValue

public java.lang.Object getValue(java.lang.Object key)
Finds the value of the variable with the given name. Returns null if variable does not exist.


getVar

public Variable getVar(java.lang.String name)
Finds the variable with given name. Returns null if variable does not exist.


put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object val)
Deprecated. The setVarValue or makeVar methods should be used instead.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.Hashtable

setVarValue

public void setVarValue(java.lang.String name,
                        java.lang.Object val)
Sets the value of variable with the given name.

Throws:
java.lang.NullPointerException - if the variable has not been previously created with addVariable(String,Object) first.

createVariable

protected Variable createVariable(java.lang.String name,
                                  java.lang.Object val)
Returns a new variable fro the variable factory. Notifies observers when a new variable is created. If a subclass need to create a new variable it should call this method.

Parameters:
name -
val -
Returns:
an new Variable object.

createVariable

protected Variable createVariable(java.lang.String name)

addVariable

public Variable addVariable(java.lang.String name,
                            java.lang.Object val)
Creates a new variable with given value.

Parameters:
name - name of variable
val - initial value of variable
Returns:
a reference to the created variable.

addConstant

public Variable addConstant(java.lang.String name,
                            java.lang.Object val)
Create a constant variable with the given name and value. Returns null if variable already exists.


makeVarIfNeeded

public Variable makeVarIfNeeded(java.lang.String name,
                                java.lang.Object val)
Create a variable with the given name and value. It silently does nothing if the value cannot be set.

Returns:
the Variable.

makeVarIfNeeded

public Variable makeVarIfNeeded(java.lang.String name)
If necessary create a variable with the given name. If the variable exists its value will not be changed.

Returns:
the Variable.

toString

public java.lang.String toString()
Returns a list of variables, one per line.

Overrides:
toString in class java.util.Hashtable

clearValues

public void clearValues()
Clears the values of all variables. Finer control is available through the Variable.setValidValue method.


getVariableFactory

public VariableFactory getVariableFactory()
Returns the variable factory of this instance.


addObserver

public void addObserver(java.util.Observer arg)
Adds an observer which will be notified when a new variable is created. The observer's update method will be called whenever a new variable is created, the second argument of this will be a reference to the new variable.

To find out if the values of variables are changed the Variable.addObserver method should be used.

Parameters:
arg - the observer
See Also:
Observable.addObserver(Observer)

countObservers

public int countObservers()

deleteObserver

public void deleteObserver(java.util.Observer arg)

deleteObservers

public void deleteObservers()

hasChanged

public boolean hasChanged()

addObserverToExistingVariables

public void addObserverToExistingVariables(java.util.Observer arg)
Adds an observer to all variables currently in the SymbolTable.

Parameters:
arg - the object to be notified when a variable changes.

clearNonConstants

public void clearNonConstants()
Remove all non constant elements



http://www.singularsys.com/jep Copyright © 2007 Singular Systems