mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-09-19 14:34:20 +00:00
Changes to filesystem API and nio implementation
* Partial implementation of nio filesystem * Addded some tests * Added project for common test dependencies * Removed default implementation of Folder#delete ** reason: didn't work because empty folders were not deleted and this cannot be done in the default implementation
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package org.cryptomator.filesystem;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class FileSystemVisitor {
|
||||
|
||||
private final Consumer<Folder> beforeFolderVisitor;
|
||||
private final Consumer<Folder> afterFolderVisitor;
|
||||
private final Consumer<File> fileVisitor;
|
||||
|
||||
private FileSystemVisitor(Consumer<Folder> beforeFolderVisitor, Consumer<Folder> afterFolderVisitor, Consumer<File> fileVisitor) {
|
||||
this.beforeFolderVisitor = beforeFolderVisitor;
|
||||
this.afterFolderVisitor = afterFolderVisitor;
|
||||
this.fileVisitor = fileVisitor;
|
||||
}
|
||||
|
||||
public static FileSystemVisitorBuilder fileSystemVisitor() {
|
||||
return new FileSystemVisitorBuilder();
|
||||
}
|
||||
|
||||
public FileSystemVisitor visit(Folder folder) {
|
||||
beforeFolderVisitor.accept(folder);
|
||||
folder.folders().forEach(this::visit);
|
||||
folder.files().forEach(this::visit);
|
||||
afterFolderVisitor.accept(folder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FileSystemVisitor visit(File file) {
|
||||
fileVisitor.accept(file);
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class FileSystemVisitorBuilder {
|
||||
|
||||
private Consumer<Folder> beforeFolderVisitor = noOp();
|
||||
private Consumer<Folder> afterFolderVisitor = noOp();
|
||||
private Consumer<File> fileVisitor = noOp();
|
||||
|
||||
private FileSystemVisitorBuilder() {
|
||||
}
|
||||
|
||||
public FileSystemVisitorBuilder beforeFolder(Consumer<Folder> beforeFolderVisitor) {
|
||||
if (beforeFolderVisitor == null) {
|
||||
throw new IllegalArgumentException("Vistior may not be null");
|
||||
}
|
||||
this.beforeFolderVisitor = beforeFolderVisitor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FileSystemVisitorBuilder afterFolder(Consumer<Folder> afterFolderVisitor) {
|
||||
if (afterFolderVisitor == null) {
|
||||
throw new IllegalArgumentException("Vistior may not be null");
|
||||
}
|
||||
this.afterFolderVisitor = afterFolderVisitor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FileSystemVisitorBuilder forEachFile(Consumer<File> fileVisitor) {
|
||||
if (fileVisitor == null) {
|
||||
throw new IllegalArgumentException("Vistior may not be null");
|
||||
}
|
||||
this.fileVisitor = fileVisitor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FileSystemVisitor visit(Folder folder) {
|
||||
return build().visit(folder);
|
||||
}
|
||||
|
||||
public FileSystemVisitor visit(File file) {
|
||||
return build().visit(file);
|
||||
}
|
||||
|
||||
public FileSystemVisitor build() {
|
||||
return new FileSystemVisitor(beforeFolderVisitor, afterFolderVisitor, fileVisitor);
|
||||
}
|
||||
|
||||
private static <T> Consumer<T> noOp() {
|
||||
return ignoredParameter -> {
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,17 +88,7 @@ public interface Folder extends Node {
|
||||
* <p>
|
||||
* If the directory does not exist this method does nothing.
|
||||
*/
|
||||
default void delete() throws UncheckedIOException {
|
||||
if (!exists()) {
|
||||
return;
|
||||
}
|
||||
folders().forEach(Folder::delete);
|
||||
files().forEach(file -> {
|
||||
try (WritableFile writableFile = file.openWritable()) {
|
||||
writableFile.delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
void delete();
|
||||
|
||||
/**
|
||||
* Moves this directory and its contents to the given destination. If the
|
||||
|
||||
Reference in New Issue
Block a user