Package uk.ac.manchester.spinnaker.utils
Class OptionalUtils
java.lang.Object
uk.ac.manchester.spinnaker.utils.OptionalUtils
public abstract class OptionalUtils extends Object
Extra utilities for working with
Optional
in Java 8.- Author:
- Donal Fellows
-
Method Summary
Modifier and Type Method Description static <T> Optional<T>
apply(Optional<T> source, Consumer<T>... actions)
How to apply a sequence of modification transforms to a value in an optional if there is a value there at all.static <T, U> U
ifElse(Optional<T> source, Function<T,U> ifPresent, Supplier<U> ifAbsent)
How to do one thing if an optional is present, and another if it is absent.static <T, U> Optional<U>
ifElseFlat(Optional<T> source, Function<T,U> ifPresent, Supplier<U> ifAbsent)
How to do one thing if an optional is present, and another if it is absent.
-
Method Details
-
apply
How to apply a sequence of modification transforms to a value in an optional if there is a value there at all.- Type Parameters:
T
- The type of value in the optional.- Parameters:
source
- Where to get the value from.actions
- The transformations to apply to the value in the optional if it exists; these are expected to modify that object.- Returns:
- The original optional.
-
ifElse
How to do one thing if an optional is present, and another if it is absent.- Type Parameters:
T
- The type of value in the optional.U
- The type of the result.- Parameters:
source
- Where to get the value from.ifPresent
- What to do if the value is available.ifAbsent
- What to do if the value is not available.- Returns:
- The result, depending on which arm was taken.
-
ifElseFlat
public static <T, U> Optional<U> ifElseFlat(Optional<T> source, Function<T,U> ifPresent, Supplier<U> ifAbsent)How to do one thing if an optional is present, and another if it is absent.- Type Parameters:
T
- The type of value in the optional.U
- The type of the result.- Parameters:
source
- Where to get the value from.ifPresent
- What to do if the value is available.ifAbsent
- What to do if the value is not available.- Returns:
- The result, depending on which arm was taken.
-