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