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/27 21:10:10 UTC

[commons-io] branch master updated: Add IOFunction.asFunction()

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


The following commit(s) were added to refs/heads/master by this push:
     new e9815c81 Add IOFunction.asFunction()
e9815c81 is described below

commit e9815c819b831e8568c3eab2f3492f497609e484
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 27 17:10:03 2022 -0400

    Add IOFunction.asFunction()
---
 src/changes/changes.xml                                      |  3 +++
 src/main/java/org/apache/commons/io/function/IOFunction.java | 12 ++++++++++++
 .../java/org/apache/commons/io/function/IOFunctionTest.java  | 12 ++++++++++--
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 09fdebbb..971f52c2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -421,6 +421,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add IOSupplier.asSupplier().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add IOFunction.asFunction().
+      </action>
       <!-- UPDATE -->
       <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
         Bump actions/cache from 2.1.6 to 3.0.5 #307, #337.
diff --git a/src/main/java/org/apache/commons/io/function/IOFunction.java b/src/main/java/org/apache/commons/io/function/IOFunction.java
index 3a277ebf..c1c08026 100644
--- a/src/main/java/org/apache/commons/io/function/IOFunction.java
+++ b/src/main/java/org/apache/commons/io/function/IOFunction.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.function;
 
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.util.Objects;
 import java.util.function.Consumer;
 import java.util.function.Function;
@@ -128,6 +129,17 @@ public interface IOFunction<T, R> {
      */
     R apply(final T t) throws IOException;
 
+    /**
+     * Converts this instance to a {@link Function} that throws {@link UncheckedIOException} instead of
+     * {@link IOException}.
+     *
+     * @return an unchecked Function.
+     * @since 2.12.0
+     */
+    default Function<T, R> asFunction() {
+        return t -> Uncheck.apply(this, t);
+    }
+
     /**
      * Returns a composed {@link IOFunction} that first applies the {@code before}
      * function to its input, and then applies this function to the result.
diff --git a/src/test/java/org/apache/commons/io/function/IOFunctionTest.java b/src/test/java/org/apache/commons/io/function/IOFunctionTest.java
index 4c0a02ae..52106bb8 100644
--- a/src/test/java/org/apache/commons/io/function/IOFunctionTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOFunctionTest.java
@@ -23,6 +23,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Supplier;
@@ -96,7 +98,7 @@ public class IOFunctionTest {
     @Test
     public void testApply() throws IOException {
         final IOFunction<InputStream, Integer> readByte = InputStream::read;
-        final InputStream is = new ByteArrayInputStream(new byte[] { (byte)0xa, (byte)0xb, (byte)0xc});
+        final InputStream is = new ByteArrayInputStream(new byte[] {(byte) 0xa, (byte) 0xb, (byte) 0xc});
         assertEquals(0xa, readByte.apply(is));
         assertEquals(0xb, readByte.apply(is));
         assertEquals(0xc, readByte.apply(is));
@@ -111,6 +113,12 @@ public class IOFunctionTest {
         assertThrows(IOException.class, () -> throwException.apply(new ByteArrayInputStream(ArrayUtils.EMPTY_BYTE_ARRAY)));
     }
 
+    @Test
+    public void testAsFunction() throws IOException {
+        assertThrows(UncheckedIOException.class, () -> Optional.of("a").map(TestConstants.THROWING_IO_FUNCTION.asFunction()).get());
+        assertEquals("a", Optional.of("a").map(IOFunction.identity().asFunction()).get());
+    }
+
     @Test
     public void testComposeFunction() throws IOException {
         final Function<InputStream, Integer> alwaysSeven = is -> 7;
@@ -158,7 +166,7 @@ public class IOFunctionTest {
     @Test
     public void testIdentity() throws IOException {
         final IOFunction<InputStream, InputStream> identityFunction = IOFunction.identity();
-        try (InputStream is = new ByteArrayInputStream(new byte[] { (byte) 0xa, (byte) 0xb, (byte) 0xc })) {
+        try (InputStream is = new ByteArrayInputStream(new byte[] {(byte) 0xa, (byte) 0xb, (byte) 0xc})) {
             assertEquals(is, identityFunction.apply(is));
         }
     }