Add IOConsumer.asConsumer().
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/function/IOConsumer.java
src/main/java/org/apache/commons/io/function/IOConsumer.java
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;
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);
+ }
+
}

MODIFY
src/test/java/org/apache/commons/io/function/IOConsumerTest.java
src/test/java/org/apache/commons/io/function/IOConsumerTest.java
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 @@ void testAndThen() throws IOException {
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;

Add IOConsumer.asConsumer().