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.Predicate; 22 23 /** 24 * A functional interface like {@link Predicate} that declares a {@link Throwable}. 25 * 26 * @param <T> Predicate type. 27 * @param <E> The kind of thrown exception or error. 28 * @since 3.11 29 */ 30 @FunctionalInterface 31 public interface FailablePredicate<T, E extends Throwable> { 32 33 /** FALSE singleton */ 34 @SuppressWarnings("rawtypes") 35 FailablePredicate FALSE = t -> false; 36 37 /** TRUE singleton */ 38 @SuppressWarnings("rawtypes") 39 FailablePredicate TRUE = t -> true; 40 41 /** 42 * Returns The FALSE singleton. 43 * 44 * @param <T> Predicate type. 45 * @param <E> The kind of thrown exception or error. 46 * @return The NOP singleton. 47 */ 48 @SuppressWarnings("unchecked") 49 static <T, E extends Throwable> FailablePredicate<T, E> falsePredicate() { 50 return FALSE; 51 } 52 53 /** 54 * Returns The TRUE singleton. 55 * 56 * @param <T> Predicate type. 57 * @param <E> The kind of thrown exception or error. 58 * @return The NOP singleton. 59 */ 60 @SuppressWarnings("unchecked") 61 static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() { 62 return TRUE; 63 } 64 65 /** 66 * Returns a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}. 67 * 68 * @param other a predicate that will be logically-ANDed with this predicate. 69 * @return a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}. 70 * @throws NullPointerException if other is null 71 */ 72 default FailablePredicate<T, E> and(final FailablePredicate<? super T, E> other) { 73 Objects.requireNonNull(other); 74 return t -> test(t) && other.test(t); 75 } 76 77 /** 78 * Returns a predicate that negates this predicate. 79 * 80 * @return a predicate that negates this predicate. 81 */ 82 default FailablePredicate<T, E> negate() { 83 return t -> !test(t); 84 } 85 86 /** 87 * Returns a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}. 88 * 89 * @param other a predicate that will be logically-ORed with this predicate. 90 * @return a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}. 91 * @throws NullPointerException if other is null 92 */ 93 default FailablePredicate<T, E> or(final FailablePredicate<? super T, E> other) { 94 Objects.requireNonNull(other); 95 return t -> test(t) || other.test(t); 96 } 97 98 /** 99 * Tests the predicate. 100 * 101 * @param object the object to test the predicate on 102 * @return the predicate's evaluation 103 * @throws E if the predicate fails 104 */ 105 boolean test(T object) throws E; 106 }