diff options
Diffstat (limited to 'config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java')
-rw-r--r-- | config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java b/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java new file mode 100644 index 00000000000..8a08c56b3c0 --- /dev/null +++ b/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java @@ -0,0 +1,50 @@ +package com.yahoo.config.application; + +import com.yahoo.yolean.function.ThrowingFunction; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import java.util.function.Predicate; + +/** + * Wraps a real or virtual file system — essentially a mapping from paths to bytes. + * + * @author jonmv + */ +public class FileSystemWrapper { + + Predicate<Path> existence; + ThrowingFunction<Path, byte[], IOException> reader; + + private FileSystemWrapper(Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { + this.existence = existence; + this.reader = reader; + } + + public static FileSystemWrapper ofFiles(Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { + return new FileSystemWrapper(existence, reader); + } + + public static FileSystemWrapper getDefault() { + return ofFiles(Files::exists, Files::readAllBytes); + } + + public FileWrapper wrap(Path path) { + return new FileWrapper(path); + } + + + public class FileWrapper { + private final Path path; + private FileWrapper(Path path) { this.path = path; } + + public Path path() { return path; } + public boolean exists() { return existence.test(path); } + public byte[] content() throws IOException { return reader.apply(path); } + public Optional<FileWrapper> parent() { return Optional.ofNullable(path.getParent()).map(path -> wrap(path)); } + public FileWrapper child(String name) { return wrap(path.resolve(name)); } + } + +} |