blob: 8a08c56b3c079cc11097a81d9416b57cd4d12706 (
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)); }
}
}
|