blob: 1f757bd6aba88ffaa2d4391e36801f9211887efb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// Copyright Yahoo. 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.UncheckedIOException;
import java.nio.file.NoSuchFileException;
import java.util.Optional;
import static com.yahoo.yolean.Exceptions.uncheck;
/**
* Utils related to IOException.
*
* @author hakonhall
*/
public class IOExceptionUtil {
/**
* 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(Exceptions.SupplierThrowingIOException<T> supplier) {
try {
return Optional.ofNullable(uncheck(supplier));
} catch (UncheckedIOException e) {
if (e.getCause() instanceof NoSuchFileException) {
return Optional.empty();
}
throw e;
}
}
}
|