11package io .github .computerdaddyguy .jfiletreeprettyprinter .scanner ;
22
3+ import io .github .computerdaddyguy .jfiletreeprettyprinter .PathPredicates ;
34import io .github .computerdaddyguy .jfiletreeprettyprinter .scanner .TreeEntry .DirectoryEntry ;
45import io .github .computerdaddyguy .jfiletreeprettyprinter .scanner .TreeEntry .FileEntry ;
56import io .github .computerdaddyguy .jfiletreeprettyprinter .scanner .TreeEntry .MaxDepthReachEntry ;
@@ -30,18 +31,18 @@ public DefaultPathToTreeScanner(ScanningOptions options) {
3031
3132 @ Override
3233 public TreeEntry scan (Path fileOrDir ) {
33- return handle (0 , fileOrDir , options .pathFilter ());
34+ return handle (fileOrDir , 0 , fileOrDir . relativize ( fileOrDir ). resolve ( "." ) , options .pathFilter ());
3435 }
3536
3637 @ Nullable
37- private TreeEntry handle (int depth , Path fileOrDir , Predicate <Path > filter ) {
38- return fileOrDir . toFile (). isDirectory ()
39- ? handleDirectory (depth , fileOrDir , filter )
38+ private TreeEntry handle (Path root , int depth , Path fileOrDir , Predicate <Path > filter ) {
39+ return PathPredicates . isDirectory (fileOrDir )
40+ ? handleDirectory (root , depth , fileOrDir , filter )
4041 : handleFile (fileOrDir );
4142 }
4243
4344 @ Nullable
44- private TreeEntry handleDirectory (int depth , Path dir , Predicate <Path > filter ) {
45+ private TreeEntry handleDirectory (Path root , int depth , Path dir , Predicate <Path > filter ) {
4546
4647 if (depth >= options .getMaxDepth ()) {
4748 var maxDepthEntry = new MaxDepthReachEntry (depth );
@@ -50,17 +51,17 @@ private TreeEntry handleDirectory(int depth, Path dir, Predicate<Path> filter) {
5051
5152 List <TreeEntry > childEntries ;
5253
53- try (var childrenStream = Files .newDirectoryStream (dir )) {
54+ try (var childrenStream = Files .newDirectoryStream (dir , path -> filter . test ( path ) )) {
5455 var it = directoryStreamToIterator (childrenStream , filter );
55- childEntries = handleDirectoryChildren (depth , dir , it , filter );
56+ childEntries = handleDirectoryChildren (root , depth , dir , it , filter );
5657 } catch (IOException e ) {
5758 throw new UncheckedIOException ("Unable to list files for directory: " + dir , e );
5859 }
5960
6061 return new DirectoryEntry (dir , childEntries );
6162 }
6263
63- private List <TreeEntry > handleDirectoryChildren (int depth , Path dir , Iterator <Path > pathIterator , Predicate <Path > filter ) {
64+ private List <TreeEntry > handleDirectoryChildren (Path root , int depth , Path dir , Iterator <Path > pathIterator , Predicate <Path > filter ) {
6465
6566 var childEntries = new ArrayList <TreeEntry >();
6667 int maxChildEntries = options .getChildLimit ().applyAsInt (dir );
@@ -73,7 +74,7 @@ private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Pa
7374 break ;
7475 }
7576 var child = pathIterator .next ();
76- var childEntry = handle (depth + 1 , child , filter );
77+ var childEntry = handle (root , depth + 1 , child , filter );
7778 if (childEntry == null ) {
7879 childCount --; // The child did not pass the filter, so it doesn't count
7980 } else {
@@ -83,19 +84,19 @@ private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Pa
8384
8485 // Loop has early exit?
8586 if (pathIterator .hasNext ()) {
86- childEntries .addAll (handleLeftOverChildren (depth , pathIterator , filter ));
87+ childEntries .addAll (handleLeftOverChildren (root , depth , pathIterator , filter ));
8788 }
8889
8990 return childEntries ;
9091 }
9192
92- private List <TreeEntry > handleLeftOverChildren (int depth , Iterator <Path > pathIterator , Predicate <Path > filter ) {
93+ private List <TreeEntry > handleLeftOverChildren (Path root , int depth , Iterator <Path > pathIterator , Predicate <Path > filter ) {
9394 var childEntries = new ArrayList <TreeEntry >();
9495
9596 var skippedChildren = new ArrayList <Path >();
9697 while (pathIterator .hasNext ()) {
9798 var child = pathIterator .next ();
98- var childEntry = handle (depth + 1 , child , filter );
99+ var childEntry = handle (root , depth + 1 , child , filter );
99100 if (childEntry != null ) { // Is null if no children file is retained by filter
100101 skippedChildren .add (child );
101102 }
@@ -111,7 +112,6 @@ private List<TreeEntry> handleLeftOverChildren(int depth, Iterator<Path> pathIte
111112 private Iterator <Path > directoryStreamToIterator (DirectoryStream <Path > childrenStream , Predicate <Path > filter ) {
112113 return StreamSupport
113114 .stream (childrenStream .spliterator (), false )
114- .filter (filter )
115115 .sorted (options .pathComparator ())
116116 .iterator ();
117117 }
0 commit comments