summaryrefslogtreecommitdiffstats
path: root/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
diff options
context:
space:
mode:
Diffstat (limited to 'yolean/src/main/java/com/yahoo/yolean/Exceptions.java')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java25
1 files changed, 25 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 82677a14242..bbd03bbdc9e 100644
--- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
+++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
@@ -3,6 +3,8 @@ package com.yahoo.yolean;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.nio.file.NoSuchFileException;
+import java.util.Optional;
/**
* Helper methods for handling exceptions
@@ -70,6 +72,18 @@ public class Exceptions {
}
}
+
+ /** Similar to uncheck() except NoSuchFileException is silently ignored. */
+ public static void ifExists(RunnableThrowingIOException runnable) {
+ try {
+ runnable.run();
+ } catch (NoSuchFileException e) {
+ // Do nothing - OK
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
@FunctionalInterface
public interface RunnableThrowingIOException {
void run() throws IOException;
@@ -98,6 +112,17 @@ 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) {
+ try {
+ return Optional.ofNullable(supplier.get());
+ } catch (NoSuchFileException e) {
+ return Optional.empty();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
@FunctionalInterface
public interface SupplierThrowingIOException<T> {
T get() throws IOException;