diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-11-27 00:34:14 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-11-27 00:34:14 +0100 |
commit | b78827cfaccdfd0c3417cdaaae540045123d15f2 (patch) | |
tree | b7a2175a67f7872353c9c62e38176a3917c7c080 /node-admin/src | |
parent | 7cf6eab97522fdc028ea783bd80af017fe91f464 (diff) |
Remove redundant utils in IOExceptionUtil
Diffstat (limited to 'node-admin/src')
2 files changed, 7 insertions, 44 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java index 26ca069aceb..8a23b1d18b1 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java @@ -1,11 +1,15 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.task.util.file; +import com.yahoo.yolean.Exceptions; + import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.NoSuchFileException; import java.util.Optional; +import static com.yahoo.yolean.Exceptions.uncheck; + /** * Utils related to IOException. * @@ -14,53 +18,11 @@ import java.util.Optional; * @author hakonhall */ public class IOExceptionUtil { - public static <T> void uncheck(RunnableThrowingIOException<T> runnable) { - try { - runnable.run(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - /** - * If an IOException is thrown, an UncheckedIOException is made with the given format+args - * message. Useful to decorate the exception when the bare IOException leaves out details. - */ - public static <T> T uncheck(SupplierThrowingIOException<T> supplier, - String format, - String... args) { - try { - return supplier.get(); - } catch (IOException e) { - String message = String.format(format, (Object[]) args); - throw new UncheckedIOException(message, e); - } - } - - public static <T> T uncheck(SupplierThrowingIOException<T> supplier) { - try { - return supplier.get(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - @FunctionalInterface - public interface SupplierThrowingIOException<T> { - T get() throws IOException; - } - - - @FunctionalInterface - public interface RunnableThrowingIOException<T> { - void run() throws IOException; - } - /** * Useful if it's not known whether a file or directory exists, in case e.g. * NoSuchFileException is thrown and the caller wants an Optional.empty() in that case. */ - public static <T> Optional<T> ifExists(SupplierThrowingIOException<T> supplier) { + public static <T> Optional<T> ifExists(Exceptions.SupplierThrowingIOException<T> supplier) { try { return Optional.ofNullable(uncheck(supplier)); } catch (UncheckedIOException e) { diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index f573051eca6..67fcfc69393 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -26,6 +26,7 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.yahoo.vespa.hosted.node.admin.task.util.file.IOExceptionUtil.ifExists; import static com.yahoo.yolean.Exceptions.uncheck; import static com.yahoo.yolean.Exceptions.uncheckAndIgnore; @@ -118,7 +119,7 @@ public class UnixPath { } public Optional<FileAttributes> getAttributesIfExists() { - return Optional.ofNullable(uncheckAndIgnore(this::getAttributes, NoSuchFileException.class)); + return ifExists(this::getAttributes); } public UnixPath createNewFile() { |