Skip to content

Commit

Permalink
Merge pull request #5259 from bjhargrave/filetree-methods
Browse files Browse the repository at this point in the history
io: Make FileTree predicate methods public since they can be useful
  • Loading branch information
bjhargrave committed May 23, 2022
2 parents ebe59ed + 33de246 commit cff2231
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions aQute.libg/src/aQute/lib/io/FileTree.java
@@ -1,5 +1,7 @@
package aQute.lib.io;

import static java.util.Objects.requireNonNull;

import java.io.File;
import java.nio.file.Path;
import java.text.CollationKey;
Expand Down Expand Up @@ -103,7 +105,16 @@ public List<File> getFiles(File baseDir, List<String> defaultIncludes) {
return getFiles(baseDir, files.isEmpty() ? paths.matches(defaultIncludes) : paths.matches());
}

private List<File> getFiles(File baseDir, Predicate<String> matches) {
/**
* Return a list of files using the specified baseDir and the specified
* relative path matching predicate.
*
* @param baseDir The base directory for locating files.
* @param matches The path matching predicate. The predicate only matches
* against the relative path from the specified baseDir.
* @return A list of files.
*/
public List<File> getFiles(File baseDir, Predicate<String> matches) {
ArrayList<File> result = new ArrayList<>();
new FileTreeSpliterator(baseDir, matches, files).forEachRemaining(result::add);
result.trimToSize();
Expand Down Expand Up @@ -136,7 +147,16 @@ public Stream<File> stream(File baseDir, List<String> defaultIncludes) {
return stream(baseDir, files.isEmpty() ? paths.matches(defaultIncludes) : paths.matches());
}

private Stream<File> stream(File baseDir, Predicate<String> matches) {
/**
* Return a stream of files using the specified baseDir and the specified
* relative path matching predicate.
*
* @param baseDir The base directory for locating files.
* @param matches The path matching predicate. The predicate only matches
* against the relative path from the specified baseDir.
* @return A stream of files.
*/
public Stream<File> stream(File baseDir, Predicate<String> matches) {
return StreamSupport.stream(new FileTreeSpliterator(baseDir, matches, files), false);
}

Expand All @@ -154,7 +174,7 @@ final static class FileTreeSpliterator extends AbstractSpliterator<File> {
super(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.DISTINCT | Spliterator.NONNULL);
basePath = baseDir.toPath();
this.matches = matches;
this.matches = requireNonNull(matches);
List<File> copy = new ArrayList<>(files); // mutable copy
this.files = copy;
extra = copy.spliterator(); // late-binding
Expand Down
2 changes: 1 addition & 1 deletion aQute.libg/src/aQute/lib/io/package-info.java
@@ -1,4 +1,4 @@
@Version("4.2.0")
@Version("4.3.0")
package aQute.lib.io;

import org.osgi.annotation.versioning.Version;

0 comments on commit cff2231

Please sign in to comment.