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:25 UTC

[commons-io] branch master updated (e9815c81 -> 73a3f2d0)

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

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


    from e9815c81 Add IOFunction.asFunction()
     new dda40c55 Add IOBiFunction#asBiFunction()
     new 35a30840 Add IOConsumer.asConsumer().
     new 28f639e7 Add IOBiConsumer#asBiConsumer()
     new c5e8b7b4 Javadoc
     new 73a3f2d0 Add IORunnable#asRunnable()

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  3 +
 .../apache/commons/io/function/IOBiConsumer.java   | 13 +++
 .../apache/commons/io/function/IOBiFunction.java   | 12 +++
 .../org/apache/commons/io/function/IOConsumer.java | 12 +++
 .../org/apache/commons/io/function/IOFunction.java | 95 +++++++++-------------
 .../apache/commons/io/function/IOPredicate.java    |  2 +-
 .../org/apache/commons/io/function/IORunnable.java | 10 +++
 .../org/apache/commons/io/function/IOSupplier.java |  2 +-
 .../commons/io/function/IOBiConsumerTest.java      | 15 ++++
 .../commons/io/function/IOBiFunctionTest.java      | 12 +++
 .../apache/commons/io/function/IOConsumerTest.java | 12 +++
 .../apache/commons/io/function/IORunnableTest.java | 13 +++
 12 files changed, 141 insertions(+), 60 deletions(-)


[commons-io] 02/05: Add IOConsumer.asConsumer().

Posted by gg...@apache.org.
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 35a3084010e15334219b176c62432d6623698a3e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 27 23:52:19 2022 -0400

    Add IOConsumer.asConsumer().
---
 src/changes/changes.xml                                     |  3 +++
 .../java/org/apache/commons/io/function/IOConsumer.java     | 13 +++++++++++++
 .../java/org/apache/commons/io/function/IOConsumerTest.java | 13 +++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 971f52c2..b234f13f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -424,6 +424,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add IOFunction.asFunction().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add IOConsumer.asConsumer().
+      </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/IOConsumer.java b/src/main/java/org/apache/commons/io/function/IOConsumer.java
index 724ab1c1..a7aef3da 100644
--- a/src/main/java/org/apache/commons/io/function/IOConsumer.java
+++ b/src/main/java/org/apache/commons/io/function/IOConsumer.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.stream.Stream;
@@ -153,4 +154,16 @@ public interface IOConsumer<T> {
             after.accept(t);
         };
     }
