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 <gardgreg...@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;