Skip to content

Commit d3b5140

Browse files
committed
Add Uncheck.getAsLong(IOIntSupplier, Supplier<String>)
1 parent 6fea8f4 commit d3b5140

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The <action> type attribute can be add,update,fix,remove.
7171
Add Uncheck.getAsInt(IOIntSupplier [, Supplier&lt;String&gt;]).
7272
</action>
7373
<action dev="ggregory" type="add" due-to="Gary Gregory">
74-
Add Uncheck.getAsLong(IOLongSupplier).
74+
Add Uncheck.getAsLong(IOLongSupplier [, Supplier&lt;String&gt;]).
7575
</action>
7676
<action dev="ggregory" type="add" due-to="Gary Gregory">
7777
Add Uncheck.run(IORunnable, Supplier&lt;String&gt;)

src/main/java/org/apache/commons/io/function/Uncheck.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,23 @@ public static long getAsLong(final IOLongSupplier supplier) {
266266
}
267267
}
268268

269+
/**
270+
* Gets the result from an IO long supplier.
271+
*
272+
* @param supplier Supplies the return value.
273+
* @param message The UncheckedIOException message if an I/O error occurs.
274+
* @return result from the supplier.
275+
* @throws UncheckedIOException if an I/O error occurs.
276+
* @since 2.14.0
277+
*/
278+
public static long getAsLong(final IOLongSupplier supplier, final Supplier<String> message) {
279+
try {
280+
return supplier.getAsLong();
281+
} catch (final IOException e) {
282+
throw wrap(e, message);
283+
}
284+
}
285+
269286
/**
270287
* Runs an IO runnable.
271288
*

src/test/java/org/apache/commons/io/function/UncheckTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ public void testGetAsInt() {
238238
assertEquals(1, atomicInt.get());
239239
}
240240

241+
@Test
242+
public void testGetAsIntMessage() {
243+
// No exception
244+
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
245+
throw new IOException();
246+
}, () -> CUSTOM_MESSAGE));
247+
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
248+
assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
249+
assertEquals(1, atomicInt.get());
250+
// exception
251+
final IOException expected = new IOException(CAUSE_MESSAGE);
252+
try {
253+
Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
254+
fail();
255+
} catch (final UncheckedIOException e) {
256+
assertUncheckedIOException(expected, e);
257+
}
258+
}
259+
241260
@Test
242261
public void testGetAsLong() {
243262
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
@@ -249,18 +268,18 @@ public void testGetAsLong() {
249268
}
250269

251270
@Test
252-
public void testGetAsIntMessage() {
271+
public void testGetAsLongMessage() {
253272
// No exception
254-
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
273+
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(() -> {
255274
throw new IOException();
256275
}, () -> CUSTOM_MESSAGE));
257-
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER, () -> CUSTOM_MESSAGE));
258-
assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1), () -> CUSTOM_MESSAGE));
259-
assertEquals(1, atomicInt.get());
276+
assertThrows(UncheckedIOException.class, () -> Uncheck.getAsLong(TestConstants.THROWING_IO_LONG_SUPPLIER, () -> CUSTOM_MESSAGE));
277+
assertEquals(1L, Uncheck.getAsLong(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L), () -> CUSTOM_MESSAGE));
278+
assertEquals(1L, atomicLong.get());
260279
// exception
261280
final IOException expected = new IOException(CAUSE_MESSAGE);
262281
try {
263-
Uncheck.getAsInt(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
282+
Uncheck.getAsLong(() -> new BrokenInputStream(expected).read(), () -> CUSTOM_MESSAGE);
264283
fail();
265284
} catch (final UncheckedIOException e) {
266285
assertUncheckedIOException(expected, e);

0 commit comments

Comments
 (0)