Skip to content

Commit 7b49cee

Browse files
Fix sonar
1 parent 5073402 commit 7b49cee

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

src/example/java/io/github/computerdaddyguy/jfiletreeprettyprinter/example/CompleteExample.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public class CompleteExample {
1111

1212
public static void main(String[] args) {
1313

14-
var rootFolder = "JFileTreePrettyPrinter";
15-
1614
var filterDir = PathPredicates.builder()
1715
.pathTest(path -> !PathPredicates.hasName(path, ".git"))
1816
.pathTest(path -> !PathPredicates.hasFullPathMatchingGlob(path, "**/.git"))

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/renderer/DefaultTreeEntryRenderer.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Objects;
1414
import java.util.Optional;
1515
import org.jspecify.annotations.NullMarked;
16+
import org.jspecify.annotations.Nullable;
1617

1718
@NullMarked
1819
class DefaultTreeEntryRenderer implements TreeEntryRenderer {
@@ -41,25 +42,28 @@ private String renderTree(TreeEntry entry, Depth depth) {
4142

4243
private String renderDirectory(Depth depth, DirectoryEntry dirEntry, List<Path> compactPaths) {
4344

44-
Optional<String> extension = null;
45+
boolean extensionEvaluated = false;
46+
String extension = null;
47+
4548
if (options.areCompactDirectoriesUsed()
4649
&& !depth.isRoot()
4750
&& dirEntry.getEntries().size() == 1
4851
&& dirEntry.getEntries().get(0) instanceof DirectoryEntry childDirEntry) {
4952

5053
extension = computeLineExtension(dirEntry.getDir());
51-
if (extension.isEmpty()) {
54+
extensionEvaluated = true;
55+
if (extension == null) {
5256
var newCompactPaths = new ArrayList<>(compactPaths);
5357
newCompactPaths.add(childDirEntry.getDir());
5458
return renderDirectory(depth, childDirEntry, newCompactPaths);
5559
}
5660
}
5761

5862
var line = lineRenderer.renderDirectoryBegin(depth, dirEntry, compactPaths);
59-
if (extension == null) {
63+
if (!extensionEvaluated) {
6064
extension = computeLineExtension(dirEntry.getDir());
6165
}
62-
line += extension.orElse("");
66+
line += Optional.ofNullable(extension).orElse("");
6367

6468
var childIt = dirEntry.getEntries().iterator();
6569

@@ -81,16 +85,17 @@ private String renderDirectory(Depth depth, DirectoryEntry dirEntry, List<Path>
8185
return line + childLines.toString();
8286
}
8387

84-
private Optional<String> computeLineExtension(Path path) {
88+
@Nullable
89+
private String computeLineExtension(Path path) {
8590
if (options.getLineExtension() == null) {
86-
return Optional.empty();
91+
return null;
8792
}
88-
return Optional.ofNullable(options.getLineExtension().apply(path));
93+
return options.getLineExtension().apply(path);
8994
}
9095

9196
private String renderFile(Depth depth, FileEntry fileEntry) {
9297
var line = lineRenderer.renderFile(depth, fileEntry);
93-
line += computeLineExtension(fileEntry.getFile()).orElse("");
98+
line += Optional.ofNullable(computeLineExtension(fileEntry.getFile())).orElse("");
9499
return line;
95100
}
96101

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/scanner/DefaultPathToTreeScanner.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ private TreeEntry handleDirectory(int depth, Path dir, Predicate<Path> filter) {
5757
throw new UncheckedIOException("Unable to list files for directory: " + dir, e);
5858
}
5959

60-
// // Filter is active and no children match
61-
// if (depth > 0 && filter != null && childEntries.isEmpty() && !filter.test(dir)) {
62-
// return null; // Do no show this directory at all
63-
// }
64-
6560
return new DirectoryEntry(dir, childEntries);
6661
}
6762

@@ -97,24 +92,17 @@ private List<TreeEntry> handleDirectoryChildren(int depth, Path dir, Iterator<Pa
9792
private List<TreeEntry> handleLeftOverChildren(int depth, Iterator<Path> pathIterator, Predicate<Path> filter) {
9893
var childEntries = new ArrayList<TreeEntry>();
9994

100-
if (filter == null) {
101-
var skippedChildren = new ArrayList<Path>();
102-
pathIterator.forEachRemaining(skippedChildren::add);
95+
var skippedChildren = new ArrayList<Path>();
96+
while (pathIterator.hasNext()) {
97+
var child = pathIterator.next();
98+
var childEntry = handle(depth + 1, child, filter);
99+
if (childEntry != null) { // Is null if no children file is retained by filter
100+
skippedChildren.add(child);
101+
}
102+
}
103+
if (!skippedChildren.isEmpty()) {
103104
var childrenSkippedEntry = new SkippedChildrenEntry(skippedChildren);
104105
childEntries.add(childrenSkippedEntry);
105-
} else {
106-
var skippedChildren = new ArrayList<Path>();
107-
while (pathIterator.hasNext()) {
108-
var child = pathIterator.next();
109-
var childEntry = handle(depth + 1, child, filter);
110-
if (childEntry != null) { // Is null if no children file is retained by filter
111-
skippedChildren.add(child);
112-
}
113-
}
114-
if (!skippedChildren.isEmpty()) {
115-
var childrenSkippedEntry = new SkippedChildrenEntry(skippedChildren);
116-
childEntries.add(childrenSkippedEntry);
117-
}
118106
}
119107

120108
return childEntries;

src/test/java/io/github/computerdaddyguy/jfiletreeprettyprinter/LineExtensionTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ void compact_dir_first_dir() {
8888
compact_dir(lineExtension, expected);
8989
}
9090

91+
@Test
92+
void compact_dir_empty_string_workds() {
93+
94+
Function<Path, String> lineExtension = p -> {
95+
if (PathPredicates.hasName(p, "dirA")) {
96+
return "";
97+
}
98+
return null;
99+
};
100+
101+
var expected = """
102+
targetPath/
103+
├─ dirA/
104+
│ └─ dirB/dirC/
105+
│ ├─ file1
106+
│ ├─ file2
107+
│ └─ file3
108+
└─ dirX/""";
109+
110+
compact_dir(lineExtension, expected);
111+
}
112+
91113
@Test
92114
void compact_dir_middle_dir() {
93115

0 commit comments

Comments
 (0)