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:28 UTC
[commons-io] 03/05: Add IOBiConsumer#asBiConsumer()
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;