public final class OptionalUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> void |
ifPresentOrElse(java.util.Optional<T> value,
java.util.function.Consumer<T> action,
java.lang.Runnable emptyAction)
Invokes a consumer if the Optional has a value, otherwise invoked a
Runnable when the Optional is empty.
|
static <T> java.util.Optional<T> |
or(java.util.Optional<T> value,
java.util.function.Supplier<java.util.Optional<? extends T>> supplier)
Retrieves the value of the optional if present or invokes the supplier
for a value.
|
static <T> java.util.stream.Stream<T> |
stream(java.util.Optional<T> value)
Converts an Optional into a Stream that can be used in a flatmap.
|
public static <T> java.util.Optional<T> or(java.util.Optional<T> value, java.util.function.Supplier<java.util.Optional<? extends T>> supplier)
T
- Type of value.value
- Value to check.supplier
- Supplier for a value if no value is present.public static <T> java.util.stream.Stream<T> stream(java.util.Optional<T> value)
This is a polyfill of Java 9's Optional#stream
.
T
- Value type.value
- Value to convert to a stream.public static <T> void ifPresentOrElse(java.util.Optional<T> value, java.util.function.Consumer<T> action, java.lang.Runnable emptyAction)
T
- Type of value.value
- Value to check.action
- Action to invoke if a value is present.emptyAction
- Runnable to invoke if a value is not present.