-
Notifications
You must be signed in to change notification settings - Fork 0
/
InternalTraverser.java
42 lines (35 loc) · 1.35 KB
/
InternalTraverser.java
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
package bibonne.filestree.traverser;
import bibonne.filestree.utils.FilesUtils;
import bibonne.filestree.external.FilesUtilsFromJdkFiles;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.concurrent.StructuredTaskScope;
import java.util.stream.Stream;
record InternalTraverser(BrowseResult result, FilesUtils filesUtils) implements Callable<BrowseResult> {
public InternalTraverser(BrowseResult browseResult){
this(browseResult, new FilesUtilsFromJdkFiles());
}
@Override
public BrowseResult call() {
try(var scope=new StructuredTaskScope.ShutdownOnFailure() ; var files = listAllFiles(result.currentDirectory())){
files.forEach(path -> {
if (isDirectory(path)){
scope.fork(new InternalTraverser(result.child(path), filesUtils));
}else{
result.processFile(path);
}
});
scope.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
return result.aggregate();
}
private boolean isDirectory(Path path) {
return filesUtils.isDirectorySafely(path);
}
private Stream<Path> listAllFiles(Path directory) {
return filesUtils.listSafely(directory);
}
}