Class ComputedValue<T>

Type Parameters:
T - the type of value being observed
All Implemented Interfaces:
IObservable, IObservableValue<T>
Direct Known Subclasses:
AggregateValidationStatus

public abstract class ComputedValue<T> extends AbstractObservableValue<T>
A Lazily calculated value that automatically computes and registers listeners on its dependencies as long as all of its dependencies are IObservable objects. Any change to one of the observable dependencies causes the value to be recomputed.

This class is thread safe. All state accessing methods must be invoked from the current realm. Methods for adding and removing listeners may be invoked from any thread.

Example: compute the sum of all elements in an IObservableList < Integer >.

 final IObservableList addends = WritableValue.withValueType(Integer.TYPE);
 addends.add(Integer.valueOf(0));
 addends.add(Integer.valueOf(1));
 addends.add(Integer.valueOf(2));

 IObservableValue sum = new ComputedValue() {
        protected Object calculate() {
                int sum = 0;
                for (Iterator it = addends.iterator(); it.hasNext();) {
                        Integer addend = (Integer) it.next();
                        sum += addend.intValue();
                }
                return sum;
        }
 };

 System.out.println(sum.getValue()); // => 3

 addends.add(Integer.valueOf(10));
 System.out.println(sum.getValue()); // => 13
 
Since:
1.0
  • Constructor Details

    • ComputedValue

      public ComputedValue()
    • ComputedValue

      public ComputedValue(Object valueType)
      Parameters:
      valueType - can be null
    • ComputedValue

      public ComputedValue(Realm realm)
      Parameters:
      realm - the realm to use; not null
    • ComputedValue

      public ComputedValue(Realm realm, Object valueType)
      Parameters:
      realm - the realm to use; not null
      valueType - can be null
  • Method Details

    • create

      public static <T> IObservableValue<T> create(Supplier<T> supplier)
      Factory method to create ComputedValue objects in an easy manner.

      The created list has a null IObservableValue.getValueType().

      Example observing the size of an IObservableList:

       IObservableValue<Integer> listSizeObservable = ComputedValue.create(() -> observableList.size());
       
      Parameters:
      supplier - Supplier, which is tracked using ObservableTracker to find out observables it uses, in the same manner as calculate().
      Returns:
      ComputedValue whose value is computed using the given Supplier.
      Since:
      1.6
    • doGetValue

      protected final T doGetValue()
      Specified by:
      doGetValue in class AbstractObservableValue<T>
    • calculate

      protected abstract T calculate()
      Subclasses must override this method to provide the object's value. Any dependencies used to calculate the value must be IObservable, and implementers must use one of the interface methods tagged TrackedGetter for ComputedValue to recognize it as a dependency.
      Returns:
      the object's value
    • makeDirty

      protected final void makeDirty()
    • isStale

      public boolean isStale()
      Description copied from interface: IObservable
      Returns whether the state of this observable is stale and is expected to change soon. A non-stale observable that becomes stale will notify its stale listeners. A stale object that becomes non-stale does so by changing its state and notifying its change listeners, it does not notify its stale listeners about becoming non-stale. Clients that do not expect asynchronous changes may ignore staleness of observable objects.
      Specified by:
      isStale in interface IObservable
      Overrides:
      isStale in class AbstractObservableValue<T>
      Returns:
      true if this observable's state is stale and will change soon.
    • getValueType

      public Object getValueType()
      Description copied from interface: IObservableValue
      The value type of this observable value, or null if this observable value is untyped.
      Returns:
      the value type, or null
    • hasListeners

      protected boolean hasListeners()
      Since:
      1.1
    • addChangeListener

      public void addChangeListener(IChangeListener listener)
      Description copied from interface: IObservable
      Adds the given change listener to the list of change listeners. Change listeners are notified about changes of the state of this observable in a generic way, without specifying the change that happened. To get the changed state, a change listener needs to query for the current state of this observable.
      Specified by:
      addChangeListener in interface IObservable
      Overrides:
      addChangeListener in class AbstractObservable
      Parameters:
      listener - the listener to add; not null
    • addValueChangeListener

      public void addValueChangeListener(IValueChangeListener<? super T> listener)
      Specified by:
      addValueChangeListener in interface IObservableValue<T>
      Overrides:
      addValueChangeListener in class AbstractObservableValue<T>
      Parameters:
      listener - the change listener to add; not null
    • dispose

      public void dispose()
      Description copied from interface: IObservable
      Disposes of this observable object, removing all listeners registered with this object, and all listeners this object might have registered on other objects.
      Specified by:
      dispose in interface IObservable
      Overrides:
      dispose in class AbstractObservable
    • addListener

      protected void addListener(Object listenerType, IObservablesListener listener)
      Parameters:
      listenerType - arbitrary object to identify a type of the listener
      listener - the listener to add; not null
    • removeListener

      protected void removeListener(Object listenerType, IObservablesListener listener)
      Parameters:
      listenerType - arbitrary object to identify a type of the listener
      listener - the listener to remove; not null
    • fireEvent

      protected void fireEvent(ObservableEvent event)
    • firstListenerAdded

      protected void firstListenerAdded()
    • lastListenerRemoved

      protected void lastListenerRemoved()
    • getRealm

      public Realm getRealm()
      Returns:
      Returns the realm.
    • clone

      protected Object clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException