    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3.function;
    019
    020import java.util.Objects;
    021import java.util.function.BiPredicate;
    022
    023/**
    024 * A functional interface like {@link BiPredicate} that declares a {@link Throwable}.
    025 *
    026 * @param <T> Predicate type 1.
    027 * @param <U> Predicate type 2.
    028 * @param <E> The kind of thrown exception or error.
    029 * @since 3.11
    030 */
    031@FunctionalInterface
    032public interface FailableBiPredicate<T, U, E extends Throwable> {
    033
    034    /** FALSE singleton */
    035    @SuppressWarnings("rawtypes")
    036    FailableBiPredicate FALSE = (t, u) -> false;
    037
    038    /** TRUE singleton */
    039    @SuppressWarnings("rawtypes")
    040    FailableBiPredicate TRUE = (t, u) -> true;
    041
    042    /**
    043     * Gets the FALSE singleton.
    044     *
    045     * @param <T> Consumed type 1.
    046     * @param <U> Consumed type 2.
    047     * @param <E> The kind of thrown exception or error.
    048     * @return The NOP singleton.
    049     */
    050    @SuppressWarnings("unchecked")
    051    static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> falsePredicate() {
    052        return FALSE;
    053    }
    054
    055    /**
    056     * Gets the TRUE singleton.
    057     *
    058     * @param <T> Consumed type 1.
    059     * @param <U> Consumed type 2.
    060     * @param <E> The kind of thrown exception or error.
    061     * @return The NOP singleton.
    062     */
    063    @SuppressWarnings("unchecked")
    064    static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate() {
    065        return TRUE;
    066    }
    067
    068    /**
    069     * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
    070     *
    071     * @param other a predicate that will be logically-ANDed with this predicate.
    072     * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
    073     * @throws NullPointerException if other is null
    074     */
    075    default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) {
    076        Objects.requireNonNull(other);
    077        return (final T t, final U u) -> test(t, u) && other.test(t, u);
    078    }
    079
    080    /**
    081     * Returns a predicate that negates this predicate.
    082     *
    083     * @return a predicate that negates this predicate.
    084     */
    085    default FailableBiPredicate<T, U, E> negate() {
    086        return (final T t, final U u) -> !test(t, u);
    087    }
    088
    089    /**
    090     * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
    091     *
    092     * @param other a predicate that will be logically-ORed with this predicate.
    093     * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
    094     * @throws NullPointerException if other is null
    095     */
    096    default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) {
    097        Objects.requireNonNull(other);
    098        return (final T t, final U u) -> test(t, u) || other.test(t, u);
    099    }
    100
    101    /**
    102     * Tests the predicate.
    103     *
    104     * @param object1 the first object to test the predicate on
    105     * @param object2 the second object to test the predicate on
    106     * @return the predicate's evaluation
    107     * @throws E Thrown when this predicate fails.
    108     */
    109    boolean test(T object1, U object2) throws E;
    110}