Interface Optionals
public interface Optionals
Utility methods to work with
Optional
s.- Author:
- Oliver Gierke, Christoph Strobl
-
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <T> Optional
<T> firstNonEmpty
(Iterable<Supplier<Optional<T>>> suppliers) static <S,
T> Optional <T> firstNonEmpty
(Iterable<S> source, Function<S, Optional<T>> function) Applies the given function to the elements of the source and returns the first non-empty result.static <S,
T> T firstNonEmpty
(Iterable<S> source, Function<S, T> function, T defaultValue) Applies the given function to the elements of the source and returns the first non-empty result.static <T> Optional
<T> firstNonEmpty
(Supplier<Optional<T>>... suppliers) static <T,
S> void ifAllPresent
(Optional<T> left, Optional<S> right, BiConsumer<T, S> consumer) Invokes the givenBiConsumer
if all givenOptional
are present.static <T> void
ifPresentOrElse
(Optional<T> optional, Consumer<? super T> consumer, Runnable runnable) static boolean
isAnyPresent
(Optional<?>... optionals) Returns whether any of the givenOptional
s is present.static <T,
S, R> Optional <R> mapIfAllPresent
(Optional<T> left, Optional<S> right, BiFunction<T, S, R> function) Maps the values contained in the givenOptional
if both of them are present.static <T> Optional
<T> Returns the next element of the givenIterator
orOptional.empty()
in case there is no next element.static <T> Stream
<T> Returns aPair
if bothOptional
instances have values orOptional.empty()
if one or both are missing.
-
Method Details
-
isAnyPresent
-
toStream
- Parameters:
optionals
- must not be null.- Returns:
-
firstNonEmpty
-
firstNonEmpty
-
firstNonEmpty
-
firstNonEmpty
-
next
Returns the next element of the givenIterator
orOptional.empty()
in case there is no next element.- Parameters:
iterator
- must not be null.- Returns:
-
withBoth
-
ifAllPresent
Invokes the givenBiConsumer
if all givenOptional
are present.- Parameters:
left
- must not be null.right
- must not be null.consumer
- must not be null.
-
mapIfAllPresent
static <T,S, Optional<R> mapIfAllPresentR> (Optional<T> left, Optional<S> right, BiFunction<T, S, R> function) Maps the values contained in the givenOptional
if both of them are present.- Parameters:
left
- must not be null.right
- must not be null.function
- must not be null.- Returns:
-
ifPresentOrElse
-