E
- the list element typepublic abstract class ComputedList<E> extends AbstractObservableList<E>
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(Integer.valueOf(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(Integer.valueOf(0)); else if (i == 1) result.add(Integer.valueOf(1)); else { Integer left = (Integer) result.get(i - 2); Integer right = (Integer) result.get(i - 1); result.add(Integer.valueOf(left.intValue() + right.intValue())); } } return result; } }; System.out.println(fibonacci); // => "[]" count.setValue(Integer.valueOf(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<? super E> listener)
Adds the given list change listener to the list of list change listeners.
|
protected abstract List<E> |
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() |
E |
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<E>
public E get(int index)
get
in interface List<E>
get
in interface IObservableList<E>
get
in class AbstractList<E>
protected abstract List<E> 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<E>
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<E>
public void addListChangeListener(IListChangeListener<? super E> listener)
IObservableList
addListChangeListener
in interface IObservableList<E>
addListChangeListener
in class AbstractObservableList<E>
public void dispose()
IObservable
dispose
in interface IObservable
dispose
in class AbstractObservableList<E>
Copyright (c) 2000, 2016 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.