Package org.eclipse.core.databinding.observable.list

# Class ComputedList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.eclipse.core.databinding.observable.list.AbstractObservableList<E>
org.eclipse.core.databinding.observable.list.ComputedList<E>

Type Parameters:
  `E` - the list element type

All Implemented Interfaces:
  `Iterable<E>`, `Collection<E>`, `List<E>`, `IObservable`, `IObservableCollection<E>`, `IObservableList<E>`

public abstract class ComputedList<E> extends AbstractObservableList<E>
A lazily calculated list 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 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]"

Since:
  1.1

## Field Summary

### Fields inherited from class java.util.AbstractList

`modCount`

## Constructor Summary

Constructors
Constructor
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.

## Method Summary

Modifier and Type
Method
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.
`static <E> IObservableList<E>`
`create(Supplier<List<E>> supplier)`
Factory method to create `ComputedList` 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 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.

### Methods inherited from class org.eclipse.core.databinding.observable.list.AbstractObservableList

`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`

### Methods inherited from class java.util.AbstractList

`add, clear, listIterator, listIterator, remove, removeRange, set, subList`

### Methods inherited from class java.util.AbstractCollection

`toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.util.Collection

`parallelStream, removeIf, stream, toArray`

### Methods inherited from interface org.eclipse.core.databinding.observable.list.IObservableList

`listIterator, listIterator, remove, set, subList`

### Methods inherited from interface java.lang.Iterable

`forEach`

### Methods inherited from interface java.util.List

`add, clear, replaceAll, sort, spliterator`

## Constructor Details

### ComputedList

public ComputedList()
Creates a computed list in the default realm and with an unknown (null) element type.

### ComputedList

public ComputedList(Object elementType)
Creates a computed list in the default realm and with the given element type.

Parameters:
  `elementType` - the element type, may be `null` to indicate unknown element type

### ComputedList

public ComputedList(Realm realm)
Creates a computed list in given realm and with an unknown (null) element type.

Parameters:
  `realm` - the realm

### ComputedList

public ComputedList(Realm realm, Object elementType)
Creates a computed list in the given realm and with the given element type.

Parameters:
  `realm` - the realm
  `elementType` - the element type, may be `null` to indicate unknown element type

## Method Details

### create

public static <E> IObservableList<E> create(Supplier<List<E>> supplier)
Factory method to create `ComputedList` objects in an easy manner.

The created list has a null `IObservableList.getElementType()`.

Parameters:
  `supplier` - `Supplier`, which is tracked using `ObservableTracker` to find out observables it uses, in the same manner as `calculate()`.
Returns:
  `ComputedList` whose elements are computed using the given `Supplier`.
Since:
  1.12

### doGetSize

protected int doGetSize()

Specified by:
  `doGetSize` in class `AbstractObservableList<E>`
Returns:
  the size

### get

public E get(int index)

Specified by:
  `get` in interface `IObservableList<E>`
Specified by:
  `get` in interface `List<E>`
Specified by:
  `get` in class `AbstractList<E>`

### calculate

protected abstract List<E> calculate()
Subclasses must override this method to calculate the list contents. Any dependencies used to calculate the list must be `IObservable`, and implementers must use one of the interface methods tagged TrackedGetter for ComputedList to recognize it as a dependency.

Returns:
  the object's list.

### 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 `AbstractObservableList<E>`
Returns:
  true if this observable's state is stale and will change soon.

### getElementType

public Object getElementType()
Description copied from interface: `IObservableCollection`
Returns the element type of this observable collection, or `null` if this observable collection is untyped.

Returns:
  the type of the elements or `null` if untyped

### 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 `AbstractObservableList<E>`
Parameters:
  `listener` - the listener to add; not `null`

### addListChangeListener

public void addListChangeListener(IListChangeListener<? super E> listener)
Description copied from interface: `IObservableList`
Adds the given list change listener to the list of list change listeners.

Specified by:
  `addListChangeListener` in interface `IObservableList<E>`
Overrides:
  `addListChangeListener` in class `AbstractObservableList<E>`
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 `AbstractObservableList<E>`