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

com.github.leeonky.util.function.Extension Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.util.function;

import java.util.Objects;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Optional.empty;
import static java.util.stream.Stream.of;

public class Extension {

    @SuppressWarnings("unchecked")
    public static  Predicate not(Predicate t) {
        Objects.requireNonNull(t);
        return (Predicate) t.negate();
    }

    @SafeVarargs
    @SuppressWarnings("unchecked")
    public static  Optional getFirstPresent(Supplier>... optionals) {
        return (Optional) of(optionals).map(Supplier::get).filter(Optional::isPresent).findFirst().orElse(empty());
    }

    @SafeVarargs
    @SuppressWarnings("unchecked")
    public static  Optional firstPresent(Optional... optionals) {
        return (Optional) of(optionals).filter(Optional::isPresent).findFirst().orElse(empty());
    }

    public static  BinaryOperator notAllowParallelReduce() {
        return (o1, o2) -> {
            throw new IllegalStateException("Not allow parallel here!");
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy