Module com.tobiasdiez.easybind
Package com.tobiasdiez.easybind.optional
Interface ObservableOptionalValue<T>
- All Superinterfaces:
javafx.beans.Observable,javafx.beans.value.ObservableObjectValue<Optional<T>>,javafx.beans.value.ObservableValue<Optional<T>>
- All Known Subinterfaces:
OptionalBinding<T>
- All Known Implementing Classes:
OptionalWrapper,PreboundOptionalBinding
public interface ObservableOptionalValue<T>
extends javafx.beans.value.ObservableObjectValue<Optional<T>>
An
ObservableValue that may or may not contain values (i.e. an Optional in the category of observable values).-
Method Summary
Modifier and TypeMethodDescriptiondefault EasyObservableValue<T>Converts this optional observable to an ordinary observable, holdingnullif this observable does not hold any value.Returns a new observable that holds the same value as this observable when the value is present and matches the given predicate, otherwise it holds an empty optional.<U> OptionalBinding<U>flatMapOpt(Function<T, Optional<U>> mapper) Returns a new observable that holds the result of applying the given function to the value as this observable, if present, otherwise empty.default TgetValueOrElse(T other) If this observable holds a value, returns the value, otherwise returnsother.default TIf this observable holds a value, returns the value, otherwise throwsNoSuchElementException.default voidifValuePresent(Consumer<? super T> action) If this observable holds a value, invokes the given function with the value, otherwise does nothing.javafx.beans.binding.BooleanBindingisEmpty()Returns a new observable that holdstrueif this observable does not hold value, orfalseotherwise.javafx.beans.binding.BooleanBindingReturns a new observable that holdstrueif this observable holds value, orfalseotherwise.default booleanChecks whether this observable has no value.default booleanChecks whether this observable holds a value.default Subscriptiondefault SubscriptionlistenToValues(SimpleChangeListener<? super T> listener) Adds a change listener and returns aSubscriptionthat can be used to remove that listener.default <U,O extends javafx.beans.value.ObservableValue<U>>
OptionalBinding<U>mapObservable(Function<? super T, O> mapper) Returns a new observable that holds the value of the observable resulting from applying the given function to the value as this observable.<U> OptionalBinding<U>Returns anObservableValuethat holds the result of applying the given mapping function on this value.Returns a new observable that holds the value held by this observable, or the value held byotherwhen this observable is empty.Returns a new observable that holds the value held by this observable, orotherwhen this observable is empty.default <U,O extends javafx.beans.property.Property<U>>
PropertyBinding<U>selectProperty(Function<? super T, O> mapper) default Subscriptiondefault SubscriptionsubscribeToValues(Consumer<? super T> subscriber) Invokes thesubscriberfor the current value, if present, and every new value.Methods inherited from interface javafx.beans.Observable
addListener, removeListenerMethods inherited from interface javafx.beans.value.ObservableObjectValue
getMethods inherited from interface javafx.beans.value.ObservableValue
addListener, flatMap, getValue, map, orElse, removeListener, when
-
Method Details
-
isValuePresent
default boolean isValuePresent()Checks whether this observable holds a value.- Returns:
trueif a value is present, otherwisefalse
-
isValueEmpty
default boolean isValueEmpty()Checks whether this observable has no value.- Returns:
trueif a value is not present, otherwisefalse
-
ifValuePresent
If this observable holds a value, invokes the given function with the value, otherwise does nothing.- Parameters:
action- the function to invoke on the value currently held by this observable
-
getValueOrElseThrow
If this observable holds a value, returns the value, otherwise throwsNoSuchElementException.- Throws:
NoSuchElementException- if no is value present
-
getValueOrElse
If this observable holds a value, returns the value, otherwise returnsother.- Parameters:
other- value to return if there is no value present in this observable- Returns:
- the value, if present, otherwise
other.
-
asOrdinary
Converts this optional observable to an ordinary observable, holdingnullif this observable does not hold any value.- Returns:
- an observable that has the same value as this observable, if present, otherwise
null.
-
orElseOpt
Returns a new observable that holds the value held by this observable, orotherwhen this observable is empty.This method is similar to the JavaFX 19 method
orElsebut recognizes an empty optional instead ofnullvalues. -
orElseOpt
Returns a new observable that holds the value held by this observable, or the value held byotherwhen this observable is empty.This method is similar to the JavaFX 19 method
orElsebut recognizes an empty optional instead ofnullvalues and allows an observable as fallback. -
filter
Returns a new observable that holds the same value as this observable when the value is present and matches the given predicate, otherwise it holds an empty optional.- Parameters:
predicate- the predicate to apply to a value, if present- Throws:
NullPointerException- if the predicate isnull
-
mapOpt
Returns anObservableValuethat holds the result of applying the given mapping function on this value. The result is updated when thisObservableOptionalValuechanges. If this value is an empty optional, no mapping is applied and the resulting value is also an empty optional. If the function returnsnull, then this is converted into an empty optional.This method is similar to the JavaFX 19 method
mapbut with special handling of empty optionals instead ofnullvalues.- Parameters:
mapper- the mapping to apply to a value, if present- See Also:
-
flatMapOpt
Returns a new observable that holds the result of applying the given function to the value as this observable, if present, otherwise empty.This method is similar to
ObservableValue.map(Function), but the mapping function is one whose result is already anOptional, and if invoked,flatMapdoes not wrap it within an additionalOptional.This method should not be confused with the JavaFX 19 method
flatMapwhich accepts amapperproducing an observable value, and not anOptional.- Parameters:
mapper- the mapping to apply to a value, if present
-
mapObservable
default <U,O extends javafx.beans.value.ObservableValue<U>> OptionalBinding<U> mapObservable(Function<? super T, O> mapper) Returns a new observable that holds the value of the observable resulting from applying the given function to the value as this observable. If this observable is empty or the function returnsnull, then this is converted to an empty optional.- Parameters:
mapper- the mapping to apply to a value, if present- See Also:
-
selectProperty
default <U,O extends javafx.beans.property.Property<U>> PropertyBinding<U> selectProperty(Function<? super T, O> mapper) -
isPresent
javafx.beans.binding.BooleanBinding isPresent()Returns a new observable that holdstrueif this observable holds value, orfalseotherwise. -
isEmpty
javafx.beans.binding.BooleanBinding isEmpty()Returns a new observable that holdstrueif this observable does not hold value, orfalseotherwise. -
listenToValues
Adds a change listener and returns aSubscriptionthat can be used to remove that listener. The listener will only be invoked if the new value is present, uselisten(ChangeListener)if you want to be notified about empty values as well. -
subscribeToValues
Invokes thesubscriberfor the current value, if present, and every new value. Usesubscribe(Consumer)if you want to be notified about empty values as well.- Parameters:
subscriber- action to invoke for values of this observable.- Returns:
- a subscription that can be used to stop invoking subscriber for any further changes.
- See Also:
-
subscribe
-
listen
-