public abstract class ComputedList extends AbstractObservableList
IObservable objects. Any change to one of the observable dependencies
causes the list 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 fibonacci sequence, up to as many elements as the value
of an IObservableValue < Integer >.
final IObservableValue count = WritableValue.withValueType(Integer.TYPE);
count.setValue(new Integer(0));
IObservableList fibonacci = new ComputedList() {
protected List calculate() {
int size = ((Integer) count.getValue()).intValue();
List result = new ArrayList();
for (int i = 0; i < size; i++) {
if (i == 0)
result.add(new Integer(0));
else if (i == 1)
result.add(new Integer(1));
else {
Integer left = (Integer) result.get(i - 2);
Integer right = (Integer) result.get(i - 1);
result.add(new Integer(left.intValue() + right.intValue()));
}
}
return result;
}
};
System.out.println(fibonacci); // => "[]"
count.setValue(new Integer(5));
System.out.println(fibonacci); // => "[0, 1, 1, 2, 3]"
modCount| Constructor and Description |
|---|
ComputedList()
Creates a computed list in the default realm and with an unknown (null)
element type.
|
ComputedList(Object elementType)
Creates a computed list in the default realm and with the given element
type.
|
ComputedList(Realm realm)
Creates a computed list in given realm and with an unknown (null) element
type.
|
ComputedList(Realm realm,
Object elementType)
Creates a computed list in the given realm and with the given element
type.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(IChangeListener listener)
Adds the given change listener to the list of change listeners.
|
void |
addListChangeListener(IListChangeListener listener)
Adds the given list change listener to the list of list change listeners.
|
protected abstract List |
calculate()
Subclasses must override this method to calculate the list contents.
|
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 int |
doGetSize() |
Object |
get(int index) |
Object |
getElementType()
Returns the element type of this observable collection, or
null if this observable collection is untyped. |
boolean |
isStale()
Returns whether the state of this observable is stale and is expected to
change soon.
|
add, addAll, addAll, addDisposeListener, addStaleListener, checkRealm, contains, containsAll, equals, fireChange, fireListChange, fireStale, firstListenerAdded, getRealm, hashCode, hasListeners, indexOf, isDisposed, isEmpty, iterator, lastIndexOf, lastListenerRemoved, move, remove, removeAll, removeChangeListener, removeDisposeListener, removeListChangeListener, removeStaleListener, retainAll, size, toArray, toArrayadd, clear, listIterator, listIterator, remove, removeRange, set, subListtoStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitlistIterator, listIterator, remove, set, subListpublic ComputedList()
public ComputedList(Object elementType)
elementType - the element type, may be null to indicate unknown
element typepublic ComputedList(Realm realm)
realm - the realmprotected int doGetSize()
doGetSize in class AbstractObservableListpublic Object get(int index)
get in interface Listget in interface IObservableListget in class AbstractListprotected abstract List calculate()
IObservable, and
implementers must use one of the interface methods tagged TrackedGetter
for ComputedList to recognize it as a dependency.public boolean isStale()
IObservableisStale in interface IObservableisStale in class AbstractObservableListpublic Object getElementType()
IObservableCollectionnull if this observable collection is untyped.null if untypedpublic void addChangeListener(IChangeListener listener)
IObservableaddChangeListener in interface IObservableaddChangeListener in class AbstractObservableListpublic void addListChangeListener(IListChangeListener listener)
IObservableListaddListChangeListener in interface IObservableListaddListChangeListener in class AbstractObservableListpublic void dispose()
IObservabledispose in interface IObservabledispose in class AbstractObservableList
Copyright (c) 2000, 2014 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.