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/28 11:57:30 UTC

[commons-io] 05/05: Add IORunnable#asRunnable()

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 73a3f2d08701ba8d222ff2f9e023428c46457311
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 28 07:57:17 2022 -0400

    Add IORunnable#asRunnable()
---
 .../java/org/apache/commons/io/function/IORunnable.java     | 10 ++++++++++
 .../java/org/apache/commons/io/function/IORunnableTest.java | 13 +++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/src/main/java/org/apache/commons/io/function/IORunnable.java b/src/main/java/org/apache/commons/io/function/IORunnable.java
index 01403ef7..8afa2979 100644
--- a/src/main/java/org/apache/commons/io/function/IORunnable.java
+++ b/src/main/java/org/apache/commons/io/function/IORunnable.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.function;
 
 import java.io.IOException;
+import java.io.UncheckedIOException;
 
 /**
  * Like {@link Runnable} but throws {@link IOException}.
@@ -27,6 +28,15 @@ import java.io.IOException;
 @FunctionalInterface
 public interface IORunnable {
 
+    /**
+     * Creates a {@link Runnable} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
+     *
+     * @return an unchecked Predicate.
+     */
+    default Runnable asRunnable() {
+        return () -> Uncheck.run(this);
+    }
+
     /**
      * Like {@link Runnable#run()} but throws {@link IOException}.
      *
diff --git a/src/test/java/org/apache/commons/io/function/IORunnableTest.java b/src/test/java/org/apache/commons/io/function/IORunnableTest.java
index 2a77d6ea..4788f6ee 100644
--- a/src/test/java/org/apache/commons/io/function/IORunnableTest.java
+++ b/src/test/java/org/apache/commons/io/function/IORunnableTest.java
@@ -18,10 +18,16 @@
 package org.apache.commons.io.function;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.apache.commons.io.file.PathUtils;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -42,4 +48,11 @@ public class IORunnableTest {
         assertEquals("A1", ref.get());
     }
 
+    @Test
+    public void testAsRunnable() throws Exception {
+        assertThrows(UncheckedIOException.class, () -> Executors.callable(TestConstants.THROWING_IO_RUNNABLE.asRunnable()).call());
+        final IORunnable runnable = () -> Files.size(PathUtils.current());
+        assertNull(Executors.callable(runnable.asRunnable()).call());
+    }
+
 }