Interface OptionalSupplier<T>
- Type Parameters:
T- The type of value supplied by this supplier.
- All Superinterfaces:
Supplier<T>
- All Known Implementing Classes:
ResettableSupplier,Setting,StringSetting
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Combines the features of
Supplier and Optional into a single class.
This interface extends Supplier and provides convenience methods for working with potentially null values,
similar to Optional. The key difference is that this interface is lazy - the value is only computed when
Supplier.get() is called, and the Optional-like methods operate on that computed value.
Features:
- Extends Supplier - can be used anywhere a Supplier is expected
- Optional-like API - provides isPresent(), isEmpty(), map(), orElse(), etc.
- Lazy evaluation - value is only computed when get() is called
- Null-safe - handles null values gracefully
Usage:
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> OptionalSupplier<T>empty()Creates an empty OptionalSupplier that always returnsnull .default OptionalSupplier<T>If a value is present, and the value matches the given predicate, returns an OptionalSupplier describing the value, otherwise returns an empty OptionalSupplier.default <U> OptionalSupplier<U>flatMap(Function<? super T, ? extends OptionalSupplier<? extends U>> mapper) If a value is present, returns the result of applying the given OptionalSupplier-bearing mapping function to the value, otherwise returns an empty OptionalSupplier.default voidIf a value is present, performs the given action with the value, otherwise does nothing.default voidifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.default booleanisEmpty()Returnstrue if the supplied value isnull .default booleanReturnstrue if the supplied value is notnull .default <U> OptionalSupplier<U>If a value is present, applies the provided mapping function to it and returns an OptionalSupplier describing the result.static <T> OptionalSupplier<T>Creates an OptionalSupplier from a Supplier.static <T> OptionalSupplier<T>ofNullable(T value) Creates an OptionalSupplier that always returns the specified value.default TIf a value is present, returns the value, otherwise returnsother .default TIf a value is present, returns the value, otherwise returns the result produced by the supplying function.orElseThrow(Supplier<? extends X> exceptionSupplier) If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.Converts this OptionalSupplier to anOptional.
-
Method Details
-
of
Creates an OptionalSupplier from a Supplier.- Type Parameters:
T- The value type.- Parameters:
supplier- The supplier. Must not benull .- Returns:
- A new OptionalSupplier instance.
-
ofNullable
Creates an OptionalSupplier that always returns the specified value.- Type Parameters:
T- The value type.- Parameters:
value- The value to return. Can benull .- Returns:
- A new OptionalSupplier instance.
-
empty
Creates an empty OptionalSupplier that always returnsnull .- Type Parameters:
T- The value type.- Returns:
- A new OptionalSupplier instance that always returns
null .
-
isPresent
Returnstrue if the supplied value is notnull .- Returns:
true if the supplied value is notnull .
-
isEmpty
Returnstrue if the supplied value isnull .- Returns:
true if the supplied value isnull .
-
map
If a value is present, applies the provided mapping function to it and returns an OptionalSupplier describing the result.- Type Parameters:
U- The type of the result of the mapping function.- Parameters:
mapper- A mapping function to apply to the value, if present. Must not benull .- Returns:
- An OptionalSupplier describing the result of applying a mapping function to the value of this OptionalSupplier, if a value is present, otherwise an empty OptionalSupplier.
-
flatMap
default <U> OptionalSupplier<U> flatMap(Function<? super T, ? extends OptionalSupplier<? extends U>> mapper) If a value is present, returns the result of applying the given OptionalSupplier-bearing mapping function to the value, otherwise returns an empty OptionalSupplier.- Type Parameters:
U- The type parameter to the OptionalSupplier returned by the mapping function.- Parameters:
mapper- A mapping function to apply to the value, if present. Must not benull .- Returns:
- The result of applying an OptionalSupplier-bearing mapping function to the value of this OptionalSupplier, if a value is present, otherwise an empty OptionalSupplier.
-
filter
If a value is present, and the value matches the given predicate, returns an OptionalSupplier describing the value, otherwise returns an empty OptionalSupplier.- Parameters:
predicate- A predicate to apply to the value, if present. Must not benull .- Returns:
- An OptionalSupplier describing the value of this OptionalSupplier if a value is present and the value matches the given predicate, otherwise an empty OptionalSupplier.
-
orElse
If a value is present, returns the value, otherwise returnsother .- Parameters:
other- The value to be returned if there is no value present. Can benull .- Returns:
- The value, if present, otherwise
other .
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
other- ASupplierwhose result is returned if no value is present. Must not benull .- Returns:
- The value, if present, otherwise the result of
other.get() .
-
orElseThrow
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.- Type Parameters:
X- Type of the exception to be thrown.- Parameters:
exceptionSupplier- The supplying function that produces an exception to be thrown. Must not benull .- Returns:
- The value, if present.
- Throws:
X- If no value is present.
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action- The action to be performed, if a value is present. Must not benull .
-
ifPresentOrElse
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action- The action to be performed, if a value is present. Must not benull .emptyAction- The empty-based action to be performed, if no value is present. Must not benull .
-
toOptional
Converts this OptionalSupplier to anOptional.- Returns:
- An Optional containing the value if present, otherwise an empty Optional.
-