+
+    /**
+     * Converts this instance to a {@link Consumer} that throws {@link UncheckedIOException} instead of
+     * {@link IOException}.
+     *
+     * @return an unchecked Consumer.
+     * @since 2.12.0
+     */
+    default Consumer<T> asConsumer() {
+        return t -> Uncheck.accept(this, t);
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
index 67a8e6ce..2a2f2949 100644
--- a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
@@ -19,11 +19,14 @@ package org.apache.commons.io.function;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.StringReader;
+import java.io.UncheckedIOException;
 import java.nio.file.Files;
+import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.commons.io.IOUtils;
@@ -57,6 +60,16 @@ public class IOConsumerTest {
         assertEquals("B12B", ref.get());
     }
 
+    @Test
+    public void testAsConsumer() throws IOException {
+        assertThrows(UncheckedIOException.class, () -> Optional.of("a").ifPresent(TestConstants.THROWING_IO_CONSUMER.asConsumer()));
+        final AtomicReference<String> ref = new AtomicReference<>();
+        final IOConsumer<String> consumer1 = s -> ref.set(s + "1");
+        Optional.of("a").ifPresent(consumer1.asConsumer());
+        assertEquals("a1", ref.get());
+    }
+
+
     @Test
     public void testNoop() {
         final Closeable nullCloseable = null;


[commons-io] 04/05: Javadoc

Posted by gg...@apache.org.
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 c5e8b7b4f8fbd03e3e96098030409fc9edb56bde
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 28 00:23:19 2022 -0400

    Javadoc
---
 .../apache/commons/io/function/IOBiConsumer.java   |  2 +-
 .../apache/commons/io/function/IOBiFunction.java   |  2 +-
 .../org/apache/commons/io/function/IOConsumer.java |  3 +-
 .../org/apache/commons/io/function/IOFunction.java | 95 +++++++++-------------
 .../apache/commons/io/function/IOPredicate.java    |  2 +-
 .../org/apache/commons/io/function/IOSupplier.java |  2 +-
 6 files changed, 42 insertions(+), 64 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/function/IOBiConsumer.java b/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
index fbe25077..652b573a 100644
--- a/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
+++ b/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
@@ -73,7 +73,7 @@ public interface IOBiConsumer<T, U> {
     }
 
     /**
-     * Converts this instance to a {@link BiConsumer} that throws {@link UncheckedIOException} instead of
+     * Creates a {@link BiConsumer} for this instance that throws {@link UncheckedIOException} instead of
      * {@link IOException}.
      *
      * @return an unchecked BiConsumer.
diff --git a/src/main/java/org/apache/commons/io/function/IOBiFunction.java b/src/main/java/org/apache/commons/io/function/IOBiFunction.java
index 0d7ca5af..9a96909c 100644
--- a/src/main/java/org/apache/commons/io/function/IOBiFunction.java
+++ b/src/main/java/org/apache/commons/io/function/IOBiFunction.java
@@ -79,7 +79,7 @@ public interface IOBiFunction<T, U, R> {
     R apply(T t, U u) throws IOException;
 
     /**
-     * Converts this instance to a {@link BiFunction} that throws {@link UncheckedIOException} instead of
+     * Creates a {@link BiFunction} for this instance that throws {@link UncheckedIOException} instead of
      * {@link IOException}.
      *
      * @return an unchecked BiFunction.
diff --git a/src/main/java/org/apache/commons/io/function/IOConsumer.java b/src/main/java/org/apache/commons/io/function/IOConsumer.java
index a7aef3da..54db62b2 100644
--- a/src/main/java/org/apache/commons/io/function/IOConsumer.java
+++ b/src/main/java/org/apache/commons/io/function/IOConsumer.java
@@ -156,8 +156,7 @@ public interface IOConsumer<T> {
     }
 
     /**
-     * Converts this instance to a {@link Consumer} that throws {@link UncheckedIOException} instead of
-     * {@link IOException}.
+     * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
      *
      * @return an unchecked Consumer.
      * @since 2.12.0
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 c1c08026..ed9b2395 100644
--- a/src/main/java/org/apache/commons/io/function/IOFunction.java
+++ b/src/main/java/org/apache/commons/io/function/IOFunction.java
@@ -45,14 +45,12 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies this function to
-     * its input, and then applies the {@code after} consumer to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
+     * {@code after} consumer to the result. If evaluation of either function throws an exception, it is relayed to the
+     * caller of the composed function.
      *
      * @param after the consumer to apply after this function is applied
-     * @return a composed function that first applies this function and then
-     * applies the {@code after} consumer
+     * @return a composed function that first applies this function and then applies the {@code after} consumer
      * @throws NullPointerException if after is null
      *
      * @see #compose(IOFunction)
@@ -63,16 +61,13 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies this function to
-     * its input, and then applies the {@code after} function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
+     * {@code after} function to the result. If evaluation of either function throws an exception, it is relayed to the
+     * caller of the composed function.
      *
-     * @param <V> the type of output of the {@code after} function, and of the
-     *           composed function
+     * @param <V> the type of output of the {@code after} function, and of the composed function
      * @param after the function to apply after this function is applied
-     * @return a composed function that first applies this function and then
-     * applies the {@code after} function
+     * @return a composed function that first applies this function and then applies the {@code after} function
      * @throws NullPointerException if after is null
      *
      * @see #compose(IOFunction)
@@ -83,14 +78,12 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies this function to
-     * its input, and then applies the {@code after} consumer to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
+     * {@code after} consumer to the result. If evaluation of either function throws an exception, it is relayed to the
+     * caller of the composed function.
      *
      * @param after the consumer to apply after this function is applied
-     * @return a composed function that first applies this function and then
-     * applies the {@code after} consumer
+     * @return a composed function that first applies this function and then applies the {@code after} consumer
      * @throws NullPointerException if after is null
      *
      * @see #compose(IOFunction)
@@ -101,16 +94,13 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies this function to
-     * its input, and then applies the {@code after} function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies this function to its input, and then applies the
+     * {@code after} function to the result. If evaluation of either function throws an exception, it is relayed to the
+     * caller of the composed function.
      *
-     * @param <V> the type of output of the {@code after} function, and of the
-     *           composed function
+     * @param <V> the type of output of the {@code after} function, and of the composed function
      * @param after the function to apply after this function is applied
-     * @return a composed function that first applies this function and then
-     * applies the {@code after} function
+     * @return a composed function that first applies this function and then applies the {@code after} function
      * @throws NullPointerException if after is null
      *
      * @see #compose(IOFunction)
@@ -130,8 +120,7 @@ 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}.
+     * Creates a {@link Function} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
      *
      * @return an unchecked Function.
      * @since 2.12.0
@@ -141,16 +130,13 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies the {@code before}
-     * function to its input, and then applies this function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
+     * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
+     * composed function.
      *
-     * @param <V> the type of input to the {@code before} function, and to the
-     *           composed function
+     * @param <V> the type of input to the {@code before} function, and to the composed function
      * @param before the function to apply before this function is applied
-     * @return a composed function that first applies the {@code before}
-     * function and then applies this function
+     * @return a composed function that first applies the {@code before} function and then applies this function
      * @throws NullPointerException if before is null
      *
      * @see #andThen(IOFunction)
@@ -161,16 +147,13 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies the {@code before}
-     * function to its input, and then applies this function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
+     * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
+     * composed function.
      *
-     * @param <V> the type of input to the {@code before} function, and to the
-     *           composed function
+     * @param <V> the type of input to the {@code before} function, and to the composed function
      * @param before the function to apply before this function is applied
-     * @return a composed function that first applies the {@code before}
-     * function and then applies this function
+     * @return a composed function that first applies the {@code before} function and then applies this function
      * @throws NullPointerException if before is null
      *
      * @see #andThen(IOFunction)
@@ -181,14 +164,12 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies the {@code before}
-     * function to its input, and then applies this function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
+     * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
+     * composed function.
      *
      * @param before the supplier which feeds the application of this function
-     * @return a composed function that first applies the {@code before}
-     * function and then applies this function
+     * @return a composed function that first applies the {@code before} function and then applies this function
      * @throws NullPointerException if before is null
      *
      * @see #andThen(IOFunction)
@@ -199,14 +180,12 @@ public interface IOFunction<T, R> {
     }
 
     /**
-     * Returns a composed {@link IOFunction} that first applies the {@code before}
-     * function to its input, and then applies this function to the result.
-     * If evaluation of either function throws an exception, it is relayed to
-     * the caller of the composed function.
+     * Returns a composed {@link IOFunction} that first applies the {@code before} function to its input, and then applies
+     * this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the
+     * composed function.
      *
      * @param before the supplier which feeds the application of this function
-     * @return a composed function that first applies the {@code before}
-     * function and then applies this function
+     * @return a composed function that first applies the {@code before} function and then applies this function
      * @throws NullPointerException if before is null
      *
      * @see #andThen(IOFunction)
diff --git a/src/main/java/org/apache/commons/io/function/IOPredicate.java b/src/main/java/org/apache/commons/io/function/IOPredicate.java
index c7b4fe6a..fb9062b3 100644
--- a/src/main/java/org/apache/commons/io/function/IOPredicate.java
+++ b/src/main/java/org/apache/commons/io/function/IOPredicate.java
@@ -85,7 +85,7 @@ public interface IOPredicate<T> {
     }
 
     /**
-     * Converts this instance to a {@link Predicate} that throws {@link UncheckedIOException} instead of
+     * Creates a {@link Predicate} for this instance that throws {@link UncheckedIOException} instead of
      * {@link IOException}.
      *
      * @return an unchecked Predicate.
diff --git a/src/main/java/org/apache/commons/io/function/IOSupplier.java b/src/main/java/org/apache/commons/io/function/IOSupplier.java
index a9a656a2..cbdfac83 100644
--- a/src/main/java/org/apache/commons/io/function/IOSupplier.java
+++ b/src/main/java/org/apache/commons/io/function/IOSupplier.java
@@ -31,7 +31,7 @@ import java.util.function.Supplier;
 public interface IOSupplier<T> {
 
     /**
-     * Converts this instance to a Supplier that throws {@link UncheckedIOException} instead of {@link IOException}.
+     * Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
      *
      * @return an unchecked Predicate.
      * @since 2.12.0


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

Posted by gg...@apache.org.
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());
+    }
+
 }


[commons-io] 03/05: Add IOBiConsumer#asBiConsumer()

Posted by gg...@apache.org.
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 28f639e720ed32ac17bbb4eb90d8fff68305d0c8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jul 28 00:18:02 2022 -0400

    Add IOBiConsumer#asBiConsumer()
---
 .../java/org/apache/commons/io/function/IOBiConsumer.java | 13 +++++++++++++
 .../org/apache/commons/io/function/IOBiConsumerTest.java  | 15 +++++++++++++++
 .../org/apache/commons/io/function/IOConsumerTest.java    |  1 -
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/io/function/IOBiConsumer.java b/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
index f7dc67b8..fbe25077 100644
--- a/src/main/java/org/apache/commons/io/function/IOBiConsumer.java
+++ b/src/main/java/org/apache/commons/io/function/IOBiConsumer.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.BiConsumer;
 
@@ -70,4 +71,16 @@ public interface IOBiConsumer<T, U> {
             after.accept(t, u);
         };
     }
+
+    /**
+     * Converts this instance to a {@link BiConsumer} that throws {@link UncheckedIOException} instead of
+     * {@link IOException}.
+     *
+     * @return an unchecked BiConsumer.
+     * @since 2.12.0
+     */
+    default BiConsumer<T, U> asBiConsumer() {
+        return (t, u) -> Uncheck.accept(this, t, u);
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java
index 2d7d9700..8a7de2c6 100644
--- a/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java
@@ -18,8 +18,12 @@
 package org.apache.commons.io.function;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.junit.jupiter.api.Test;
@@ -46,6 +50,17 @@ public class IOBiConsumerTest {
         assertEquals("B22B", ref.get());
     }
 
+    @Test
+    public void testAsBiConsumer() throws IOException {
+        final Map<String, Integer> map = new HashMap<>();
+        map.put("a", 1);
+        assertThrows(UncheckedIOException.class, () -> map.forEach(TestConstants.THROWING_IO_BI_CONSUMER.asBiConsumer()));
+        final AtomicReference<String> ref = new AtomicReference<>();
+        final IOBiConsumer<String, Integer> consumer1 = (t, u) -> ref.set(t + u);
+        map.forEach(consumer1.asBiConsumer());
+        assertEquals("a1", ref.get());
+    }
+
     @Test
     public void testNoopIOConsumer() throws IOException {
         IOBiConsumer.noop().accept(null, null);
diff --git a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
index 2a2f2949..8bcf93de 100644
--- a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
@@ -69,7 +69,6 @@ public class IOConsumerTest {
         assertEquals("a1", ref.get());
     }
 
-
     @Test
     public void testNoop() {
         final Closeable nullCloseable = null;


[commons-io] 01/05: Add IOBiFunction#asBiFunction()

Posted by gg...@apache.org.
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 dda40c55847eeeef9a9c414825f12718835b04d1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 27 19:13:15 2022 -0400

    Add IOBiFunction#asBiFunction()
---
 .../java/org/apache/commons/io/function/IOBiFunction.java    | 12 ++++++++++++
 .../org/apache/commons/io/function/IOBiFunctionTest.java     | 12 ++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/src/main/java/org/apache/commons/io/function/IOBiFunction.java b/src/main/java/org/apache/commons/io/function/IOBiFunction.java
index cc5afdb9..0d7ca5af 100644
--- a/src/main/java/org/apache/commons/io/function/IOBiFunction.java
+++ b/src/main/java/org/apache/commons/io/function/IOBiFunction.java
@@ -18,7 +18,9 @@
 package org.apache.commons.io.function;
 
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.util.Objects;
+import java.util.function.BiFunction;
 import java.util.function.Function;
 
 /**
@@ -75,4 +77,14 @@ public interface IOBiFunction<T, U, R> {
      * @throws IOException if an I/O error occurs.
      */
     R apply(T t, U u) throws IOException;
+
+    /**
+     * Converts this instance to a {@link BiFunction} that throws {@link UncheckedIOException} instead of
+     * {@link IOException}.
+     *
+     * @return an unchecked BiFunction.
+     */
+    default BiFunction<T, U, R> asBiFunction() {
+        return (t, u) -> Uncheck.apply(this, t, u);
+    }
 }
diff --git a/src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java b/src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java
index 2f5f86fe..941c9933 100644
--- a/src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java
+++ b/src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.function;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -25,6 +26,8 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
 import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.commons.io.file.PathUtils;
 import org.junit.jupiter.api.Test;
@@ -75,6 +78,15 @@ public class IOBiFunctionTest {
         assertThrows(IOException.class, () -> isDirectory.apply(PathUtils.current(), PathUtils.EMPTY_LINK_OPTION_ARRAY));
     }
 
+    @Test
+    public void testAsBiFunction() throws IOException {
+        final Map<String, Long> map = new HashMap<>();
+        map.put("1", 0L);
+        final IOBiFunction<String, Long, Long> f = (t, u) -> Files.size(PathUtils.current());
+        map.computeIfPresent("1", f.asBiFunction());
+        assertNotEquals(0L, map.get("1"));
+    }
+
     @Test
     public void testNoopIOConsumer() throws IOException {
         assertNull(IOBiFunction.noop().apply(null, null));