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

org.mapfish.print.OptionalUtils Maven / Gradle / Ivy

There is a newer version: 3.22.0
Show newest version
package org.mapfish.print;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * Utilities for Java's Optional class.
 */
public final class OptionalUtils {
    private OptionalUtils() {
    }

    /**
     * Return the first optional to be defined.
     *
     * @param a The first
     * @param b The second
     * @param  The type
     * @return a or b or empty
     */
    @SuppressWarnings("unchecked")
    public static  Optional or(final Optional a, final Optional b) {
        return a.isPresent() ? a : (Optional) b;
    }

    /**
     * Return the first optional to be defined.
     *
     * @param optionals the lambdas returning the optionals
     * @param  The type
     * @return the first non-empty or empty
     */
    @SuppressWarnings("unchecked")
    @SafeVarargs
    public static  Optional or(final Supplier>... optionals) {
        return (Optional) Arrays.stream(optionals)
                .map(Supplier::get)
                .filter(Optional::isPresent)
                .findFirst()
                .orElseGet(Optional::empty);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy