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

hm.binkley.util.function.ThrowingPredicate Maven / Gradle / Ivy

The newest version!
package hm.binkley.util.function;

import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * {@code ThrowingPredicate} is a throwing look-a=like of {@link Predicate}.  It cannot be
 * a {@code Predicate} as it takes throwing versions of predicates.  Otherwise it is a faithful
 * reproduction.
 *
 * @author B. K. Oxley (binkley)
 */
@SuppressWarnings({"UnusedDeclaration", "JavaDoc"})
@FunctionalInterface
public interface ThrowingPredicate {
    /** @see Predicate#test(Object) */
    boolean test(final T t)
            throws E, InterruptedException;

    /** @see Predicate#and(Predicate) */
    @Nonnull
    default ThrowingPredicate and(@Nonnull final ThrowingPredicate other) {
        return t -> test(t) && other.test(t);
    }

    /** @see Predicate#negate() */
    @Nonnull
    default ThrowingPredicate negate() {
        return t -> !test(t);
    }

    /** @see Predicate#or(Predicate) */
    @Nonnull
    default ThrowingPredicate or(@Nonnull final ThrowingPredicate other) {
        return t -> test(t) || other.test(t);
    }

    /** @see Predicate#isEqual(Object) */
    @Nonnull
    static  ThrowingPredicate isEqual(
            final Object targetRef) {
        return null == targetRef ? Objects::isNull : targetRef::equals;
    }

    /** Creates a facade {@code Predicate} wrapping this throwing one. */
    default  Predicate asPredicate(
            final Defer defer) {
        return t -> defer.as(() -> test(t));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy