Package jasima.core.util.observer
Class DerivedObservable<T>
- java.lang.Object
-
- jasima.core.util.observer.ObservableValue<T>
-
- jasima.core.util.observer.DerivedObservable<T>
-
- Type Parameters:
T- Type of the value produced.
public class DerivedObservable<T> extends ObservableValue<T>
This class extendsObservableValuein order to create values that are derived from other observable values. The way they are created is determined by a function of typeSupplier<T>passed in the constructor. As another parameter when constructing a DerivedObservable the set ofObservableValues used in the expression has to be given explicitly.Internally this derived observable registers itself as a listener to its dependencies. Whenever one of them changes, the stored value is marked as invalid and recalculated upon the net call to
get(). Changes to a derived observable are also propagated to any registered listeners, allowing arbitrarily complex hierarchies of derived observable values.- Author:
- Torsten Hildebrandt
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jasima.core.util.observer.ObservableValue
ObservableValue.EventType, ObservableValue.ObservableListener<V>
-
-
Constructor Summary
Constructors Constructor Description DerivedObservable(Supplier<T> expression, ObservableValue<?>... dependencies)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Set<ObservableValue<?>>dependencySet()Returns the set of all dependencies used directly in the expression.Tget()Returns the current value of this derived observable value.voidinvalidate()Marks the currently stored value as invalid so subsequent calls to#get()will trigger a call ofupdate().booleanisStale()Returnstrueis the stored value was marked as (potentially) being invalid (seeinvalidate(), so anupdate()is required.voidset(T newValue)Calling this method on a derived observable doesn't make sense and will always throw anUnsupportedOperationException.Tupdate()Updated the stored value by evaluatingexpressionagain.-
Methods inherited from class jasima.core.util.observer.ObservableValue
addListener, addWeakListener, equals, fireEvent, getLastValue, internalSet, numListener, removeListener, update, versionId, whenEquals
-
-
-
-
Constructor Detail
-
DerivedObservable
public DerivedObservable(Supplier<T> expression, ObservableValue<?>... dependencies)
-
-
Method Detail
-
get
public T get()
Returns the current value of this derived observable value. Calling this method callsupdate()when needed, so the value returned is always the current one.- Overrides:
getin classObservableValue<T>
-
set
public void set(T newValue)
Calling this method on a derived observable doesn't make sense and will always throw anUnsupportedOperationException. Updates to the store value can be triggered explicitly by callingupdate()orinvalidate()followed by aget(), but should usually not be necessary if all dependencies ofexpressionwere provided.- Overrides:
setin classObservableValue<T>- Parameters:
newValue- The new value to store. Can benull.
-
invalidate
public void invalidate()
Marks the currently stored value as invalid so subsequent calls to#get()will trigger a call ofupdate(). This method is also called automatically whenever a dependent observable value has changed or could have changed.
-
update
public T update()
Updated the stored value by evaluatingexpressionagain. This method usually does not have to be called explicitly but is triggered automatically byget()whenever the value produces byexpressionmight have changed.
-
isStale
public boolean isStale()
Returnstrueis the stored value was marked as (potentially) being invalid (seeinvalidate(), so anupdate()is required.- Overrides:
isStalein classObservableValue<T>
-
dependencySet
public Set<ObservableValue<?>> dependencySet()
Returns the set of all dependencies used directly in the expression.- Overrides:
dependencySetin classObservableValue<T>- Returns:
- The dependency set as immutable instance.
-
-