public class OptionalUtilities extends Object
Optional
and exceptions.
See OptionalFactory
for utility functions to create Optional
s.
Modifier and Type | Method and Description |
---|---|
static <S,T,E extends Exception> |
flatMap(Optional<S> optional,
CheckedFunction<S,Optional<T>,E> mapFunction)
Like
Optional.flatMap(java.util.function.Function<? super T, java.util.Optional<U>>) but tolerates an exception in the mapping function, which is
immediately thrown. |
static <S,E extends Exception> |
ifPresent(Optional<S> optional,
CheckedConsumer<S,E> consumerFunction)
Like
Optional.map(java.util.function.Function<? super T, ? extends U>) but tolerates an exception in the mapping function, which is
immediately thrown. |
static <S,T,E extends Exception> |
map(Optional<S> optional,
CheckedFunction<S,T,E> mapFunction)
Like
Optional.map(java.util.function.Function<? super T, ? extends U>) but tolerates an exception in the mapping function, which is
immediately thrown. |
static <T,U,V,E extends Exception> |
mapBoth(Optional<U> optional1,
Optional<V> optional2,
CheckedBiFunction<U,V,T,E> mapFunction)
Mapping only occurs if both
Optional s are non-empty (equivalent to a logical
and on the optionals) |
static <T,E extends Exception> |
orElseGet(Optional<T> optional,
CheckedSupplier<T,E> supplier)
Like
Optional.orElseGet(java.util.function.Supplier<? extends T>) but tolerates an exception in the supplier function, which is
immediately thrown. |
static <T,E extends Exception> |
orElseGetFlat(Optional<T> optional,
CheckedSupplier<Optional<T>,E>... suppliers)
Like
orElseGet(java.util.Optional<T>, org.anchoranalysis.core.functional.checked.CheckedSupplier<T, E>) but returns a Optional and can chain many alternatives. |
static <T> Optional<T> |
orFlat(Iterable<Optional<T>> optionals)
The first optional if it's present, or the second, or the third etc.
|
static <T> Optional<T> |
orFlat(Optional<T>... optional)
Like
orFlat(Iterable) but allows Optional s to be specified as
variable-arguments. |
static <T> Optional<T> |
orFlat(java.util.stream.Stream<Optional<T>> optionals)
The first optional if it's present, or the second, or the third etc.
|
static <T,E extends Exception> |
orFlatSupplier(CheckedSupplier<Optional<T>,E>... optional)
Like
orFlatSupplier(Iterable) but allows Optional s to be specified as
variable-arguments. |
static <T,E extends Exception> |
orFlatSupplier(Iterable<CheckedSupplier<Optional<T>,E>> optionals)
The first optional if it's present, or the second, or the third etc.
|
public static <S,E extends Exception> void ifPresent(Optional<S> optional, CheckedConsumer<S,E> consumerFunction) throws E extends Exception
Optional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is
immediately thrown.S
- optional-typeE
- exception that may be thrown during mapping.optional
- incoming optional.consumerFunction
- the function that is called if an optional contains a value.E
- an exception if consumerFunction
throws it.E extends Exception
public static <S,T,E extends Exception> Optional<T> map(Optional<S> optional, CheckedFunction<S,T,E> mapFunction) throws E extends Exception
Optional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is
immediately thrown.S
- incoming optional-type for mapT
- outgoing optional-type for mapE
- exception that may be thrown during mappingoptional
- incoming optional.mapFunction
- the function that does the mapping from incoming to outgoing.E
- an exception if the mapping function throws it.E extends Exception
public static <T,E extends Exception> T orElseGet(Optional<T> optional, CheckedSupplier<T,E> supplier) throws E extends Exception
Optional.orElseGet(java.util.function.Supplier<? extends T>)
but tolerates an exception in the supplier function, which is
immediately thrown.T
- optional-typeE
- exception that may be thrown during mappingoptional
- incoming optional.supplier
- supplies a value if the optional is empty.E
- an exception if the supplier throws it.E extends Exception
@SafeVarargs public static <T,E extends Exception> Optional<T> orElseGetFlat(Optional<T> optional, CheckedSupplier<Optional<T>,E>... suppliers) throws E extends Exception
orElseGet(java.util.Optional<T>, org.anchoranalysis.core.functional.checked.CheckedSupplier<T, E>)
but returns a Optional
and can chain many alternatives.T
- optional-typeE
- exception that may be thrown during mappingoptional
- incoming optionalsuppliers
- tries each alternative Optional
value successively (if optional is
empty) until one is not-emptyE
- an exception if the supplier throws itE extends Exception
public static <S,T,E extends Exception> Optional<T> flatMap(Optional<S> optional, CheckedFunction<S,Optional<T>,E> mapFunction) throws E extends Exception
Optional.flatMap(java.util.function.Function<? super T, java.util.Optional<U>>)
but tolerates an exception in the mapping function, which is
immediately thrown.S
- incoming optional-type for mapT
- outgoing optional-type for mapE
- exception that may be thrown during mappingoptional
- incoming optional.mapFunction
- the function that does the mapping from incoming to outgoing.E
- an exception if the mapping function throws it.E extends Exception
public static <T,U,V,E extends Exception> Optional<T> mapBoth(Optional<U> optional1, Optional<V> optional2, CheckedBiFunction<U,V,T,E> mapFunction) throws E extends Exception
Optional
s are non-empty (equivalent to a logical
and on the optionals)U
- first incoming optional-type for mapV
- second incoming optional-type for mapT
- outgoing optional-type for mapE
- an exception that may be thrown by an mapFunction
.optional1
- first incoming optional.optional2
- second incoming optional.mapFunction
- the function that does the mapping from both incoming objects to outgoing.E
- if mapFunction
throws it.E extends Exception
public static <T> Optional<T> orFlat(Iterable<Optional<T>> optionals)
Iterable
.T
- type of optionals.optionals
- one or more optionals to combine together using a logical or
operation.optionals[0]
OR optionals[1]
OR optionals[2]
etc.public static <T> Optional<T> orFlat(java.util.stream.Stream<Optional<T>> optionals)
Stream
.T
- type of optionals.optionals
- one or more optionals to combine together using a logical or
operation.optionals[0]
OR optionals[1]
OR optionals[2]
etc.public static <T,E extends Exception> Optional<T> orFlatSupplier(Iterable<CheckedSupplier<Optional<T>,E>> optionals) throws E extends Exception
CheckedSupplier
.T
- type of optionals.E
- an exception that may be thrown by an optional
.optionals
- one or more optionals to combine together using a logical or
operation. * @return a new optional that is optionals[0]
OR optionals[1]
OR optionals[2]
etc.optionals[0]
OR optionals[1]
OR optionals[2]
etc.E
- if any optional
throws it.E extends Exception
@SafeVarargs public static <T> Optional<T> orFlat(Optional<T>... optional)
orFlat(Iterable)
but allows Optional
s to be specified as
variable-arguments.T
- type of optionals.optional
- one or more optionals to combine together using a logical or
operation.optionals[0]
OR optionals[1]
OR optionals[2]
etc.@SafeVarargs public static <T,E extends Exception> Optional<T> orFlatSupplier(CheckedSupplier<Optional<T>,E>... optional) throws E extends Exception
orFlatSupplier(Iterable)
but allows Optional
s to be specified as
variable-arguments.T
- type of optionals.E
- an exception that may be thrown by an optional
.optional
- one or more optionals to combine together using a logical or
operation.optionals[0]
OR optionals[1]
OR optionals[2]
etc.E
- if any optional
throws it.E extends Exception
Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.