Package com.singularsys.jep

# Class VariableTable

java.lang.Object
com.singularsys.jep.VariableTable

All Implemented Interfaces:
  `JepComponent`, `Serializable`

Direct Known Subclasses:
  `ConcurrentVariableTable`, `DecoratingVariableTable`, `RealVariableTable`, `StandardVariableTable`

public class VariableTable extends Object implements JepComponent
A table of variables.

See Also:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final HashMap<String,Variable>`
  `table`
  `protected VariableFactory`
  `vf`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `VariableTable()`
  `VariableTable(VariableTable tbl)`
  Copy constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Variable`
  `addConstant(String name, Object value)`
  Adds a new variable which is marked as a constant.
  `Variable`
  `addVariable(String name)`
  Adds a new variable with no value specified.
  `Variable`
  `addVariable(String name, Object value)`
  Adds a new variable with specified value.
  `void`
  `clear()`
  Clears all variables from the table.
  `void`
  `clearValues()`
  Clears the values of all non-constant variables.
  `boolean`
  `containsKey(String key)`
  Returns true if the table contains a variable with the given name.
  `boolean`
  `containsVariable(Variable vbl)`
  Returns true if the table contains the given variable.
  `void`
  `copyConstantsFrom(VariableTable vt)`
  Copies all the constants from a variable table into this table.
  `void`
  `copyVariablesFrom(VariableTable vt)`
  Copies all the variables from a variable table into this table Copies all properties of the variable, including isConstant, validValue flags and all hook keys.
  `JepComponent`
  `getLightWeightInstance()`
  Returns a new empty VariableTable.
  `Variable`
  `getVariable(String name)`
  Gets an existing variable, returns null if variable is not found.
  `VariableFactory`
  `getVariableFactory()`
  `Collection<Variable>`
  `getVariables()`
  Returns a list of all the variables in the table.
  `void`
  `init(Jep j)`
  Initialize the component.
  `boolean`
  `isEmpty()`
  Returns true if the table is empty.
  `Set<String>`
  `keySet()`
  Returns the names of all the variables.
  `Variable`
  `remove(Variable var)`
  Remove a variable from the table.
  `Variable`
  `remove(String varname)`
  Remove a variable with a given name from the table.
  `void`
  `removeNonConstants()`
  Remove all variables from the table which are not constants.
  `void`
  `setVariableFactory(VariableFactory vf)`
  `int`
  `size()`
  The number of variables in the table.
  `String`
  `toString()`
  `Collection<Variable>`
  `values()`
  Deprecated.
  use `getVariables()`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### table

    protected final HashMap<String,Variable> table

  - ### vf

    protected transient VariableFactory vf

- ## Constructor Details

  - ### VariableTable

    public VariableTable()

  - ### VariableTable

    public VariableTable(VariableTable tbl)
    Copy constructor. For each variable from the source table create a new variable.
    Parameters:
      `tbl` - the table to copy

- ## Method Details

  - ### init

    public void init(Jep j)
    Description copied from interface: `JepComponent`
    Initialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.
    Specified by:
      `init` in interface `JepComponent`
    Parameters:
      `j` - the current Jep instance

  - ### setVariableFactory

    public void setVariableFactory(VariableFactory vf)

  - ### getVariableFactory

    public VariableFactory getVariableFactory()

  - ### addVariable

    public Variable addVariable(String name)
    Adds a new variable with no value specified. If the variable already exists in the table, that variable instance is returned. Otherwise, a new `Variable` instance is created with the variable factory.
    Parameters:
      `name` - the variable's name
    Returns:
      the variable

  - ### addVariable

    public Variable addVariable(String name, Object value) throws JepException
    Adds a new variable with specified value. If the variable already exists, its value is updated with the new value. If the variable exists and is a constant, the value is not updated and no error is reported. If the variable does not exist, it is created using the variable factory of the jep instance specified with the init(Jep j) method.
    Parameters:
      `name` - the name of the variable
      `value` - the value for the variable to be set to
    Returns:
      the variable added or updated
    Throws:
      `JepException` - if an attempt is made to set the value of a constant variable

  - ### addConstant

    public Variable addConstant(String name, Object value) throws JepException
    Adds a new variable which is marked as a constant. Its value will also be marked as valid, in particular a null value is valid for a constant.
    Parameters:
      `name` - variable name
      `value` - variable value
    Returns:
      the corresponding Variable object
    Throws:
      `JepException` - if an attempt is made to set the value of an existing constant variable

  - ### getVariable

    public Variable getVariable(String name)
    Gets an existing variable, returns null if variable is not found.
    Parameters:
      `name` - variable name
    Returns:
      the variable

  - ### clear

    public void clear()
    Clears all variables from the table.

  - ### isEmpty

    public boolean isEmpty()
    Returns true if the table is empty.
    Returns:
      true if the table is empty

  - ### keySet

    public Set<String> keySet()
    Returns the names of all the variables.
    Returns:
      a collection Strings with the variable names.

  - ### getVariables

    public Collection<Variable> getVariables()
    Returns a list of all the variables in the table. Just returns a view. Does not do a deep copy so changes to this collection will affect the underlying table.
    Returns:
      a collection of all the variables.

  - ### remove

    public Variable remove(String varname)
    Remove a variable with a given name from the table.
    Parameters:
      `varname` - name of the variable to remove
    Returns:
      the removed variable or null if the variable does not exist in the table.

  - ### remove

    public Variable remove(Variable var)
    Remove a variable from the table.
    Parameters:
      `var` - the variable to remove
    Returns:
      the removed variable or null if the variable does not exist in the table.

  - ### values

    @Deprecated public Collection<Variable> values()
    Deprecated.
    use `getVariables()`.
    A collection of all the variables. Replace with `getVariables()`
    Returns:
      a collection of all the variables.

  - ### size

    public int size()
    The number of variables in the table.
    Returns:
      the number of variables

  - ### containsKey

    public boolean containsKey(String key)
    Returns true if the table contains a variable with the given name.
    Parameters:
      `key` - variable name
    Returns:
      true if the table contains the variable

  - ### containsVariable

    public boolean containsVariable(Variable vbl)
    Returns true if the table contains the given variable.
    Parameters:
      `vbl` - the variable
    Returns:
      true if the variable is in the table

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### getLightWeightInstance

    public JepComponent getLightWeightInstance()
    Returns a new empty VariableTable.
    Specified by:
      `getLightWeightInstance` in interface `JepComponent`
    Returns:
      a new VariableTable
    See Also:
      - `copyConstantsFrom(VariableTable)`
      - `copyVariablesFrom(VariableTable)`

  - ### copyConstantsFrom

    public void copyConstantsFrom(VariableTable vt)
    Copies all the constants from a variable table into this table. Copies all properties of the variable, including isConstant, validValue flags and all hook keys. If there already is a variable with the same name all properties are copied and pre-existing hooks deleted.
    Parameters:
      `vt` - source variable table

  - ### copyVariablesFrom

    public void copyVariablesFrom(VariableTable vt)
    Copies all the variables from a variable table into this table Copies all properties of the variable, including isConstant, validValue flags and all hook keys. If there already is a variable with the same name all properties are copied and pre-existing hooks deleted.
    Parameters:
      `vt` - source variable table

  - ### clearValues

    public void clearValues()
    Clears the values of all non-constant variables. This will set the `Variable.setValidValue(boolean)` to false.
    Since:
      3.4.0

  - ### removeNonConstants

    public void removeNonConstants()
    Remove all variables from the table which are not constants.
    Since:
      3.5