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

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

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));