Package io.helidon.common
Class OptionalHelper<T>
- java.lang.Object
-
- io.helidon.common.OptionalHelper<T>
-
- Type Parameters:
T
- the type of the underlying optional value
public class OptionalHelper<T> extends Object
A wrapper on top ofOptional
to replicate some of the new Java9 methods.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<T>
asOptional()
Get the underlyingOptional
instance.static <T> OptionalHelper<T>
from(Optional<T> optional)
Static factory method to create a newOptionalHelper
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 underlyingOptional
does not have a value, set it to theOptional
produced by the supplying function.Stream<T>
stream()
If a value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.
-
-
-
Method Detail
-
from
public static <T> OptionalHelper<T> from(Optional<T> optional)
Static factory method to create a newOptionalHelper
instance.- Type Parameters:
T
- the type of the underly optional value- Parameters:
optional
- the optional to wrap- Returns:
- the created
OptionalHelper
instance
-
asOptional
public Optional<T> asOptional()
Get the underlyingOptional
instance.- Returns:
- the wrapped
Optional
-
or
public OptionalHelper<T> or(Supplier<? extends Optional<T>> supplier)
If the underlyingOptional
does not have a value, set it to theOptional
produced by the supplying function.- Parameters:
supplier
- the supplying function that produces anOptional
- Returns:
- returns this instance of
OptionalHelper
with the same the underlyingOptional
if a value is present, otherwise with theOptional
produced by the supplying function. - Throws:
NullPointerException
- if the supplying function isnull
or produces anull
result
-
ifPresentOrElse
public 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.- Parameters:
action
- the action to be performed, if a value is presentemptyAction
- the empty-based action to be performed, if no value is present- Throws:
NullPointerException
- if a value is present and the given action isnull
, or no value is present and the given empty-based action isnull
.
-
-