aboutsummaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-11-25 21:17:14 +0100
committerHåkon Hallingstad <hakon@oath.com>2018-11-25 21:17:14 +0100
commit660d28ed2ac5cd98acef4fae8e7b22a92cbdbc13 (patch)
treef62f27f6b461a96229c0f84ac78e315fa9c2a9b2 /yolean
parenta01bc21d9bcbc417a9fb2591079561f59f76865e (diff)
Ignore NoSuchFileException with ifExists
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java25
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java21
2 files changed, 38 insertions, 8 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;
diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
index 31e27fa2675..e1786528b31 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -5,8 +5,10 @@ import org.junit.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.nio.file.NoSuchFileException;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
/**
* @author bratseth
@@ -27,35 +29,38 @@ public class ExceptionsTestCase {
@Test
public void testUnchecks() {
try {
- Exceptions.uncheck(this::throwIO);
+ Exceptions.uncheck(this::throwNoSuchFileException);
} catch (UncheckedIOException e) {
assertEquals("root cause", e.getCause().getMessage());
}
try {
- Exceptions.uncheck(this::throwIO, "additional %s", "info");
+ Exceptions.uncheck(this::throwNoSuchFileException, "additional %s", "info");
} catch (UncheckedIOException e) {
assertEquals("additional info", e.getMessage());
}
try {
- int i = Exceptions.uncheck(this::throwIOWithReturnValue);
+ int i = Exceptions.uncheck(this::throwNoSuchFileExceptionSupplier);
} catch (UncheckedIOException e) {
assertEquals("root cause", e.getCause().getMessage());
}
try {
- int i = Exceptions.uncheck(this::throwIOWithReturnValue, "additional %s", "info");
+ int i = Exceptions.uncheck(this::throwNoSuchFileExceptionSupplier, "additional %s", "info");
} catch (UncheckedIOException e) {
assertEquals("additional info", e.getMessage());
}
+
+ Exceptions.ifExists(this::throwNoSuchFileException);
+ assertFalse(Exceptions.ifExists(this::throwNoSuchFileExceptionSupplier).isPresent());
}
- private void throwIO() throws IOException {
- throw new IOException("root cause");
+ private void throwNoSuchFileException() throws IOException {
+ throw new NoSuchFileException("root cause");
}
- private int throwIOWithReturnValue() throws IOException {
- throw new IOException("root cause");
+ private int throwNoSuchFileExceptionSupplier() throws IOException {
+ throw new NoSuchFileException("root cause");
}
}