All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.org.webcompere.lightweightconfig.streams.Coalesce Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
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());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy