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
The following commit(s) were added to refs/heads/master by this push: new 12dbeafe9 Add IOSupplier.getUnchecked() 12dbeafe9 is described below commit 12dbeafe93f7d63512db1c07cbe7bd9bef985044 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jul 5 10:34:58 2024 -0400 Add IOSupplier.getUnchecked() --- src/changes/changes.xml | 1 + .../org/apache/commons/io/function/IOSupplier.java | 18 +++++++++++++-- .../apache/commons/io/function/IOSupplierTest.java | 26 +++++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index da9e4ab2d..34101750a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove. <!-- ADD --> <action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterator.adapt(Iterable).</action> <action dev="ggregory" type="add" issue="IO-831" due-to="Elliotte Rusty Harold, Thach Le, Gary Gregory">Add getInputStream() for 'https' and 'http' in URIOrigin #630.</action> + <action dev="ggregory" type="add" due-to="Gary Gregory">Add IOSupplier.getUnchecked().</action> <!-- FIX --> <action dev="ggregory" type="fix" due-to="Gary Gregory">Add missing unit tests.</action> <action dev="ggregory" type="fix" due-to="Gary Gregory">FileUtils.lastModifiedFileTime(File) calls Objects.requireNonNull() on the wrong object.</action> diff --git a/src/main/java/org/apache/commons/io/function/IOSupplier.java b/src/main/java/org/apache/commons/io/function/IOSupplier.java index 5a81b407d..a021e5fce 100644 --- a/src/main/java/org/apache/commons/io/function/IOSupplier.java +++ b/src/main/java/org/apache/commons/io/function/IOSupplier.java @@ -23,6 +23,9 @@ import java.util.function.Supplier; /** * Like {@link Supplier} but throws {@link IOException}. + * <p> + * Using an IOSupplier allows you to compose usage of checked and unchecked exceptions as you best see fit. + * </p> * * @param <T> the return type of the operations. * @since 2.7 @@ -37,14 +40,25 @@ public interface IOSupplier<T> { * @since 2.12.0 */ default Supplier<T> asSupplier() { - return () -> Uncheck.get(this); + return this::getUnchecked; } /** * Gets a result. * - * @return a result + * @return a result. * @throws IOException if an I/O error occurs. */ T get() throws IOException; + + /** + * Gets a result. + * + * @return a result. + * @throws UncheckedIOException if an I/O error occurs. + * @since 2.17.0 + */ + default T getUnchecked() throws UncheckedIOException { + return Uncheck.get(this); + } } diff --git a/src/test/java/org/apache/commons/io/function/IOSupplierTest.java b/src/test/java/org/apache/commons/io/function/IOSupplierTest.java index 7a8e94f57..ef60b29af 100644 --- a/src/test/java/org/apache/commons/io/function/IOSupplierTest.java +++ b/src/test/java/org/apache/commons/io/function/IOSupplierTest.java @@ -39,10 +39,14 @@ public class IOSupplierTest { return supplier.get(); } - private String getThrowsNone(final IOSupplier<String> supplier) { + private String getThrowsNoneAsSupplier(final IOSupplier<String> supplier) { return supplier.asSupplier().get(); } + private String getThrowsNoneGetUnchecked(final IOSupplier<String> supplier) { + return supplier.getUnchecked(); + } + @BeforeEach public void initEach() { ref1 = new AtomicReference<>(); @@ -51,8 +55,12 @@ public class IOSupplierTest { @Test public void testAsSupplier() { assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_SUPPLIER.asSupplier().get()); - assertEquals("new1", getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(ref1, "new1"))); - assertEquals("new1", ref1.get()); + final String s1 = "string1"; + final String s2 = "string2"; + assertEquals(s1, getThrowsNoneAsSupplier(() -> TestUtils.compareAndSetThrowsIO(ref1, null, s1))); + assertEquals(s1, ref1.get()); + assertEquals(s2, getThrowsNoneAsSupplier(() -> TestUtils.compareAndSetThrowsIO(ref1, s1, s2))); + assertEquals(s2, ref1.get()); assertNotEquals(TestConstants.THROWING_IO_SUPPLIER.asSupplier(), TestConstants.THROWING_IO_SUPPLIER.asSupplier()); } @@ -66,4 +74,16 @@ public class IOSupplierTest { assertEquals("new1", ref1.get()); } + @Test + public void testGetUnchecked() { + assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_SUPPLIER.asSupplier().get()); + final String s1 = "string1"; + final String s2 = "string2"; + assertEquals(s1, getThrowsNoneGetUnchecked(() -> TestUtils.compareAndSetThrowsIO(ref1, null, s1))); + assertEquals(s1, ref1.get()); + assertEquals(s2, getThrowsNoneGetUnchecked(() -> TestUtils.compareAndSetThrowsIO(ref1, s1, s2))); + assertEquals(s2, ref1.get()); + assertNotEquals(TestConstants.THROWING_IO_SUPPLIER.asSupplier(), TestConstants.THROWING_IO_SUPPLIER.asSupplier()); + } + } \ No newline at end of file