Package org.linkki.util
Class Optionals
- java.lang.Object
-
- org.linkki.util.Optionals
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classOptionals.Either<T>Wrapper around anOptionalthat offers Java 9's Optional#or method.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Optionals.Either<T>either(Optional<T> o)In anticipation of Java 9 Optional#or.static <T> voidifPresentOrElse(Optional<T> optional, Consumer<? super T> action, Runnable emptyAction)In anticipation of Java 9 Optional#ifPresentOrElse.static <T> Stream<T>stream(Optional<T> o)In anticipation of Java 9 Optional#stream.
-
-
-
Method Detail
-
ifPresentOrElse
public static <T> void ifPresentOrElse(Optional<T> optional, Consumer<? super T> action, Runnable emptyAction)
In anticipation of Java 9 Optional#ifPresentOrElse.If a value is present in the given optional, performs the given action with the value, otherwise performs the given empty-based action.
- Parameters:
optional- anOptionalaction- the action to be performed, if a value is presentemptyAction- the empty-based action to be performed, if no value is present- Throws:
NullPointerException- if a value is present and the given action is null, or no value is present and the given empty-based action is null.
-
stream
public static <T> Stream<T> stream(Optional<T> o)
In anticipation of Java 9 Optional#stream.If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
API Note: This method can be used to transform a Stream of optional elements to a Stream of present value elements:
Stream<Optional<T>> os = .. Stream<T> s = os.flatMap(Optional::stream)
- Returns:
- the optional value as a Stream
-
either
public static <T> Optionals.Either<T> either(Optional<T> o)
In anticipation of Java 9 Optional#or. Creates anOptionals.Eitherwrapper around the givenOptionalon whichOptionals.Either.or(Supplier)can be called.
-
-