IOPredicate.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.function;

  18. import java.io.IOException;
  19. import java.io.UncheckedIOException;
  20. import java.util.Objects;
  21. import java.util.function.Predicate;

  22. /**
  23.  * Like {@link Predicate} but throws {@link IOException}.
  24.  *
  25.  * @param <T> the type of the input to the predicate
  26.  * @since 2.12.0
  27.  */
  28. @FunctionalInterface
  29. public interface IOPredicate<T> {

  30.     /**
  31.      * Always false.
  32.      *
  33.      * @param <T> the type of the input to the predicate
  34.      * @return a constant predicate that tests always false.
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     static <T> IOPredicate<T> alwaysFalse() {
  38.         return (IOPredicate<T>) Constants.IO_PREDICATE_FALSE;
  39.     }

  40.     /**
  41.      * Always true.
  42.      *
  43.      * @param <T> the type of the input to the predicate
  44.      * @return a constant predicate that tests always true.
  45.      */
  46.     @SuppressWarnings("unchecked")
  47.     static <T> IOPredicate<T> alwaysTrue() {
  48.         return (IOPredicate<T>) Constants.IO_PREDICATE_TRUE;
  49.     }

  50.     /**
  51.      * Creates a predicate that tests if two arguments are equal using {@link Objects#equals(Object, Object)}.
  52.      *
  53.      * @param <T> the type of arguments to the predicate
  54.      * @param target the object to compare for equality, may be {@code null}
  55.      * @return a predicate that tests if two arguments are equal using {@link Objects#equals(Object, Object)}
  56.      */
  57.     static <T> IOPredicate<T> isEqual(final Object target) {
  58.         return null == target ? Objects::isNull : object -> target.equals(object);
  59.     }

  60.     /**
  61.      * Creates a composed predicate that represents a short-circuiting logical AND of this predicate and another. When
  62.      * evaluating the composed predicate, if this predicate is {@code false}, then the {@code other} predicate is not
  63.      * evaluated.
  64.      *
  65.      * <p>
  66.      * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this
  67.      * predicate throws an exception, the {@code other} predicate will not be evaluated.
  68.      * </p>
  69.      *
  70.      * @param other a predicate that will be logically-ANDed with this predicate
  71.      * @return a composed predicate that represents the short-circuiting logical AND of this predicate and the {@code other}
  72.      *         predicate
  73.      * @throws NullPointerException if other is null
  74.      */
  75.     default IOPredicate<T> and(final IOPredicate<? super T> other) {
  76.         Objects.requireNonNull(other);
  77.         return t -> test(t) && other.test(t);
  78.     }

  79.     /**
  80.      * Creates a {@link Predicate} for this instance that throws {@link UncheckedIOException} instead of
  81.      * {@link IOException}.
  82.      *
  83.      * @return an UncheckedIOException Predicate.
  84.      */
  85.     default Predicate<T> asPredicate() {
  86.         return t -> Uncheck.test(this, t);
  87.     }

  88.     /**
  89.      * Creates a predicate that represents the logical negation of this predicate.
  90.      *
  91.      * @return a predicate that represents the logical negation of this predicate
  92.      */
  93.     default IOPredicate<T> negate() {
  94.         return t -> !test(t);
  95.     }

  96.     /**
  97.      * Creates a composed predicate that represents a short-circuiting logical OR of this predicate and another. When
  98.      * evaluating the composed predicate, if this predicate is {@code true}, then the {@code other} predicate is not
  99.      * evaluated.
  100.      *
  101.      * <p>
  102.      * Any exceptions thrown during evaluation of either predicate are relayed to the caller; if evaluation of this
  103.      * predicate throws an exception, the {@code other} predicate will not be evaluated.
  104.      * </p>
  105.      *
  106.      * @param other a predicate that will be logically-ORed with this predicate
  107.      * @return a composed predicate that represents the short-circuiting logical OR of this predicate and the {@code other}
  108.      *         predicate
  109.      * @throws NullPointerException if other is null
  110.      */
  111.     default IOPredicate<T> or(final IOPredicate<? super T> other) {
  112.         Objects.requireNonNull(other);
  113.         return t -> test(t) || other.test(t);
  114.     }

  115.     /**
  116.      * Evaluates this predicate on the given argument.
  117.      *
  118.      * @param t the input argument
  119.      * @return {@code true} if the input argument matches the predicate, otherwise {@code false}
  120.      * @throws IOException if an I/O error occurs.
  121.      */
  122.     boolean test(T t) throws IOException;

  123. }