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 extendsObservableValue
in 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 ofObservableValue
s 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.T
get()
Returns the current value of this derived observable value.void
invalidate()
Marks the currently stored value as invalid so subsequent calls to#get()
will trigger a call ofupdate()
.boolean
isStale()
Returnstrue
is the stored value was marked as (potentially) being invalid (seeinvalidate()
, so anupdate()
is required.void
set(T newValue)
Calling this method on a derived observable doesn't make sense and will always throw anUnsupportedOperationException
.T
update()
Updated the stored value by evaluatingexpression
again.-
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:
get
in 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 ofexpression
were provided.- Overrides:
set
in 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 evaluatingexpression
again. This method usually does not have to be called explicitly but is triggered automatically byget()
whenever the value produces byexpression
might have changed.
-
isStale
public boolean isStale()
Returnstrue
is the stored value was marked as (potentially) being invalid (seeinvalidate()
, so anupdate()
is required.- Overrides:
isStale
in classObservableValue<T>
-
dependencySet
public Set<ObservableValue<?>> dependencySet()
Returns the set of all dependencies used directly in the expression.- Overrides:
dependencySet
in classObservableValue<T>
- Returns:
- The dependency set as immutable instance.
-
-