aboutsummaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-11-26 09:35:05 +0100
committerHåkon Hallingstad <hakon@oath.com>2018-11-26 09:35:05 +0100
commit39a5fb33dada81891d9082e0a5d616efe62c4c06 (patch)
treea22e2bd57f35f3d196976cdfee1492f63bf21df8 /yolean
parent1e3df1dcc8456c6f1df160eb346afd2385eaa00b (diff)
Allow ignoring any IOException
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java46
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java13
2 files changed, 37 insertions, 22 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;
}
}
diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
index e1786528b31..390018efdf7 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -9,6 +9,7 @@ import java.nio.file.NoSuchFileException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
/**
* @author bratseth
@@ -31,7 +32,7 @@ public class ExceptionsTestCase {
try {
Exceptions.uncheck(this::throwNoSuchFileException);
} catch (UncheckedIOException e) {
- assertEquals("root cause", e.getCause().getMessage());
+ assertEquals("filename", e.getCause().getMessage());
}
try {
@@ -43,7 +44,7 @@ public class ExceptionsTestCase {
try {
int i = Exceptions.uncheck(this::throwNoSuchFileExceptionSupplier);
} catch (UncheckedIOException e) {
- assertEquals("root cause", e.getCause().getMessage());
+ assertEquals("filename", e.getCause().getMessage());
}
try {
@@ -52,15 +53,15 @@ public class ExceptionsTestCase {
assertEquals("additional info", e.getMessage());
}
- Exceptions.ifExists(this::throwNoSuchFileException);
- assertFalse(Exceptions.ifExists(this::throwNoSuchFileExceptionSupplier).isPresent());
+ Exceptions.uncheckAndIgnore(this::throwNoSuchFileException, NoSuchFileException.class);
+ assertNull(Exceptions.uncheckAndIgnore(this::throwNoSuchFileExceptionSupplier, NoSuchFileException.class));
}
private void throwNoSuchFileException() throws IOException {
- throw new NoSuchFileException("root cause");
+ throw new NoSuchFileException("filename");
}
private int throwNoSuchFileExceptionSupplier() throws IOException {
- throw new NoSuchFileException("root cause");
+ throw new NoSuchFileException("filename");
}
}