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 2023/06/24 17:21:43 UTC

[commons-io] 02/02: Add Uncheck.getAsInt(IOIntSupplier, Supplier)

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 c6d891ec9368bda27544d1fda8d534a99cf068e6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:21:37 2023 -0400

    Add Uncheck.getAsInt(IOIntSupplier, Supplier<String>)
---
 src/changes/changes.xml                               |  4 ++--
 .../java/org/apache/commons/io/function/Uncheck.java  | 17 +++++++++++++++++
 .../org/apache/commons/io/function/UncheckTest.java   | 19 +++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index dc2024d3..13256551 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,10 +65,10 @@ The <action> type attribute can be add,update,fix,remove.
         Add IOIntSupplier.
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Add Uncheck.getAsInt(IOIntSupplier).
+        Add IOLongSupplier.
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
-        Add IOLongSupplier.
+        Add Uncheck.getAsInt(IOIntSupplier [, Supplier&lt;String&gt;]).
       </action>
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add Uncheck.getAsLong(IOLongSupplier).
diff --git a/src/main/java/org/apache/commons/io/function/Uncheck.java b/src/main/java/org/apache/commons/io/function/Uncheck.java
index 6419db31..13085cf9 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -233,6 +233,23 @@ public final class Uncheck {
         }
     }
 
+    /**
+     * Gets the result from an IO int supplier.
+     *
+     * @param supplier Supplies the return value.
+     * @param message The UncheckedIOException message if an I/O error occurs.
+     * @return result from the supplier.
+     * @throws UncheckedIOException if an I/O error occurs.
+     * @since 2.14.0
+     */
+    public static int getAsInt(final IOIntSupplier supplier, final Supplier<String> message) {
+        try {
+            return supplier.getAsInt();
+        } catch (final IOException e) {
+            throw wrap(e, message);
+        }
+    }
+
     /**
      * Gets the result from an IO long supplier.
      *
diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index 67186897..6597166d 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -235,6 +235,25 @@ public class UncheckTest {
         assertEquals(1, atomicInt.get());
     }
 
+    @Test
+    public void testGetAsIntMessage() {
+        // No exception
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
+            throw new IOException();
+        }, () -> CUSTOM_MESSAGE));
+        assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
+        assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
+        assertEquals(1, atomicInt.get());
+        // exception
+        final IOException expected = new IOException(CAUSE_MESSAGE);
+        try {
+            Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+            fail();
+        } catch (final UncheckedIOException e) {
+            assertUncheckedIOException(expected, e);
+        }
+    }
+
     /**
      * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
      */