public abstract class ComputedSet extends AbstractObservableSet
IObservable objects. Any change to one of the observable dependencies
causes the set 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 set of all primes greater than 1 and less than the value
of an IObservableValue < Integer >.
final IObservableValue max = WritableValue.withValueType(Integer.TYPE);
max.setValue(new Integer(0));
IObservableSet primes = new ComputedSet() {
protected Set calculate() {
int maxVal = ((Integer) max.getValue()).intValue();
Set result = new HashSet();
outer: for (int i = 2; i < maxVal; i++) {
for (Iterator it = result.iterator(); it.hasNext();) {
Integer knownPrime = (Integer) it.next();
if (i % knownPrime.intValue() == 0)
continue outer;
}
result.add(new Integer(i));
}
return result;
}
};
System.out.println(primes); // => "[]"
max.setValue(new Integer(20));
System.out.println(primes); // => "[2, 3, 5, 7, 11, 13, 17, 19]"
| Constructor and Description |
|---|
ComputedSet()
Creates a computed set in the default realm and with an unknown (null)
element type.
|
ComputedSet(Object elementType)
Creates a computed set in the default realm and with the given element
type.
|
ComputedSet(Realm realm)
Creates a computed set in given realm and with an unknown (null) element
type.
|
ComputedSet(Realm realm,
Object elementType)
Creates a computed set 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.
|
protected void |
addListener(Object listenerType,
IObservablesListener listener) |
void |
addSetChangeListener(ISetChangeListener listener) |
protected abstract Set |
calculate()
Subclasses must override this method to calculate the set contents.
|
protected Object |
clone() |
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() |
protected void |
fireEvent(ObservableEvent event) |
Object |
getElementType()
Returns the element type of this observable collection, or
null if this observable collection is untyped. |
Realm |
getRealm() |
protected Set |
getWrappedSet() |
protected boolean |
hasListeners() |
boolean |
isStale()
Returns whether the state of this observable is stale and is expected to
change soon.
|
protected void |
removeListener(Object listenerType,
IObservablesListener listener) |
add, addAll, clear, contains, containsAll, equals, fireChange, fireSetChange, firstListenerAdded, getterCalled, hashCode, isEmpty, iterator, lastListenerRemoved, remove, removeAll, removeSetChangeListener, retainAll, setStale, size, toArray, toArray, toStringaddDisposeListener, addStaleListener, checkRealm, fireStale, isDisposed, removeChangeListener, removeDisposeListener, removeStaleListenerfinalize, getClass, notify, notifyAll, wait, wait, waitaddDisposeListener, addStaleListener, getRealm, isDisposed, removeChangeListener, removeDisposeListener, removeStaleListenerpublic ComputedSet()
public ComputedSet(Object elementType)
elementType - the element type, may be null to indicate unknown
element typepublic ComputedSet(Realm realm)
realm - the realmprotected int doGetSize()
protected Set getWrappedSet()
getWrappedSet in class AbstractObservableSetprotected abstract Set calculate()
IObservable, and
implementers must use one of the interface methods tagged TrackedGetter
for ComputedSet to recognize it as a dependency.public boolean isStale()
IObservableisStale in interface IObservableisStale in class AbstractObservableSetpublic Object getElementType()
IObservableCollectionnull if this observable collection is untyped.null if untypedpublic void addChangeListener(IChangeListener listener)
IObservableaddChangeListener in interface IObservableaddChangeListener in class AbstractObservablepublic void addSetChangeListener(ISetChangeListener listener)
addSetChangeListener in interface IObservableSetaddSetChangeListener in class AbstractObservableSetpublic void dispose()
IObservabledispose in interface IObservabledispose in class AbstractObservableprotected void addListener(Object listenerType, IObservablesListener listener)
listenerType - listener - protected void removeListener(Object listenerType, IObservablesListener listener)
listenerType - listener - protected boolean hasListeners()
protected void fireEvent(ObservableEvent event)
public Realm getRealm()
protected Object clone() throws CloneNotSupportedException
clone in class ObjectCloneNotSupportedException
Copyright (c) 2000, 2014 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.