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

com.slimgears.util.stream.Optionals Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
package com.slimgears.util.stream;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class Optionals {
    public static  Function> ofType(Class type) {
        return val -> Optional
                .ofNullable(val)
                .filter(type::isInstance)
                .map(type::cast);
    }

    @SafeVarargs
    public static  Optional or(Supplier>... variants) {
        return Stream.of(variants)
                .map(Supplier::get)
                .filter(Optional::isPresent)
                .findFirst()
                .orElse(Optional.empty());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy