Class OptionalUtilities
Object
OptionalUtilities
Additional utility functions for
Optional
and exceptions.
See OptionalFactory
for utility functions to create Optional
s.
- Author:
- Owen Feehan
-
Method Summary
Modifier and TypeMethodDescriptionflatMap
(Optional<S> optional, CheckedFunction<S, Optional<T>, E> mapFunction) LikeOptional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>)
but tolerates an exception in the mapping function, which is immediately thrown.static <S,
E extends Exception>
voidifPresent
(Optional<S> optional, CheckedConsumer<S, E> consumerFunction) LikeOptional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is immediately thrown.map
(Optional<S> optional, CheckedFunction<S, T, E> mapFunction) LikeOptional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is immediately thrown.mapBoth
(Optional<U> optional1, Optional<V> optional2, CheckedBiFunction<U, V, T, E> mapFunction) Mapping only occurs if bothOptional
s are non-empty (equivalent to a logical and on the optionals)static <T,
E extends Exception>
TorElseGet
(Optional<T> optional, CheckedSupplier<T, E> supplier) LikeOptional.orElseGet(java.util.function.Supplier<? extends T>)
but tolerates an exception in the supplier function, which is immediately thrown.orElseGetFlat
(Optional<T> optional, CheckedSupplier<Optional<T>, E>... suppliers) LikeorElseGet(java.util.Optional<T>, org.anchoranalysis.core.functional.checked.CheckedSupplier<T, E>)
but returns aOptional
and can chain many alternatives.static <T> Optional
<T> The first optional if it's present, or the second, or the third etc. using anIterable
.static <T> Optional
<T> LikeorFlat(Iterable)
but allowsOptional
s to be specified as variable-arguments.static <T> Optional
<T> The first optional if it's present, or the second, or the third etc. using anStream
.orFlatSupplier
(Iterable<CheckedSupplier<Optional<T>, E>> optionals) The first optional if it's present, or the second, or the third etc. using an iterable and aCheckedSupplier
.orFlatSupplier
(CheckedSupplier<Optional<T>, E>... optional) LikeorFlatSupplier(Iterable)
but allowsOptional
s to be specified as variable-arguments.
-
Method Details
-
ifPresent
public static <S,E extends Exception> void ifPresent(Optional<S> optional, CheckedConsumer<S, E> consumerFunction) throws ELikeOptional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is immediately thrown.- Type Parameters:
S
- optional-typeE
- exception that may be thrown during mapping.- Parameters:
optional
- incoming optional.consumerFunction
- the function that is called if an optional contains a value.- Throws:
E
- an exception ifconsumerFunction
throws it.
-
map
public static <S,T, Optional<T> mapE extends Exception> (Optional<S> optional, CheckedFunction<S, T, throws EE> mapFunction) LikeOptional.map(java.util.function.Function<? super T, ? extends U>)
but tolerates an exception in the mapping function, which is immediately thrown.- Type Parameters:
S
- incoming optional-type for mapT
- outgoing optional-type for mapE
- exception that may be thrown during mapping- Parameters:
optional
- incoming optional.mapFunction
- the function that does the mapping from incoming to outgoing.- Returns:
- the outgoing "mapped" optional.
- Throws:
E
- an exception if the mapping function throws it.
-
orElseGet
public static <T,E extends Exception> T orElseGet(Optional<T> optional, CheckedSupplier<T, E> supplier) throws ELikeOptional.orElseGet(java.util.function.Supplier<? extends T>)
but tolerates an exception in the supplier function, which is immediately thrown.- Type Parameters:
T
- optional-typeE
- exception that may be thrown during mapping- Parameters:
optional
- incoming optional.supplier
- supplies a value if the optional is empty.- Returns:
- the outgoing optional.
- Throws:
E
- an exception if the supplier throws it.
-
orElseGetFlat
@SafeVarargs public static <T,E extends Exception> Optional<T> orElseGetFlat(Optional<T> optional, CheckedSupplier<Optional<T>, E>... suppliers) throws ELikeorElseGet(java.util.Optional<T>, org.anchoranalysis.core.functional.checked.CheckedSupplier<T, E>)
but returns aOptional
and can chain many alternatives.- Type Parameters:
T
- optional-typeE
- exception that may be thrown during mapping- Parameters:
optional
- incoming optionalsuppliers
- tries each alternativeOptional
value successively (if optional is empty) until one is not-empty- Returns:
- the outgoing optional
- Throws:
E
- an exception if the supplier throws it
-
flatMap
public static <S,T, Optional<T> flatMapE extends Exception> (Optional<S> optional, CheckedFunction<S, Optional<T>, throws EE> mapFunction) LikeOptional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>)
but tolerates an exception in the mapping function, which is immediately thrown.- Type Parameters:
S
- incoming optional-type for mapT
- outgoing optional-type for mapE
- exception that may be thrown during mapping- Parameters:
optional
- incoming optional.mapFunction
- the function that does the mapping from incoming to outgoing.- Returns:
- the outgoing "mapped" optional.
- Throws:
E
- an exception if the mapping function throws it.
-
mapBoth
public static <T,U, Optional<T> mapBothV, E extends Exception> (Optional<U> optional1, Optional<V> optional2, CheckedBiFunction<U, V, throws ET, E> mapFunction) Mapping only occurs if bothOptional
s are non-empty (equivalent to a logical and on the optionals)- Type Parameters:
T
- outgoing optional-type for mapU
- first incoming optional-type for mapV
- second incoming optional-type for mapE
- an exception that may be thrown by anmapFunction
.- Parameters:
optional1
- first incoming optional.optional2
- second incoming optional.mapFunction
- the function that does the mapping from both incoming objects to outgoing.- Returns:
- the outgoing "mapped" optional (empty() if either incoming optional is empty).
- Throws:
E
- ifmapFunction
throws it.
-
orFlat
The first optional if it's present, or the second, or the third etc. using anIterable
.- Type Parameters:
T
- type of optionals.- Parameters:
optionals
- one or more optionals to combine together using a logical or operation.- Returns:
- a new optional that is
optionals[0]
ORoptionals[1]
ORoptionals[2]
etc.
-
orFlat
The first optional if it's present, or the second, or the third etc. using anStream
.- Type Parameters:
T
- type of optionals.- Parameters:
optionals
- one or more optionals to combine together using a logical or operation.- Returns:
- a new optional that is
optionals[0]
ORoptionals[1]
ORoptionals[2]
etc.
-
orFlatSupplier
public static <T,E extends Exception> Optional<T> orFlatSupplier(Iterable<CheckedSupplier<Optional<T>, E>> optionals) throws EThe first optional if it's present, or the second, or the third etc. using an iterable and aCheckedSupplier
.- Type Parameters:
T
- type of optionals.E
- an exception that may be thrown by anoptional
.- Parameters:
optionals
- one or more optionals to combine together using a logical or operation. * @return a new optional that isoptionals[0]
ORoptionals[1]
ORoptionals[2]
etc.- Returns:
- a new optional that is
optionals[0]
ORoptionals[1]
ORoptionals[2]
etc. - Throws:
E
- if anyoptional
throws it.
-
orFlat
LikeorFlat(Iterable)
but allowsOptional
s to be specified as variable-arguments.- Type Parameters:
T
- type of optionals.- Parameters:
optional
- one or more optionals to combine together using a logical or operation.- Returns:
- a new optional that is
optionals[0]
ORoptionals[1]
ORoptionals[2]
etc.
-
orFlatSupplier
@SafeVarargs public static <T,E extends Exception> Optional<T> orFlatSupplier(CheckedSupplier<Optional<T>, E>... optional) throws ELikeorFlatSupplier(Iterable)
but allowsOptional
s to be specified as variable-arguments.- Type Parameters:
T
- type of optionals.E
- an exception that may be thrown by anoptional
.- Parameters:
optional
- one or more optionals to combine together using a logical or operation.- Returns:
- a new optional that is
optionals[0]
ORoptionals[1]
ORoptionals[2]
etc. - Throws:
E
- if anyoptional
throws it.
-