Skip to content

Commit 3f02200

Browse files
Fix Sonar issues
1 parent 9b4a166 commit 3f02200

File tree

15 files changed

+31
-45
lines changed

15 files changed

+31
-45
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathPredicates;
55
import java.nio.file.Path;
6-
import java.util.function.Function;
6+
import java.util.function.ToIntFunction;
77

88
public class ChildrenLimitDynamic {
99

1010
public static void main(String[] args) {
11-
Function<Path, Integer> pathLimitFunction = path -> PathPredicates.hasName(path, "node_modules") ? 0 : -1; // Negative value means "no limit"
11+
ToIntFunction<Path> pathLimitFunction = path -> PathPredicates.hasName(path, "node_modules") ? 0 : -1; // Negative value means "no limit"
1212
var prettyPrinter = FileTreePrettyPrinter.builder()
1313
.customizeOptions(options -> options.withChildrenLimitFunction(pathLimitFunction))
1414
.build();

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/FileTreePrettyPrinterBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io.github.computerdaddyguy.jfiletreeprettyprinter.renderer.TreeEntryRenderer;
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.scanner.PathToTreeScanner;
55
import java.util.Objects;
6-
import java.util.function.Function;
6+
import java.util.function.UnaryOperator;
77
import org.jspecify.annotations.NullMarked;
88

99
@NullMarked
@@ -22,7 +22,7 @@ public FileTreePrettyPrinterBuilder withOptions(PrettyPrintOptions options) {
2222
return this;
2323
}
2424

25-
public FileTreePrettyPrinterBuilder customizeOptions(Function<PrettyPrintOptions, PrettyPrintOptions> optionsCustomizer) {
25+
public FileTreePrettyPrinterBuilder customizeOptions(UnaryOperator<PrettyPrintOptions> optionsCustomizer) {
2626
Objects.requireNonNull(optionsCustomizer, "optionsCustomizer is null");
2727
var newOptions = optionsCustomizer.apply(this.options);
2828
this.options = Objects.requireNonNull(newOptions, "new options after customization is null");

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/PrettyPrintOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import io.github.computerdaddyguy.jfiletreeprettyprinter.scanner.ScanningOptions;
55
import java.nio.file.Path;
66
import java.util.Objects;
7-
import java.util.function.Function;
7+
import java.util.function.ToIntFunction;
88
import org.jspecify.annotations.NullMarked;
99

1010
@NullMarked
1111
public class PrettyPrintOptions implements ScanningOptions, RenderingOptions {
1212

13-
private Function<Path, Integer> childrenLimitFunction = p -> -1;
13+
private ToIntFunction<Path> childrenLimitFunction = p -> -1;
1414

1515
private TreeFormat treeFormat = TreeFormat.UNICODE_BOX_DRAWING;
1616
private boolean emojis = false;
@@ -31,7 +31,7 @@ public static PrettyPrintOptions createDefault() {
3131
// ----------------------------------------------
3232

3333
@Override
34-
public Function<Path, Integer> getChildrenLimitFunction() {
34+
public ToIntFunction<Path> getChildrenLimitFunction() {
3535
return childrenLimitFunction;
3636
}
3737

@@ -52,7 +52,7 @@ public PrettyPrintOptions withChildrenLimit(int childrenLimit) {
5252
*
5353
* @param childrenLimitFunction The dynamic limitation function, cannot be <code>null</code>. A negative computed value means no limit.
5454
*/
55-
public PrettyPrintOptions withChildrenLimitFunction(Function<Path, Integer> childrenLimitFunction) {
55+
public PrettyPrintOptions withChildrenLimitFunction(ToIntFunction<Path> childrenLimitFunction) {
5656
this.childrenLimitFunction = Objects.requireNonNull(childrenLimitFunction, "childrenLimitFunction is null");
5757
return this;
5858
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public void renderTree(TreeEntry entry, Depth depth, StringBuilder buff) {
4747

4848
private void renderDirectory(StringBuilder buff, Depth depth, DirectoryEntry dirEntry, List<Path> compactPaths) {
4949

50-
if (options.areCompactDirectoriesUsed()) {
51-
if (dirEntry.getEntries().size() == 1 && dirEntry.getEntries().get(0) instanceof DirectoryEntry childDirEntry) {
52-
var newCompactPaths = new ArrayList<>(compactPaths);
53-
newCompactPaths.add(childDirEntry.getDir());
54-
renderDirectory(buff, depth, childDirEntry, newCompactPaths);
55-
return;
56-
}
50+
if (options.areCompactDirectoriesUsed()
51+
&& dirEntry.getEntries().size() == 1
52+
&& dirEntry.getEntries().get(0) instanceof DirectoryEntry childDirEntry) {
53+
var newCompactPaths = new ArrayList<>(compactPaths);
54+
newCompactPaths.add(childDirEntry.getDir());
55+
renderDirectory(buff, depth, childDirEntry, newCompactPaths);
56+
return;
5757
}
5858

5959
buff.append(lineRenderer.renderDirectoryBegin(depth, dirEntry, compactPaths));

src/main/java/io/github/computerdaddyguy/jfiletreeprettyprinter/renderer/depth/DefaultDepthFormatter.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ public String format(Depth depth) {
3636
var it = depth.getSymbols().iterator();
3737
while (it.hasNext()) {
3838
var symbol = it.next();
39-
if (symbol == DepthSymbol.LAST_FILE) {
40-
if (it.hasNext()) {
41-
symbol = DepthSymbol.NONE;
42-
}
43-
} else if (symbol == DepthSymbol.NON_LAST_FILE) {
44-
if (it.hasNext()) {
45-
symbol = DepthSymbol.SKIP;
46-
}
39+
if (symbol == DepthSymbol.LAST_FILE && it.hasNext()) {
40+
symbol = DepthSymbol.NONE;
41+
} else if (symbol == DepthSymbol.NON_LAST_FILE && it.hasNext()) {
42+
symbol = DepthSymbol.SKIP;
4743
}
4844
buff.append(format(symbol));
4945
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private TreeEntry handleDirectory(int depth, Path dir) {
4343
}
4444

4545
var childrenEntries = new ArrayList<TreeEntry>();
46-
int maxChildrenEntries = options.getChildrenLimitFunction().apply(dir);
46+
int maxChildrenEntries = options.getChildrenLimitFunction().applyAsInt(dir);
4747

4848
try (var childrenStream = Files.newDirectoryStream(dir)) {
4949
var it = childrenStream.iterator();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter.scanner;
22

33
import java.nio.file.Path;
4-
import java.util.function.Function;
4+
import java.util.function.ToIntFunction;
55
import org.jspecify.annotations.NullMarked;
66

77
@NullMarked
88
public interface ScanningOptions {
99

1010
int getMaxDepth();
1111

12-
Function<Path, Integer> getChildrenLimitFunction();
12+
ToIntFunction<Path> getChildrenLimitFunction();
1313

1414
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import io.github.computerdaddyguy.jfiletreeprettyprinter.renderer.RenderingOptions.TreeFormat;
66
import java.nio.file.Path;
7-
import java.util.function.Function;
7+
import java.util.function.UnaryOperator;
88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.io.TempDir;
1010

@@ -15,13 +15,13 @@ abstract class AbstractDirectoryPrettyPrintTest {
1515

1616
protected static Path targetPath;
1717

18-
protected FileTreePrettyPrinter customize(Function<PrettyPrintOptions, PrettyPrintOptions> customizer) {
18+
protected FileTreePrettyPrinter customize(UnaryOperator<PrettyPrintOptions> customizer) {
1919
var builder = FileTreePrettyPrinter.builder();
2020
builder.customizeOptions(customizer);
2121
return builder.build();
2222
}
2323

24-
protected void run(Function<PrettyPrintOptions, PrettyPrintOptions> customizer, String expected) {
24+
protected void run(UnaryOperator<PrettyPrintOptions> customizer, String expected) {
2525
var printer = customize(customizer);
2626
var actual = printer.prettyPrint(targetPath);
2727
assertThat(actual).isEqualTo(expected);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter;
22

33
import io.github.computerdaddyguy.jfiletreeprettyprinter.util.FileStructureCreator;
4-
import java.io.IOException;
54
import org.junit.jupiter.api.BeforeAll;
65

76
class Directory_1f_Test extends AbstractDirectoryPrettyPrintTest {
87

98
@BeforeAll
10-
static void setup() throws IOException {
9+
static void setup() {
1110
// @formatter:off
1211
targetPath = FileStructureCreator
1312
.forTargetPath(root)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter;
22

33
import io.github.computerdaddyguy.jfiletreeprettyprinter.util.FileStructureCreator;
4-
import java.io.IOException;
54
import org.junit.jupiter.api.BeforeAll;
65

76
class Directory_2f_Test extends AbstractDirectoryPrettyPrintTest {
87

98
@BeforeAll
10-
static void setup() throws IOException {
9+
static void setup() {
1110
// @formatter:off
1211
targetPath = FileStructureCreator
1312
.forTargetPath(root)

0 commit comments

Comments
 (0)