Package org.springframework.data.util
Interface Optionals
public interface Optionals
Utility methods to work with
Optionals.- 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 givenBiConsumerif all givenOptionalare present.static <T> voidifPresentOrElse(Optional<T> optional, Consumer<? super T> consumer, Runnable runnable) static booleanisAnyPresent(Optional<?>... optionals) Returns whether any of the givenOptionals 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 givenOptionalif both of them are present.static <T> Optional<T>Returns the next element of the givenIteratororOptional.empty()in case there is no next element.static <T> Stream<T>Returns aPairif bothOptionalinstances have values orOptional.empty()if one or both are missing.
-
Method Details
-
isAnyPresent
Returns whether any of the givenOptionals is present.- Parameters:
optionals- must not be null.- Returns:
-
toStream
- Parameters:
optionals- must not be null.- Returns:
-
firstNonEmpty
Applies the given function to the elements of the source and returns the first non-empty result.- Parameters:
source- must not be null.function- must not be null.- Returns:
-
firstNonEmpty
Applies the given function to the elements of the source and returns the first non-empty result.- Parameters:
source- must not be null.function- must not be null.- Returns:
-
firstNonEmpty
- Parameters:
suppliers- must not be null.- Returns:
-
firstNonEmpty
- Parameters:
suppliers- must not be null.- Returns:
-
next
Returns the next element of the givenIteratororOptional.empty()in case there is no next element.- Parameters:
iterator- must not be null.- Returns:
-
withBoth
Returns aPairif bothOptionalinstances have values orOptional.empty()if one or both are missing.- Parameters:
left-right-- Returns:
-
ifAllPresent
Invokes the givenBiConsumerif all givenOptionalare 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 givenOptionalif both of them are present.- Parameters:
left- must not be null.right- must not be null.function- must not be null.- Returns:
-
ifPresentOrElse
static <T> void ifPresentOrElse(Optional<T> optional, Consumer<? super T> consumer, Runnable runnable) - Parameters:
optional- must not be null.consumer- must not be null.runnable- must not be null.
-