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, toArray
add, clear, listIterator, listIterator, remove, removeRange, set, subList
toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
listIterator, listIterator, remove, set, subList
add, clear, replaceAll, sort, spliterator
parallelStream, removeIf, stream
public 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 AbstractObservableList
public Object get(int index)
get
in interface List
get
in interface IObservableList
get
in class AbstractList
protected 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()
IObservable
isStale
in interface IObservable
isStale
in class AbstractObservableList
public Object getElementType()
IObservableCollection
null
if this observable collection is untyped.null
if untypedpublic void addChangeListener(IChangeListener listener)
IObservable
addChangeListener
in interface IObservable
addChangeListener
in class AbstractObservableList
public void addListChangeListener(IListChangeListener listener)
IObservableList
addListChangeListener
in interface IObservableList
addListChangeListener
in class AbstractObservableList
public void dispose()
IObservable
dispose
in interface IObservable
dispose
in class AbstractObservableList
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.