T
- the type of value being observedpublic abstract class ComputedValue<T> extends AbstractObservableValue<T>
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
Constructor and Description |
---|
ComputedValue() |
ComputedValue(Object valueType) |
ComputedValue(Realm realm) |
ComputedValue(Realm realm,
Object valueType) |
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(IChangeListener listener)
Adds the given change listener to the list of change listeners.
|
protected void |
addListener(Object listenerType,
IObservablesListener listener) |
void |
addValueChangeListener(IValueChangeListener<? super T> listener) |
protected abstract T |
calculate()
Subclasses must override this method to provide the object's value.
|
protected Object |
clone() |
static <T> IObservableValue<T> |
create(Supplier<T> supplier)
Factory method to create
ComputedValue objects in an easy manner. |
void |
dispose()
Disposes of this observable object, removing all listeners registered
with this object, and all listeners this object might have registered on
other objects.
|
protected T |
doGetValue() |
protected void |
fireEvent(ObservableEvent event) |
protected void |
firstListenerAdded() |
Realm |
getRealm() |
Object |
getValueType()
The value type of this observable value, or
null if this
observable value is untyped. |
protected boolean |
hasListeners() |
boolean |
isStale()
Returns whether the state of this observable is stale and is expected to
change soon.
|
protected void |
lastListenerRemoved() |
protected void |
makeDirty() |
protected void |
removeListener(Object listenerType,
IObservablesListener listener) |
doSetValue, fireChange, fireValueChange, getValue, removeValueChangeListener, setValue
addDisposeListener, addStaleListener, checkRealm, fireStale, isDisposed, removeChangeListener, removeDisposeListener, removeStaleListener
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addDisposeListener, addStaleListener, getRealm, isDisposed, removeChangeListener, removeDisposeListener, removeStaleListener
public ComputedValue()
public ComputedValue(Object valueType)
valueType
- can be null
public ComputedValue(Realm realm)
realm
- public static <T> IObservableValue<T> create(Supplier<T> supplier)
ComputedValue
objects in an easy manner.
IObservableList
:
IObservableValue<Integer> listSizeObservable = ComputedValue.create(() -> observableList.size());
supplier
- Supplier
, whose Supplier.get()
method is a
TrackedGetter. See
ObservableTracker.getterCalled(IObservable)
for
details.ComputedValue
whose value is computed using the given
Supplier
.protected final T doGetValue()
doGetValue
in class AbstractObservableValue<T>
protected abstract T calculate()
IObservable
, and
implementers must use one of the interface methods tagged TrackedGetter
for ComputedValue to recognize it as a dependency.protected final void makeDirty()
public boolean isStale()
IObservable
isStale
in interface IObservable
isStale
in class AbstractObservableValue<T>
public Object getValueType()
IObservableValue
null
if this
observable value is untyped.null
protected boolean hasListeners()
public void addChangeListener(IChangeListener listener)
IObservable
addChangeListener
in interface IObservable
addChangeListener
in class AbstractObservable
public void addValueChangeListener(IValueChangeListener<? super T> listener)
addValueChangeListener
in interface IObservableValue<T>
addValueChangeListener
in class AbstractObservableValue<T>
public void dispose()
IObservable
dispose
in interface IObservable
dispose
in class AbstractObservable
protected void addListener(Object listenerType, IObservablesListener listener)
listenerType
- listener
- protected void removeListener(Object listenerType, IObservablesListener listener)
listenerType
- listener
- protected void fireEvent(ObservableEvent event)
protected void firstListenerAdded()
protected void lastListenerRemoved()
public Realm getRealm()
protected Object clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException
Copyright (c) 2000, 2016 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.