Package com.singularsys.jep

# Class Variable

java.lang.Object
com.singularsys.jep.Variable

All Implemented Interfaces:
  `Serializable`

Direct Known Subclasses:
  `BoundVariable`, `PublishingVariable`

public class Variable extends Object implements Serializable
Information about a variable. Each variable has a name, and a value. There is a flag to indicate whether it is a constant or not (constants cannot have their value changed). There is also a flag to indicate whether the value of the variable is valid. If the variable is initialized without a value then its value is said to be invalid.

Version:
  2.3.0 beta 2 Now extends Observable so observers can track if the value has been changed., 4.1.0 Observable removed as deprecated since Java 9
Author:
  Richard Morris Created on 18-Nov-2003
See Also:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Node.HookKey[]`
  `keys`
  `protected Object[]`
  `keyVals`
  `protected final String`
  `name`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Variable(Variable var)`
  Copy constructor.
  `protected`
  `Variable(String name)`
  Constructors are protected.
  `protected`
  `Variable(String name, Object value)`
  Constructors are protected.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `copyPropertiesFrom(Variable oldvar)`
  Copies additional properties from and old variable.
  `Object`
  `getHook(Node.HookKey key)`
  Get the value associated with a key.
  `String`
  `getName()`
  Returns the variable name.
  `Object`
  `getValue()`
  Returns the variable's value.
  `boolean`
  `hasValidValue()`
  Is the value of this variable valid?
  `Collection<Node.HookKey>`
  `hookKeys()`
  Return all the hooks set for this variable.
  `boolean`
  `isConstant()`
  Returns true if the variable is a constant.
  `Object`
  `removeHook(Node.HookKey key)`
  Remove a hook from a variable
  `Object`
  `setHook(Node.HookKey key, Object value)`
  Sets the value associated with a key.
  `void`
  `setIsConstant(boolean b)`
  Setting this property to true ensures that the value will not be changed.
  `void`
  `setValidValue(boolean val)`
  Sets whether the value of variable is valid.
  `boolean`
  `setValue(Object val)`
  Sets the value of the variable.
  `protected boolean`
  `setValueRaw(Object object)`
  In general subclasses should override this method rather than setValue.
  `String`
  `toString()`
  Returns a string with the variable name followed by its value.

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

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

- ## Field Details

  - ### name

    protected final String name

  - ### keys

    protected Node.HookKey[] keys

  - ### keyVals

    protected Object[] keyVals

- ## Constructor Details

  - ### Variable

    protected Variable(String name)
    Constructors are protected. Variables should only be created through the associated `VariableFactory` which are in turned called by `VariableTable`.

  - ### Variable

    protected Variable(String name, Object value)
    Constructors are protected. Variables should only be created through the associated `VariableFactory` which are in turned called by `VariableTable`.

  - ### Variable

    protected Variable(Variable var)
    Copy constructor. Creates a new variable with the same name, value, and isConstant flag as an existing variable. Does not copy hooks.
    Parameters:
      `var` - variable to copy from

- ## Method Details

  - ### getName

    public String getName()
    Returns the variable name.
    Returns:
      the variable name.

  - ### isConstant

    public boolean isConstant()
    Returns true if the variable is a constant.

  - ### setIsConstant

    public void setIsConstant(boolean b)
    Setting this property to true ensures that the value will not be changed.
    Parameters:
      `b` - true if the variable is a constant.

  - ### getValue

    public Object getValue()
    Returns the variable's value.
    Returns:
      the Variable value as Object.

  - ### hasValidValue

    public boolean hasValidValue()
    Is the value of this variable valid?
    Returns:
      whether the value is valid

  - ### setValidValue

    public void setValidValue(boolean val)
    Sets whether the value of variable is valid. Constant values are not affected, that is always valid.

  - ### setValue

    public boolean setValue(Object val)
    Sets the value of the variable. Constant values cannot be changed.

    Calls `setValueRaw(Object)` subclasses should override that method.

    This method sets the validValue flag to true.

    Parameters:
      `val` - the new value
    Returns:
      false if tried to change a constant value.
    Since:
      2.3.0 beta 2 added Observable, 4.1 removed Observable

  - ### setValueRaw

    protected boolean setValueRaw(Object object)
    In general subclasses should override this method rather than setValue.

    This method sets the validValue flag to true.

    Parameters:
      `object` -
    Returns:
      true if tried to change a constant value.
    Since:
      2.3.0 beta 2

  - ### getHook

    public Object getHook(Node.HookKey key)
    Get the value associated with a key.
    Parameters:
      `key` - the key to look up
    Returns:
      the value or null if no key exists
    Since:
      3.5

  - ### setHook

    public Object setHook(Node.HookKey key, Object value)
    Sets the value associated with a key.
    Parameters:
      `key` - the key
      `value` - the value
    Returns:
      the old object associated with this key, may be null
    Since:
      3.5

  - ### hookKeys

    public Collection<Node.HookKey> hookKeys()
    Return all the hooks set for this variable.
    Returns:
      a list of all the hooks, an empty list if none set
    Since:
      3.5

  - ### removeHook

    public Object removeHook(Node.HookKey key)
    Remove a hook from a variable
    Parameters:
      `key` -
    Returns:
      the value of the removed hook or null if hook not found
    Since:
      3.5

  - ### copyPropertiesFrom

    public void copyPropertiesFrom(Variable oldvar)
    Copies additional properties from and old variable. This method copies the `isConstant` and `validValue` flags, as well as creating a hook for each hook in the old table. Any existing hooks will be removed.
    Parameters:
      `oldvar` - variable to copy properties to
    Since:
      Jep 4.1

  - ### toString

    public String toString()
    Returns a string with the variable name followed by its value. For example for the variable "a" with the value 10, the following string is returned:

        a: 10

    If the variable is a constant the string " (Constant)" is appended.
    Overrides:
      `toString` in class `Object`
    Returns:
      A string with the variable name and value.