diff options
author | Håkon Hallingstad <hakon@oath.com> | 2018-01-27 22:21:42 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2018-01-27 22:21:42 +0100 |
commit | 1ca31adc455b4fb92b7844f9e92959344852a087 (patch) | |
tree | aacb64180c5183fe3659c8cad21d5aab4c447303 | |
parent | c3168ca58af1396e7a7562fdeb15fa604425020d (diff) |
Use ifExists util
2 files changed, 4 insertions, 14 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 ef553a131e8..c36567e6135 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,9 +1,9 @@ // 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 java.io.FileNotFoundException; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.NoSuchFileException; import java.util.Optional; public class IOExceptionUtil { @@ -36,13 +36,13 @@ public class IOExceptionUtil { /** * Useful if it's not known whether a file or directory exists, in case e.g. - * FileNotFoundException is thrown and the caller wants an Optional.empty() in that case. + * NoSuchFileException is thrown and the caller wants an Optional.empty() in that case. */ public static <T> Optional<T> ifExists(SupplierThrowingIOException<T> supplier) { try { return Optional.ofNullable(uncheck(supplier)); } catch (UncheckedIOException e) { - if (e.getCause() instanceof FileNotFoundException) { + if (e.getCause() instanceof NoSuchFileException) { return Optional.empty(); } 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 a83d79a97a9..05691bfcdc7 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 @@ -1,10 +1,8 @@ // 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 java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.NoSuchFileException; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; @@ -108,15 +106,7 @@ public class UnixPath { } public Optional<FileAttributes> getAttributesIfExists() { - try { - return Optional.of(getAttributes()); - } catch (UncheckedIOException e) { - if (e.getCause() instanceof NoSuchFileException) { - return Optional.empty(); - } - - throw e; - } + return IOExceptionUtil.ifExists(() -> getAttributes()); } @Override |