You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/07/24 16:38:36 UTC

[commons-io] 13/21: Add IOPredicate

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit b2415886d263d2ce514c53aefb97a7446b6f1740
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 24 11:14:11 2022 -0400

    Add IOPredicate
---
 .../java/org/apache/commons/io/UncheckedIO.java     | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/main/java/org/apache/commons/io/UncheckedIO.java b/src/main/java/org/apache/commons/io/UncheckedIO.java
index 9289d9e4..fd766484 100644
--- a/src/main/java/org/apache/commons/io/UncheckedIO.java
+++ b/src/main/java/org/apache/commons/io/UncheckedIO.java
@@ -23,6 +23,7 @@ import java.io.UncheckedIOException;
 import org.apache.commons.io.function.IOBiFunction;
 import org.apache.commons.io.function.IOConsumer;
 import org.apache.commons.io.function.IOFunction;
+import org.apache.commons.io.function.IOPredicate;
 import org.apache.commons.io.function.IOQuadFunction;
 import org.apache.commons.io.function.IORunnable;
 import org.apache.commons.io.function.IOSupplier;
@@ -169,6 +170,22 @@ public class UncheckedIO {
         }
     }
 
+    /**
+     * Tests an IO predicate
+     *
+     * @param <T> the type of the input to the predicate
+     * @param predicate the predicate
+     * @param t the input to the predicate
+     * @return {@code true} if the input argument matches the predicate, otherwise {@code false}
+     */
+    public static <T> boolean test(final IOPredicate<T> predicate, final T t) {
+        try {
+            return predicate.test(t);
+        } catch (final IOException e) {
+            throw wrap(e);
+        }
+    }
+
     /**
      * Creates a new UncheckedIOException for the given detail message.
      * <p>
@@ -181,4 +198,8 @@ public class UncheckedIO {
     private static UncheckedIOException wrap(final IOException e) {
         return new UncheckedIOException(e);
     }
+
+    private UncheckedIO() {
+        // no instances needed.
+    }
 }