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 16:54:25 UTC
[commons-io] branch master updated: Add Uncheck.get(IOSupplier, 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
The following commit(s) were added to refs/heads/master by this push:
new b4caca0c Add Uncheck.get(IOSupplier, Supplier<String>)
b4caca0c is described below
commit b4caca0c89ff116ec1b2c0012cc00a00821d19a3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 12:54:21 2023 -0400
Add Uncheck.get(IOSupplier, Supplier<String>)
---
src/changes/changes.xml | 3 ++
.../org/apache/commons/io/function/Uncheck.java | 17 +++++++++++
.../java/org/apache/commons/io/UncheckIOTest.java | 34 ++++++++++++++++++----
3 files changed, 48 insertions(+), 6 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index badc1289..dc2024d3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -76,6 +76,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.run(IORunnable, Supplier<String>)
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add Uncheck.get(IOSupplier, Supplier<String>)
+ </action>
<!-- FIX -->
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
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 e9da1633..6419db31 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -200,6 +200,23 @@ public final class Uncheck {
}
}
+ /**
+ * Gets the result from an IO supplier.
+ *
+ * @param <T> the return type of the operations.
+ * @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.
+ */
+ public static <T> T get(final IOSupplier<T> supplier, final Supplier<String> message) {
+ try {
+ return supplier.get();
+ } catch (final IOException e) {
+ throw wrap(e, message);
+ }
+ }
+
/**
* Gets the result from an IO int supplier.
*
diff --git a/src/test/java/org/apache/commons/io/UncheckIOTest.java b/src/test/java/org/apache/commons/io/UncheckIOTest.java
index 8ea4d796..3611cc3c 100644
--- a/src/test/java/org/apache/commons/io/UncheckIOTest.java
+++ b/src/test/java/org/apache/commons/io/UncheckIOTest.java
@@ -43,6 +43,13 @@ public class UncheckIOTest {
private static final String CUSTOM_MESSAGE = "Custom message";
private static final byte[] BYTES = { 'a', 'b' };
+ private void assertUncheckedIOException(final IOException expected, final UncheckedIOException e) {
+ assertEquals(CUSTOM_MESSAGE, e.getMessage());
+ final IOException cause = e.getCause();
+ assertEquals(expected.getClass(), cause.getClass());
+ assertEquals(CAUSE_MESSAGE, cause.getMessage());
+ }
+
private ByteArrayInputStream newInputStream() {
return new ByteArrayInputStream(BYTES);
}
@@ -97,6 +104,23 @@ public class UncheckIOTest {
assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue());
}
+ /**
+ * Tests {@link Uncheck#get(IOSupplier, Supplier)}.
+ */
+ @Test
+ public void testGetMessage() {
+ // No exception
+ assertEquals('a', Uncheck.get(() -> newInputStream().read()).intValue(), () -> CUSTOM_MESSAGE);
+ // Exception
+ final IOException expected = new IOException(CAUSE_MESSAGE);
+ try {
+ Uncheck.get(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+ fail();
+ } catch (final UncheckedIOException e) {
+ assertUncheckedIOException(expected, e);
+ }
+ }
+
/**
* Tests {@link Uncheck#run(IORunnable)}.
*/
@@ -108,25 +132,23 @@ public class UncheckIOTest {
}
/**
- * Tests {@link Uncheck#run(IORunnable)}.
+ * Tests {@link Uncheck#run(IORunnable, Supplier))}.
*
* @throws IOException
*/
@Test
public void testRunMessage() throws IOException {
+ // No exception
final ByteArrayInputStream stream = newInputStream();
Uncheck.run(() -> stream.skip(1), () -> CUSTOM_MESSAGE);
assertEquals('b', Uncheck.get(stream::read).intValue());
final IOException expected = new IOException(CAUSE_MESSAGE);
- //
+ // Exception
try {
Uncheck.run(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
fail();
} catch (final UncheckedIOException e) {
- assertEquals(CUSTOM_MESSAGE, e.getMessage());
- final IOException cause = e.getCause();
- assertEquals(expected.getClass(), cause.getClass());
- assertEquals(CAUSE_MESSAGE, cause.getMessage());
+ assertUncheckedIOException(expected, e);
}
}
}