aboutsummaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-05-25 17:14:55 +0200
committerHåkon Hallingstad <hakon@oath.com>2018-05-25 17:14:55 +0200
commit8022f5c2b9bfc271cd7bf9b151fd32b144d650e8 (patch)
tree989a16f56f009b8b5ce77b3c7873bd33e1abbcc9 /yolean
parent13bca1f3eca84f0ce20fb02e063a4016736902ff (diff)
Add uncheck to yolean
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java58
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java37
2 files changed, 95 insertions, 0 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
index 83c381e586f..82677a14242 100644
--- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
+++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
@@ -1,6 +1,9 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+
/**
* Helper methods for handling exceptions
*
@@ -44,4 +47,59 @@ public class Exceptions {
return message;
}
+ /**
+ * Wraps any IOException thrown from a runnable in an UncheckedIOException.
+ */
+ public static void uncheck(RunnableThrowingIOException runnable) {
+ try {
+ runnable.run();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ /**
+ * Wraps any IOException thrown from a runnable in an UncheckedIOException w/message.
+ */
+ public static void uncheck(RunnableThrowingIOException runnable, String format, String... args) {
+ try {
+ runnable.run();
+ } catch (IOException e) {
+ String message = String.format(format, (Object[]) args);
+ throw new UncheckedIOException(message, e);
+ }
+ }
+
+ @FunctionalInterface
+ public interface RunnableThrowingIOException {
+ void run() throws IOException;
+ }
+
+ /**
+ * Wraps any IOException thrown from a supplier in an UncheckedIOException.
+ */
+ public static <T> T uncheck(SupplierThrowingIOException<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ /**
+ * Wraps any IOException thrown from a supplier in an UncheckedIOException w/message.
+ */
+ public static <T> T uncheck(SupplierThrowingIOException<T> supplier, String format, String... args) {
+ try {
+ return supplier.get();
+ } catch (IOException e) {
+ String message = String.format(format, (Object[]) args);
+ throw new UncheckedIOException(message, e);
+ }
+ }
+
+ @FunctionalInterface
+ public interface SupplierThrowingIOException<T> {
+ T get() throws IOException;
+ }
}
diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
index db605609926..31e27fa2675 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -3,6 +3,9 @@ package com.yahoo.yolean;
import org.junit.Test;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+
import static org.junit.Assert.assertEquals;
/**
@@ -21,4 +24,38 @@ public class ExceptionsTestCase {
assertEquals("Foo",Exceptions.toMessageString(new Exception(new Exception("Foo"))));
}
+ @Test
+ public void testUnchecks() {
+ try {
+ Exceptions.uncheck(this::throwIO);
+ } catch (UncheckedIOException e) {
+ assertEquals("root cause", e.getCause().getMessage());
+ }
+
+ try {
+ Exceptions.uncheck(this::throwIO, "additional %s", "info");
+ } catch (UncheckedIOException e) {
+ assertEquals("additional info", e.getMessage());
+ }
+
+ try {
+ int i = Exceptions.uncheck(this::throwIOWithReturnValue);
+ } catch (UncheckedIOException e) {
+ assertEquals("root cause", e.getCause().getMessage());
+ }
+
+ try {
+ int i = Exceptions.uncheck(this::throwIOWithReturnValue, "additional %s", "info");
+ } catch (UncheckedIOException e) {
+ assertEquals("additional info", e.getMessage());
+ }
+ }
+
+ private void throwIO() throws IOException {
+ throw new IOException("root cause");
+ }
+
+ private int throwIOWithReturnValue() throws IOException {
+ throw new IOException("root cause");
+ }
}