1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.lang3.function; 19 20 import java.util.Objects; 21 import java.util.function.BiPredicate; 22 23 /** 24 * A functional interface like {@link BiPredicate} that declares a {@link Throwable}. 25 * 26 * @param <T> Predicate type 1. 27 * @param <U> Predicate type 2. 28 * @param <E> The kind of thrown exception or error. 29 * @since 3.11 30 */ 31 @FunctionalInterface 32 public interface FailableBiPredicate<T, U, E extends Throwable> { 33 34 /** FALSE singleton */ 35 @SuppressWarnings("rawtypes") 36 FailableBiPredicate FALSE = (t, u) -> false; 37 38 /** TRUE singleton */ 39 @SuppressWarnings("rawtypes") 40 FailableBiPredicate TRUE = (t, u) -> true; 41 42 /** 43 * Returns The FALSE singleton. 44 * 45 * @param <T> Consumed type 1. 46 * @param <U> Consumed type 2. 47 * @param <E> The kind of thrown exception or error. 48 * @return The NOP singleton. 49 */ 50 @SuppressWarnings("unchecked") 51 static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> falsePredicate() { 52 return FALSE; 53 } 54 55 /** 56 * Returns The TRUE singleton. 57 * 58 * @param <T> Consumed type 1. 59 * @param <U> Consumed type 2. 60 * @param <E> The kind of thrown exception or error. 61 * @return The NOP singleton. 62 */ 63 @SuppressWarnings("unchecked") 64 static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate() { 65 return TRUE; 66 } 67 68 /** 69 * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}. 70 * 71 * @param other a predicate that will be logically-ANDed with this predicate. 72 * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}. 73 * @throws NullPointerException if other is null 74 */ 75 default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) { 76 Objects.requireNonNull(other); 77 return (final T t, final U u) -> test(t, u) && other.test(t, u); 78 } 79 80 /** 81 * Returns a predicate that negates this predicate. 82 * 83 * @return a predicate that negates this predicate. 84 */ 85 default FailableBiPredicate<T, U, E> negate() { 86 return (final T t, final U u) -> !test(t, u); 87 } 88 89 /** 90 * Returns a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}. 91 * 92 * @param other a predicate that will be logically-ORed with this predicate. 93 * @return a composed {@link FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}. 94 * @throws NullPointerException if other is null 95 */ 96 default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) { 97 Objects.requireNonNull(other); 98 return (final T t, final U u) -> test(t, u) || other.test(t, u); 99 } 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 }