    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 {@link Throwable}.
    025 *
    026 * @param <T> Predicate type.
    027 * @param <E> The kind of thrown exception or error.
    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> The kind of thrown exception or error.
    046     * @return The NOP singleton.
    047     */
    048    @SuppressWarnings("unchecked")
    049    static <T, E extends Throwable> FailablePredicate<T, E> falsePredicate() {
    050        return FALSE;
    051    }
    052
    053    /**
    054     * Returns The TRUE singleton.
    055     *
    056     * @param <T> Predicate type.
    057     * @param <E> The kind of thrown exception or error.
    058     * @return The NOP singleton.
    059     */
    060    @SuppressWarnings("unchecked")
    061    static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() {
    062        return TRUE;
    063    }
    064
    065    /**
    066     * Returns a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}.
    067     *
    068     * @param other a predicate that will be logically-ANDed with this predicate.
    069     * @return a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}.
    070     * @throws NullPointerException if other is null
    071     */
    072    default FailablePredicate<T, E> and(final FailablePredicate<? super T, E> other) {
    073        Objects.requireNonNull(other);
    074        return t -> test(t) && other.test(t);
    075    }
    076
    077    /**
    078     * Returns a predicate that negates this predicate.
    079     *
    080     * @return a predicate that negates this predicate.
    081     */
    082    default FailablePredicate<T, E> negate() {
    083        return t -> !test(t);
    084    }
    085
    086    /**
    087     * Returns a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}.
    088     *
    089     * @param other a predicate that will be logically-ORed with this predicate.
    090     * @return a composed {@link FailablePredicate} like {@link Predicate#and(Predicate)}.
    091     * @throws NullPointerException if other is null
    092     */
    093    default FailablePredicate<T, E> or(final FailablePredicate<? super T, E> other) {
    094        Objects.requireNonNull(other);
    095        return t -> test(t) || other.test(t);
    096    }
    097
    098    /**
    099     * 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}