    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.LongPredicate;
    022
    023/**
    024 * A functional interface like {@link LongPredicate} 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 FailableLongPredicate<E extends Throwable> {
    031
    032    /** FALSE singleton */
    033    @SuppressWarnings("rawtypes")
    034    FailableLongPredicate FALSE = t -> false;
    035
    036    /** TRUE singleton */
    037    @SuppressWarnings("rawtypes")
    038    FailableLongPredicate TRUE = t -> true;
    039
    040    /**
    041     * Returns 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> FailableLongPredicate<E> falsePredicate() {
    048        return FALSE;
    049    }
    050
    051    /**
    052     * Returns 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> FailableLongPredicate<E> truePredicate() {
    059        return TRUE;
    060    }
    061
    062    /**
    063     * Returns a composed {@link FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
    064     *
    065     * @param other a predicate that will be logically-ANDed with this predicate.
    066     * @return a composed {@link FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
    067     * @throws NullPointerException if other is null
    068     */
    069    default FailableLongPredicate<E> and(final FailableLongPredicate<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 FailableLongPredicate<E> negate() {
    080        return t -> !test(t);
    081    }
    082
    083    /**
    084     * Returns a composed {@link FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
    085     *
    086     * @param other a predicate that will be logically-ORed with this predicate.
    087     * @return a composed {@link FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
    088     * @throws NullPointerException if other is null
    089     */
    090    default FailableLongPredicate<E> or(final FailableLongPredicate<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(long value) throws E;
    103}