Skip to content

Commit 28f639e

Browse files
author
Gary Gregory
committed
Add IOBiConsumer#asBiConsumer()
1 parent 35a3084 commit 28f639e

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/main/java/org/apache/commons/io/function/IOBiConsumer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.io.function;
1919

2020
import java.io.IOException;
21+
import java.io.UncheckedIOException;
2122
import java.util.Objects;
2223
import java.util.function.BiConsumer;
2324

@@ -70,4 +71,16 @@ default IOBiConsumer<T, U> andThen(final IOBiConsumer<? super T, ? super U> afte
7071
after.accept(t, u);
7172
};
7273
}
74+
75+
/**
76+
* Converts this instance to a {@link BiConsumer} that throws {@link UncheckedIOException} instead of
77+
* {@link IOException}.
78+
*
79+
* @return an unchecked BiConsumer.
80+
* @since 2.12.0
81+
*/
82+
default BiConsumer<T, U> asBiConsumer() {
83+
return (t, u) -> Uncheck.accept(this, t, u);
84+
}
85+
7386
}

src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
package org.apache.commons.io.function;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.IOException;
24+
import java.io.UncheckedIOException;
25+
import java.util.HashMap;
26+
import java.util.Map;
2327
import java.util.concurrent.atomic.AtomicReference;
2428

2529
import org.junit.jupiter.api.Test;
@@ -46,6 +50,17 @@ public void testAndThen() throws IOException {
4650
assertEquals("B22B", ref.get());
4751
}
4852

53+
@Test
54+
public void testAsBiConsumer() throws IOException {
55+
final Map<String, Integer> map = new HashMap<>();
56+
map.put("a", 1);
57+
assertThrows(UncheckedIOException.class, () -> map.forEach(TestConstants.THROWING_IO_BI_CONSUMER.asBiConsumer()));
58+
final AtomicReference<String> ref = new AtomicReference<>();
59+
final IOBiConsumer<String, Integer> consumer1 = (t, u) -> ref.set(t + u);
60+
map.forEach(consumer1.asBiConsumer());
61+
assertEquals("a1", ref.get());
62+
}
63+
4964
@Test
5065
public void testNoopIOConsumer() throws IOException {
5166
IOBiConsumer.noop().accept(null, null);

src/test/java/org/apache/commons/io/function/IOConsumerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public void testAsConsumer() throws IOException {
6969
assertEquals("a1", ref.get());
7070
}
7171

72-
7372
@Test
7473
public void testNoop() {
7574
final Closeable nullCloseable = null;

0 commit comments

Comments
 (0)