T - the type of the underlying optional valuepublic class OptionalHelper<T> extends Object
Optional to replicate some of the new Java9
methods.| Modifier and Type | Method and Description |
|---|---|
Optional<T> |
asOptional()
Get the underlying
Optional instance. |
static <T> OptionalHelper<T> |
from(Optional<T> optional)
Static factory method to create a new
OptionalHelper instance. |
void |
ifPresentOrElse(Consumer<T> action,
Runnable emptyAction)
If a value is present, performs the given action with the value,
otherwise performs the given empty-based action.
|
OptionalHelper<T> |
or(Supplier<? extends Optional<T>> supplier)
If the underlying
Optional does not have a value, set it to the
Optional produced by the supplying function. |
Stream<T> |
stream()
If a value is present, returns a sequential
Stream containing
only that value, otherwise returns an empty Stream. |
public static <T> OptionalHelper<T> from(Optional<T> optional)
OptionalHelper instance.T - the type of the underly optional valueoptional - the optional to wrapOptionalHelper instancepublic Optional<T> asOptional()
Optional instance.Optionalpublic OptionalHelper<T> or(Supplier<? extends Optional<T>> supplier)
Optional does not have a value, set it to the
Optional produced by the supplying function.supplier - the supplying function that produces an OptionalOptionalHelper with the same
the underlying Optional if a value is present, otherwise
with the Optional produced by the supplying function.NullPointerException - if the supplying function is null or
produces a null resultpublic void ifPresentOrElse(Consumer<T> action, Runnable emptyAction)
action - the action to be performed, if a value is presentemptyAction - the empty-based action to be performed, if no value is
presentNullPointerException - if a value is present and the given action
is null, or no value is present and the given empty-based
action is null.Copyright © 2018, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.