Class DerivedObservable<T>

  • Type Parameters:
    T - Type of the value produced.

    public class DerivedObservable<T>
    extends ObservableValue<T>
    This class extends ObservableValue in order to create values that are derived from other observable values. The way they are created is determined by a function of type Supplier<T> passed in the constructor. As another parameter when constructing a DerivedObservable the set of ObservableValues used in the expression has to be given explicitly.

    Internally this derived observable registers itself as a listener to its dependencies. Whenever one of them changes, the stored value is marked as invalid and recalculated upon the net call to get(). Changes to a derived observable are also propagated to any registered listeners, allowing arbitrarily complex hierarchies of derived observable values.

    Author:
    Torsten Hildebrandt
    • Method Detail

      • get

        public T get()
        Returns the current value of this derived observable value. Calling this method calls update() when needed, so the value returned is always the current one.
        Overrides:
        get in class ObservableValue<T>
      • set

        public void set​(T newValue)
        Calling this method on a derived observable doesn't make sense and will always throw an UnsupportedOperationException. Updates to the store value can be triggered explicitly by calling update() or invalidate() followed by a get(), but should usually not be necessary if all dependencies of expression were provided.
        Overrides:
        set in class ObservableValue<T>
        Parameters:
        newValue - The new value to store. Can be null.
      • invalidate

        public void invalidate()
        Marks the currently stored value as invalid so subsequent calls to #get() will trigger a call of update(). This method is also called automatically whenever a dependent observable value has changed or could have changed.
      • update

        public T update()
        Updated the stored value by evaluating expression again. This method usually does not have to be called explicitly but is triggered automatically by get() whenever the value produces by expression might have changed.
      • dependencySet

        public Set<ObservableValue<?>> dependencySet()
        Returns the set of all dependencies used directly in the expression.
        Overrides:
        dependencySet in class ObservableValue<T>
        Returns:
        The dependency set as immutable instance.