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.Predicate;
022
023/**
024 * A functional interface like {@link Predicate} that declares a {@code Throwable}.
025 *
026 * @param <T> Predicate type.
027 * @param <E> Thrown exception.
028 * @since 3.11
029 */
030@FunctionalInterface
031public interface FailablePredicate<T, E extends Throwable> {
032
033    /** FALSE singleton */
034    @SuppressWarnings("rawtypes")
035    FailablePredicate FALSE = t -> false;
036
037    /** TRUE singleton */
038    @SuppressWarnings("rawtypes")
039    FailablePredicate TRUE = t -> true;
040
041    /**
042     * Returns The FALSE singleton.
043     *
044     * @param <T> Predicate type.
045     * @param <E> Thrown exception.
046     * @return The NOP singleton.
047     */
048    static <T, E extends Throwable> FailablePredicate<T, E> falsePredicate() {
049        return FALSE;
050    }
051
052    /**
053     * Returns The FALSE TRUE.
054     *
055     * @param <T> Predicate type.
056     * @param <E> Thrown exception.
057     * @return The NOP singleton.
058     */
059    static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() {
060        return TRUE;
061    }
062
063    /**
064     * Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
065     *
066     * @param other a predicate that will be logically-ANDed with this predicate.
067     * @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
068     * @throws NullPointerException if other is null
069     */
070    default FailablePredicate<T, E> and(final FailablePredicate<? super T, E> other) {
071        Objects.requireNonNull(other);
072        return t -> test(t) && other.test(t);
073    }
074
075    /**
076     * Returns a predicate that negates this predicate.
077     *
078     * @return a predicate that negates this predicate.
079     */
080    default FailablePredicate<T, E> negate() {
081        return t -> !test(t);
082    }
083
084    /**
085     * Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
086     *
087     * @param other a predicate that will be logically-ORed with this predicate.
088     * @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
089     * @throws NullPointerException if other is null
090     */
091    default FailablePredicate<T, E> or(final FailablePredicate<? super T, E> other) {
092        Objects.requireNonNull(other);
093        return t -> test(t) || other.test(t);
094    }
095
096    /**
097     * Tests the predicate.
098     *
099     * @param object the object to test the predicate on
100     * @return the predicate's evaluation
101     * @throws E if the predicate fails
102     */
103    boolean test(T object) throws E;
104}