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 *      http://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 {@code Throwable}.
025 *
026 * @param <T> Predicate type 1.
027 * @param <U> Predicate type 2.
028 * @param <E> Thrown exception.
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     * Returns The FALSE singleton.
044     *
045     * @param <T> Consumed type 1.
046     * @param <U> Consumed type 2.
047     * @param <E> Thrown exception.
048     * @return The NOP singleton.
049     */
050    static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> falsePredicate() {
051        return FALSE;
052    }
053
054    /**
055     * Returns The FALSE TRUE.
056     *
057     * @param <T> Consumed type 1.
058     * @param <U> Consumed type 2.
059     * @param <E> Thrown exception.
060     * @return The NOP singleton.
061     */
062    static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate() {
063        return TRUE;
064    }
065
066    /**
067     * Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
068     *
069     * @param other a predicate that will be logically-ANDed with this predicate.
070     * @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
071     * @throws NullPointerException if other is null
072     */
073    default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) {
074        Objects.requireNonNull(other);
075        return (final T t, final U u) -> test(t, u) && other.test(t, u);
076    }
077
078    /**
079     * Returns a predicate that negates this predicate.
080     *
081     * @return a predicate that negates this predicate.
082     */
083    default FailableBiPredicate<T, U, E> negate() {
084        return (final T t, final U u) -> !test(t, u);
085    }
086
087    /**
088     * Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
089     *
090     * @param other a predicate that will be logically-ORed with this predicate.
091     * @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
092     * @throws NullPointerException if other is null
093     */
094    default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) {
095        Objects.requireNonNull(other);
096        return (final T t, final U u) -> test(t, u) || other.test(t, u);
097    }
098
099    /**
100     * Tests the predicate.
101     *
102     * @param object1 the first object to test the predicate on
103     * @param object2 the second object to test the predicate on
104     * @return the predicate's evaluation
105     * @throws E Thrown when this predicate fails.
106     */
107    boolean test(T object1, U object2) throws E;
108}