Class Variable

java.lang.Object
com.singularsys.jep.Variable
All Implemented Interfaces:
Serializable

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:
  • Field Details

  • 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
      Parameters:
      var -
  • 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 object)
      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.

      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:
      false 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
    • 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.