diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-11-26 09:35:05 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-11-26 09:35:05 +0100 |
commit | 39a5fb33dada81891d9082e0a5d616efe62c4c06 (patch) | |
tree | a22e2bd57f35f3d196976cdfee1492f63bf21df8 /yolean/src/main | |
parent | 1e3df1dcc8456c6f1df160eb346afd2385eaa00b (diff) |
Allow ignoring any IOException
Diffstat (limited to 'yolean/src/main')
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/Exceptions.java | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index 4144af81a61..fa3eb412016 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.NoSuchFileException; import java.util.Optional; +import java.util.function.Supplier; /** * Helper methods for handling exceptions @@ -72,20 +73,26 @@ public class Exceptions { } } - - /** Similar to uncheck() except NoSuchFileException is silently ignored. */ - public static void ifExists(RunnableThrowingIOException runnable) { + /** Similar to uncheck(), except an exceptionToIgnore exception is silently ignored. */ + public static <T extends IOException> void uncheckAndIgnore(RunnableThrowingIOException runnable, Class<T> exceptionToIgnore) { try { runnable.run(); - } catch (NoSuchFileException e) { - // Do nothing - OK } catch (UncheckedIOException e) { - if (! (e.getCause() instanceof NoSuchFileException)) { + IOException cause = e.getCause(); + if (cause == null) throw e; + try { + cause.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { throw e; } // Do nothing - OK } catch (IOException e) { - throw new UncheckedIOException(e); + try { + e.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw new UncheckedIOException(e); + } + // Do nothing - OK } } @@ -117,19 +124,26 @@ public class Exceptions { } } - /** Similar to uncheck() except the return value is wrapped in an Optional, and empty is returned on NoSuchFileException. */ - public static <T> Optional<T> ifExists(SupplierThrowingIOException<T> supplier) { + /** Similar to uncheck(), except null is returned if exceptionToIgnore is thrown. */ + public static <R, T extends IOException> R uncheckAndIgnore(SupplierThrowingIOException<R> supplier, Class<T> exceptionToIgnore) { try { - return Optional.ofNullable(supplier.get()); - } catch (NoSuchFileException e) { - return Optional.empty(); + return supplier.get(); } catch (UncheckedIOException e) { - if (e.getCause() instanceof NoSuchFileException) { - return Optional.empty(); + IOException cause = e.getCause(); + if (cause == null) throw e; + try { + cause.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw e; } - throw e; + return null; } catch (IOException e) { - throw new UncheckedIOException(e); + try { + e.getClass().asSubclass(exceptionToIgnore); + } catch (ClassCastException f) { + throw new UncheckedIOException(e); + } + return null; } } |