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:57:22 UTC
[commons-io] 02/02: Add Uncheck.getAsLong(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 d3b51407b3cbc3709d077ee43fe5c699b421edb1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:47:11 2023 -0400
Add Uncheck.getAsLong(IOIntSupplier, Supplier<String>)
---
src/changes/changes.xml | 2 +-
.../org/apache/commons/io/function/Uncheck.java | 17 ++++++++++++
.../apache/commons/io/function/UncheckTest.java | 31 +++++++++++++++++-----
3 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 13256551..c285a8c7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -71,7 +71,7 @@ The <action> type attribute can be add,update,fix,remove.
Add Uncheck.getAsInt(IOIntSupplier [, Supplier<String>]).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
- Add Uncheck.getAsLong(IOLongSupplier).
+ Add Uncheck.getAsLong(IOLongSupplier [, Supplier<String>]).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add Uncheck.run(IORunnable, Supplier<String>)
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 13085cf9..3b9c7d36 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -266,6 +266,23 @@ public final class Uncheck {
}
}
+ /**
+ * Gets the result from an IO long 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 long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
+ try {
+ return supplier.getAsLong();
+ } catch (final IOException e) {
+ throw wrap(e, message);
+ }
+ }
+
/**
* Runs an IO runnable.
*
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 d0785477..3eff77e8 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -238,6 +238,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);
+ }
+ }
+
@Test
public void testGetAsLong() {
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
@@ -249,18 +268,18 @@ public class UncheckTest {
}
@Test
- public void testGetAsIntMessage() {
+ public void testGetAsLongMessage() {
// No exception
- assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
+ assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
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());
+ assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER, () -> CUSTOM_MESSAGE));
+ assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L), () -> CUSTOM_MESSAGE));
+ assertEquals(1L, atomicLong.get());
// exception
final IOException expected = new IOException(CAUSE_MESSAGE);
try {
- Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
+ Uncheck.getAsLong(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
fail();
} catch (final UncheckedIOException e) {
assertUncheckedIOException(expected, e);