diff options
Diffstat (limited to 'yolean')
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/Exceptions.java | 25 | ||||
-rw-r--r-- | yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java | 21 |
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"); } } |