uk.org.webcompere.lightweightconfig.streams.Coalesce Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lightweight-config Show documentation
Show all versions of lightweight-config Show documentation
A small library for enabling configuration parameters to be loaded in Java
package uk.org.webcompere.lightweightconfig.streams;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Select the first item from a stream of optional suppliers
*/
public class Coalesce {
/**
* Get the first option which is supplied as non empty
* @param options the list of options
* @param the type of return
* @return the first non empty or {@link Optional#empty()}
*/
@SafeVarargs
public static Optional getFirstNonEmpty(Supplier>... options) {
return Stream.of(options)
.map(Supplier::get)
.filter(Optional::isPresent)
.findFirst()
.flatMap(Function.identity());
}
}