Skip to content

Commit 4f67c39

Browse files
authored
#94: improve documentation and logging (#242)
1 parent 1cab8d4 commit 4f67c39

File tree

3 files changed

+228
-200
lines changed

3 files changed

+228
-200
lines changed

cli/src/main/java/com/devonfw/tools/ide/io/FileAccess.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.devonfw.tools.ide.io;
22

33
import java.nio.file.Path;
4+
import java.util.List;
45
import java.util.function.Consumer;
56
import java.util.function.Predicate;
67

@@ -213,4 +214,13 @@ default void extract(Path archiveFile, Path targetDir, Consumer<Path> postExtrac
213214
*/
214215
Path findFirst(Path dir, Predicate<Path> filter, boolean recursive);
215216

217+
/**
218+
* @param dir the {@link Path} to the directory where to list the children.
219+
* @param filter the {@link Predicate} used to {@link Predicate#test(Object) decide} which children to include (if
220+
* {@code true} is returned).
221+
* @return all children of the given {@link Path} that match the given {@link Predicate}. Will be the empty list of
222+
* the given {@link Path} is not an existing directory.
223+
*/
224+
List<Path> listChildren(Path dir, Predicate<Path> filter);
225+
216226
}

cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,27 @@ private Path findFirstRecursive(Path dir, Predicate<Path> filter, boolean recurs
763763
return null;
764764
}
765765

766+
@Override
767+
public List<Path> listChildren(Path dir, Predicate<Path> filter) {
768+
769+
if (!Files.isDirectory(dir)) {
770+
return List.of();
771+
}
772+
List<Path> children = new ArrayList<>();
773+
try (Stream<Path> childStream = Files.list(dir)) {
774+
Iterator<Path> iterator = childStream.iterator();
775+
while (iterator.hasNext()) {
776+
Path child = iterator.next();
777+
if (filter.test(child)) {
778+
this.context.trace("Accepted file {}", child);
779+
children.add(child);
780+
} else {
781+
this.context.trace("Ignoring file {} according to filter", child);
782+
}
783+
}
784+
} catch (IOException e) {
785+
throw new IllegalStateException("Failed to find children of directory " + dir, e);
786+
}
787+
return children;
788+
}
766789
}

0 commit comments

Comments
 (0)