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