public final class Optionals extends Object
| Modifier and Type | Method and Description |
|---|---|
static <T> Optional<T> |
filter(Optional<?> opt,
Class<T> cls)
Convenience method to filter an optional based on its class and cast it at the same time
|
static <T> Optional<T> |
firstOf(Supplier<Optional<T>>... suppliers)
Out of a list of suppliers of optional values, returns the first one that is present without unnecessary evaluating
the next suppliers.
|
static Optional<String> |
ofBlankable(String blankable)
Convenience method to build and Optional out of a String that can be null or blank
|
static <T> Stream<T> |
stream(Optional<T> optional)
Convenience method to turn an Optional into a Stream, due to the lack Optional#stream() method...
|
public static <T> Optional<T> firstOf(Supplier<Optional<T>>... suppliers)
Optional first = Optionals.firstOf(() -> firstCostyOperation()
() -> secondCostlyOperation()
() -> thirdCostlyOperation());
In case firstCostyOperation() returns an Optional.empty(), then secondCostlyOperation() will be invoked. If secondCostlyOperation()
returns a present Optional, then thirdCostlyOperation() won't be invoked.T - tsuppliers - for methods that need to be conditionnally invokedpublic static <T> Stream<T> stream(Optional<T> optional)
List<Optional<String>> listOfOptionals = //...
List<String> strings = list.stream().flatMap(Optionals::stream).collect(toList());
T - toptional - optionalpublic static <T> Optional<T> filter(Optional<?> opt, Class<T> cls)
T - Topt - optcls - clsCopyright © 2014–2016. All rights reserved.