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 {@code Throwable}.
025 *
026 * @param <E> Thrown exception.
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> Thrown exception.
044     * @return The NOP singleton.
045     */
046    static <E extends Throwable> FailableLongPredicate<E> falsePredicate() {
047        return FALSE;
048    }
049
050    /**
051     * Returns The FALSE TRUE.
052     *
053     * @param <E> Thrown exception.
054     * @return The NOP singleton.
055     */
056    static <E extends Throwable> FailableLongPredicate<E> truePredicate() {
057        return TRUE;
058    }
059
060    /**
061     * Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
062     *
063     * @param other a predicate that will be logically-ANDed with this predicate.
064     * @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
065     * @throws NullPointerException if other is null
066     */
067    default FailableLongPredicate<E> and(final FailableLongPredicate<E> other) {
068        Objects.requireNonNull(other);
069        return t -> test(t) && other.test(t);
070    }
071
072    /**
073     * Returns a predicate that negates this predicate.
074     *
075     * @return a predicate that negates this predicate.
076     */
077    default FailableLongPredicate<E> negate() {
078        return t -> !test(t);
079    }
080
081    /**
082     * Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
083     *
084     * @param other a predicate that will be logically-ORed with this predicate.
085     * @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
086     * @throws NullPointerException if other is null
087     */
088    default FailableLongPredicate<E> or(final FailableLongPredicate<E> other) {
089        Objects.requireNonNull(other);
090        return t -> test(t) || other.test(t);
091    }
092
093    /**
094     * Tests the predicate.
095     *
096     * @param value the parameter for the predicate to accept.
097     * @return {@code true} if the input argument matches the predicate, {@code false} otherwise.
098     * @throws E Thrown when the consumer fails.
099     */
100    boolean test(long value) throws E;
101}