Class ObservableValue<VALUE>

  • Type Parameters:
    VALUE - The type of the value stored.
    Direct Known Subclasses:
    ConstValue, DerivedObservable

    public class ObservableValue<VALUE>
    extends Object
    This class provides functionality to store a value and allows listeners to get notified whenever this value is changing. Together with its subclass DerivedObservable it allows a simple way to create reactive expressions.

    Instances of this class can be seen s a kind of variables that can be used in more complex expressions provided by DerivedObservable or used directly to e.g. collect statistics or update GUI elements.

    Author:
    Torsten Hildebrandt
    See Also:
    DerivedObservable, ObservableValues
    • Constructor Detail

      • ObservableValue

        public ObservableValue()
        Creates new instance with its value being initialized with null.
      • ObservableValue

        public ObservableValue​(VALUE initialValue)
        Creates new instance with a certain initial value.
        Parameters:
        initialValue - The initial value of this observable value.
    • Method Detail

      • get

        public VALUE get()
        Returns the currently stored value.
      • getLastValue

        public VALUE getLastValue()
        Returns the last value stored. This method returns null if only the initial value was set.
      • set

        public void set​(VALUE newValue)
        Sets a new value. If the new values differs from the old one (checked by Objects.equals(Object, Object)), then all registered listeners are notified about changes.
        Parameters:
        newValue - The new value to store. Can be null.
      • update

        public ObservableValue<VALUE> update​(Function<VALUE,​VALUE> updateFunc)
        Updates the current value of this observable by calling the function passed as a parameter. The current value is passed as a parameter, the value returned is used as the new value.
        Parameters:
        updateFunc - The update function to use.
        Returns:
        the current observable instance to allow chaining calls.
      • internalSet

        protected void internalSet​(VALUE newValue)
      • versionId

        public long versionId()
        Provides a version id. The version id is increasing whenever the stored value changes.
      • isStale

        public boolean isStale()
        Returns true if this the currently stored value needs updating.The implementation here always returns false.
      • dependencySet

        public Set<ObservableValue<?>> dependencySet()
        Returns the set of immediate dependencies, i.e., all values this observable value depends on. The implementation here always returns an empty set.
      • whenEquals

        public void whenEquals​(VALUE v,
                               Runnable action)
      • numListener

        public int numListener()
        Returns the number of listeners currently registered for this observable value.
      • addWeakListener

        public ObservableValue.ObservableListener<VALUE> addWeakListener​(ObservableValue.ObservableListener<VALUE> l)
        Adds a new listener to be notified whenever the stored value is changing. This method stores the listener using a weak reference so it can be garbage collected automatically when there are no further references to it.
        Parameters:
        l - The listener.
        Returns:
        the listener (same as l).
      • fireEvent

        protected void fireEvent​(ObservableValue.EventType event)
        Broadcast an event to all listeners.
        Parameters:
        event - The event denoted by a string.
      • equals

        public boolean equals​(Object obj)
        Returns true if the value currently stored in this object is either equal to obj or if obj is another ObservableValue with the stored values being equals.

        Warning: be careful when storing ObservableValues in Java Collections (especially Maps and Sets), as they might require a somewhat different contract of equals/hashCode.

        Overrides:
        equals in class Object