diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-05-25 17:14:55 +0200 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-05-25 17:14:55 +0200 |
commit | 8022f5c2b9bfc271cd7bf9b151fd32b144d650e8 (patch) | |
tree | 989a16f56f009b8b5ce77b3c7873bd33e1abbcc9 /yolean/src/main | |
parent | 13bca1f3eca84f0ce20fb02e063a4016736902ff (diff) |
Add uncheck to yolean
Diffstat (limited to 'yolean/src/main')
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/Exceptions.java | 58 |
1 files changed, 58 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; + } } |