ObservableValue is an entity that wraps a value and allows to
observe the value for changes. In general this interface should not be
implemented directly but one of its sub-interfaces
(ObservableBooleanValue etc.).
The value of the ObservableValue can be requested with
getValue().
An implementation of ObservableValue may support lazy evaluation,
which means that the value is not immediately recomputed after changes, but
lazily the next time the value is requested (see note 1 in "Implementation Requirements").
An ObservableValue generates two types of events: change events and
invalidation events. A change event indicates that the value has changed
(see note 2 in "Implementation Requirements"). An
invalidation event is generated if the current value is not valid anymore.
This distinction becomes important if the ObservableValue supports
lazy evaluation, because for a lazily evaluated value one does not know if an
invalid value really has changed until it is recomputed. For this reason,
generating change events requires eager evaluation while invalidation events
can be generated for eager and lazy implementations.
Implementations of this class should strive to generate as few events as possible to avoid wasting too much time in event handlers. Implementations in this library mark themselves as invalid when the first invalidation event occurs. They do not generate any more invalidation events until their value is recomputed and valid again.
Two types of listeners can be attached to an ObservableValue:
InvalidationListener to listen to invalidation events and
ChangeListener to listen to change events.
Important Note
attaching a ChangeListener enforces eager computation
even if the implementation of the ObservableValue supports lazy
evaluation.
Implementation Specification
Object#equals(Object)) of the value.ObservableBooleanValue, ObservableDoubleValue, ObservableFloatValue, ObservableIntegerValue, ObservableLongValue, ObservableNumberValue, ObservableObjectValue, ObservableStringValue
| Modifier and Type | Method and Description |
|---|---|
| public void | addListener(ChangeListener<? super T>
The listener to register listener)Adds a |
| public default < the type of values held by the resulting U> ObservableValueObservableValue | Returns: anObservableValue that holds the value of an ObservableValue
produced by applying the given mapping function on this value, or
null when the value is null; never returns nullthe mapping function to apply to a value, cannot be mapper)nullReturns an |
| public T | |
| public default < the type of values held by the resulting U> ObservableValueObservableValue | Returns: anObservableValue that holds the result of applying the given
mapping function on this value, or null when it
is null; never returns nullthe mapping function to apply to a value, cannot be mapper)nullReturns an |
| public default ObservableValue | Returns: anObservableValue that holds this value, or the given constant if
it is null; never returns nullthe value to use when this constant)ObservableValue
holds null; can be nullReturns an |
| public void | removeListener(ChangeListener<? super T>
The listener to remove listener)Removes the given listener from the list of listeners that are notified
whenever the value of the |
| public default Subscription | Returns: aSubscription which can be used to cancel this
subscription, never nulla changeSubscriber)BiConsumer to supply with the old and new values
of this ObservableValue, cannot be nullCreates a |
| public default Subscription | Returns: aSubscription which can be used to cancel this
subscription, never nulla valueSubscriber)Consumer to supply with the values of this
ObservableValue, cannot be nullCreates a |
| public default ObservableValue | Returns: anObservableValue that holds this value whenever the given
condition evaluates to true, otherwise holds the last seen value;
never returns nulla boolean condition)ObservableValue, cannot be nullReturns an |
| addListener | back to summary |
|---|---|
| public void addListener(ChangeListener<? super T> listener) Adds a
Note that the same actual
The
| |
| flatMap | back to summary |
|---|---|
| public default <U> ObservableValue Returns an
This method is similar to
For example, a property that is only
Changes in any of the values of: the scene of listView, the window of that scene, or
the showing of that window, will update the boolean value isShowing.
This method is preferred over
| |
| getValue | back to summary |
|---|---|
| public T getValue() Returns the current value of this
| |
| map | back to summary |
|---|---|
| public default <U> ObservableValue Returns an For example, mapping a string to an upper case string:
| |
| orElse | back to summary |
|---|---|
| public default ObservableValue Returns an
For example, mapping a string to an upper case string, but leaving it blank
if the input is
| |
| removeListener | back to summary |
|---|---|
| public void removeListener(ChangeListener<? super T> listener) Removes the given listener from the list of listeners that are notified
whenever the value of the If the given listener has not been previously registered (i.e. it was never added) then this method call is a no-op. If it had been previously added then it will be removed. If it had been added more than once, then only the first occurrence will be removed.
| |
| subscribe | back to summary |
|---|---|
| public default Subscription subscribe(BiConsumer<? super T, ? super T> changeSubscriber) Creates a
The parameters supplied to the
Note that the same subscriber instance may be safely subscribed for
different
Also note that when subscribing on an
| |
| subscribe | back to summary |
|---|---|
| public default Subscription subscribe(Consumer<? super T> valueSubscriber) Creates a
Note that the same subscriber instance may be safely subscribed for
different
Also note that when subscribing on an
| |
| when | back to summary |
|---|---|
| public default ObservableValue Returns an
The returned
A For example:
| |