Class Variable

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    BoundVariable, XVariable

    public class Variable
    extends java.util.Observable
    implements java.io.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.

    This class extends java.util.Observable allowing observers to track if the variable value has been changed. Use addObserver(Observer o) to add an observer. When the variable value is changed the Observer's update(o, arg) method is called, the first argument is the Variable object and the second is the object representing the new value.

    Version:
    2.3.0 beta 2 Now extends Observable so observers can track if the value has been changed.
    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 java.lang.Object[] keyVals  
      protected java.lang.String name  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Variable​(Variable var)
      Copy constructor
      protected Variable​(java.lang.String name)
      Constructors are protected.
      protected Variable​(java.lang.String name, java.lang.Object value)
      Constructors are protected.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object getHook​(Node.HookKey key)
      Get the value associated with a key.
      java.lang.String getName()
      Returns the variable name.
      java.lang.Object getValue()
      Returns the variable's value.
      boolean hasValidValue()
      Is the value of this variable valid?
      java.util.Collection<Node.HookKey> hookKeys()
      Return all the hooks set for this variable.
      boolean isConstant()
      Returns true if the variable is a constant.
      java.lang.Object removeHook​(Node.HookKey key)
      Remove a hook from a variable
      java.lang.Object setHook​(Node.HookKey key, java.lang.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​(java.lang.Object object)
      Sets the value of the variable.
      boolean setValue​(java.lang.Object object, java.util.Observer observer)
      Sets the value of the variable.
      protected boolean setValueRaw​(java.lang.Object object)
      In general subclasses should override this method rather than setValue.
      java.lang.String toString()
      Returns a string with the variable name followed by it's value.
      • Methods inherited from class java.util.Observable

        addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • name

        protected java.lang.String name
      • keyVals

        protected java.lang.Object[] keyVals
    • Constructor Detail

      • Variable

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

        protected Variable​(java.lang.String name,
                           java.lang.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 Detail

      • getName

        public java.lang.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 java.lang.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​(java.lang.Object object)
        Sets the value of the variable. Constant values cannot be changed.

        This method calls java.util.Observable.notifyObservers() to indicate to anyone interested that the value has been changed. Note subclasses should override setValueRaw rather than this method so they do not need to handle the Observable methods.

        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
      • setValue

        public final boolean setValue​(java.lang.Object object,
                                      java.util.Observer observer)
        Sets the value of the variable. The observer argument is passed to all observers update method. This can help an observer detect if it set the variables value itself, and can lead to a performance boost.
        Parameters:
        object -
        observer -
        Returns:
        false if tried to change a constant value
      • setValueRaw

        protected boolean setValueRaw​(java.lang.Object object)
        In general subclasses should override this method rather than setValue. This is because setValue calls this method and then notifies any observers.

        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 java.lang.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 java.lang.Object setHook​(Node.HookKey key,
                                        java.lang.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 java.util.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 java.lang.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 java.lang.String toString()
        Returns a string with the variable name followed by it's 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 java.lang.Object
        Returns:
        A string with the variable name and value.