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.io.function;
019
020import java.io.IOException;
021import java.io.UncheckedIOException;
022import java.util.Objects;
023import java.util.function.Predicate;
024
025/**
026 * Like {@link Predicate} but throws {@link IOException}.
027 *
028 * @param <T> the type of the input to the predicate
029 * @since 2.12.0
030 */
031@FunctionalInterface
032public interface IOPredicate<T> {
033
034    /**
035     * Always false.
036     *
037     * @param <T> the type of the input to the predicate
038     * @return a constant predicate that tests always false.
039     */
040    @SuppressWarnings("unchecked")
041    static <T> IOPredicate<T> alwaysFalse() {
042        return (IOPredicate<T>) Constants.IO_PREDICATE_FALSE;
043    }
044
045    /**
046     * Always true.
047     *
048     * @param <T> the type of the input to the predicate
049     * @return a constant predicate that tests always true.
050     */
051    @SuppressWarnings("unchecked")
052    static <T> IOPredicate<T> alwaysTrue() {
053        return (IOPredicate<T>) Constants.IO_PREDICATE_TRUE;
054    }
055
056    /**
057     * Creates a predicate that tests if two arguments are equal using {@link Objects#equals(Object, Object)}.
058     *
059     * @param <T> the type of arguments to the predicate
060     * @param target the object to compare for equality, may be {@code null}
061     * @return a predicate that tests if two arguments are equal using {@link Objects#equals(Object, Object)}
062     */
063    static <T> IOPredicate<T> isEqual(final Object target) {
064        return null == target ? Objects::isNull : object -> target.equals(object);
065    }
066
067    /**
068     * Creates a composed predicate that represents a short-circuiting logical AND of this predicate and another. When
069     * evaluating the composed predicate, if this predicate is {@code false}, then the {@code other} predicate is not
070     * evaluated.
071     *
072     * <p>
073     * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this
074     * predicate throws an exception, the {@code other} predicate will not be evaluated.
075     * </p>
076     *
077     * @param other a predicate that will be logically-ANDed with this predicate
078     * @return a composed predicate that represents the short-circuiting logical AND of this predicate and the {@code other}
079     *         predicate
080     * @throws NullPointerException if other is null
081     */
082    default IOPredicate<T> and(final IOPredicate<? super T> other) {
083        Objects.requireNonNull(other);
084        return t -> test(t) && other.test(t);
085    }
086
087    /**
088     * Creates a {@link Predicate} for this instance that throws {@link UncheckedIOException} instead of
089     * {@link IOException}.
090     *
091     * @return an UncheckedIOException Predicate.
092     */
093    default Predicate<T> asPredicate() {
094        return t -> Uncheck.test(this, t);
095    }
096
097    /**
098     * Creates a predicate that represents the logical negation of this predicate.
099     *
100     * @return a predicate that represents the logical negation of this predicate
101     */
102    default IOPredicate<T> negate() {
103        return t -> !test(t);
104    }
105
106    /**
107     * Creates a composed predicate that represents a short-circuiting logical OR of this predicate and another. When
108     * evaluating the composed predicate, if this predicate is {@code true}, then the {@code other} predicate is not
109     * evaluated.
110     *
111     * <p>
112     * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this
113     * predicate throws an exception, the {@code other} predicate will not be evaluated.
114     * </p>
115     *
116     * @param other a predicate that will be logically-ORed with this predicate
117     * @return a composed predicate that represents the short-circuiting logical OR of this predicate and the {@code other}
118     *         predicate
119     * @throws NullPointerException if other is null
120     */
121    default IOPredicate<T> or(final IOPredicate<? super T> other) {
122        Objects.requireNonNull(other);
123        return t -> test(t) || other.test(t);
124    }
125
126    /**
127     * Evaluates this predicate on the given argument.
128     *
129     * @param t the input argument
130     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}
131     * @throws IOException if an I/O error occurs.
132     */
133    boolean test(T t) throws IOException;
134
135}