Skip to content

Commit 351b2d0

Browse files
Rework PathUtils/PathPredicates
1 parent 49c07dc commit 351b2d0

File tree

14 files changed

+255
-439
lines changed

14 files changed

+255
-439
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Added
1212
- New path predicates: `hasParentMatching`, `hasAncestorMatching`, `hasDirectChildMatching`, `hasDescendantMatching`, `hasSiblingMatching`
1313

14+
### Changed
15+
- `PathUtils` removed, `PathPredicates`rework
16+
1417
---
1518
## [0.0.4] - 2025-09-27
1619

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,14 @@ child_limit_static/
178178
Or you can also set a limitation function, to dynamically choose the number of children displayed in each directory.
179179
It avoids cluttering the whole console with known large folders (e.g. `node_modules`) but continue to pretty print normally other folders.
180180

181-
Use the `ChildLimitBuilder` and `PathPredicates` classes to help you build the limit function that fits your needs..
181+
Use the `ChildLimitBuilder` and `PathPredicates` classes to help you build the limit function that fits your needs.
182182

183183
```java
184184
// Example: ChildLimitDynamic.java
185+
var isNodeModulePredicate = PathPredicates.builder().hasName("node_modules").build();
185186
var childLimit = ChildLimitBuilder.builder()
186187
.defaultLimit(ChildLimitBuilder.UNLIMITED)
187-
.limit(PathPredicates.hasName("node_modules"), 0)
188+
.limit(isNodeModulePredicate, 0)
188189
.build();
189190
var prettyPrinter = FileTreePrettyPrinter.builder()
190191
.customizeOptions(options -> options.withChildLimit(childLimit))
@@ -278,12 +279,13 @@ Files and directories can be selectively included or excluded using a custom `Pr
278279
Filtering is **recursive by default**: directory's contents will always be traversed.
279280
However, if a directory does not match and none of its children match, the directory itself will not be displayed.
280281

281-
The `PathPredicates` class provides several ready-to-use `Predicate<Path>` implementations for common cases, as well as a builder for creating more advanced predicates.
282+
The `PathPredicates` class provides several ready-to-use methods for creating common predicates, as well as a builder for creating more advanced predicates.
282283

283284
```java
284285
// Example: Filtering.java
286+
var hasJavaExtensionPredicate = PathPredicates.builder().hasExtension("java").build();
285287
var prettyPrinter = FileTreePrettyPrinter.builder()
286-
.customizeOptions(options -> options.filter(PathPredicates.hasExtension("java")))
288+
.customizeOptions(options -> options.filter(hasJavaExtensionPredicate))
287289
.build();
288290
```
289291
```
@@ -308,16 +310,16 @@ If the function returns `null`, nothing is added.
308310
```java
309311
// Example: LineExtension.java
310312
Function<Path, String> lineExtension = path -> {
311-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "api")) {
313+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "api")) {
312314
return "\t\t\t// All API code: controllers, etc.";
313315
}
314-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "domain")) {
316+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "domain")) {
315317
return "\t\t\t// All domain code: value objects, etc.";
316318
}
317-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "infra")) {
319+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "infra")) {
318320
return "\t\t\t// All infra code: database, email service, etc.";
319321
}
320-
if (PathUtils.isFile(path) && PathUtils.hasName(path, "application.properties")) {
322+
if (PathPredicates.isFile(path) && PathPredicates.hasName(path, "application.properties")) {
321323
return "\t// Config file";
322324
}
323325
return null;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
public class ChildLimitDynamic {
88

99
public static void main(String[] args) {
10+
var isNodeModulePredicate = PathPredicates.builder().hasName("node_modules").build();
1011
var childLimit = ChildLimitBuilder.builder()
1112
.defaultLimit(ChildLimitBuilder.UNLIMITED)
12-
.limit(PathPredicates.hasName("node_modules"), 0)
13+
.limit(isNodeModulePredicate, 0)
1314
.build();
1415
var prettyPrinter = FileTreePrettyPrinter.builder()
1516
.customizeOptions(options -> options.withChildLimit(childLimit))

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
public class Filtering {
77

88
public static void main(String[] args) {
9+
var hasJavaExtensionPredicate = PathPredicates.builder().hasExtension("java").build();
910
var prettyPrinter = FileTreePrettyPrinter.builder()
10-
.customizeOptions(options -> options.filter(PathPredicates.hasExtension("java")))
11+
.customizeOptions(options -> options.filter(hasJavaExtensionPredicate))
1112
.build();
1213

1314
var tree = prettyPrinter.prettyPrint("src/example/resources/filtering");

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package io.github.computerdaddyguy.jfiletreeprettyprinter.example;
22

33
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
4-
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathUtils;
4+
import io.github.computerdaddyguy.jfiletreeprettyprinter.PathPredicates;
55
import java.nio.file.Path;
66
import java.util.function.Function;
77

88
public class LineExtension {
99

1010
public static void main(String[] args) {
1111
Function<Path, String> lineExtension = path -> {
12-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "api")) {
12+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "api")) {
1313
return "\t\t\t// All API code: controllers, etc.";
1414
}
15-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "domain")) {
15+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "domain")) {
1616
return "\t\t\t// All domain code: value objects, etc.";
1717
}
18-
if (PathUtils.isDirectory(path) && PathUtils.hasName(path, "infra")) {
18+
if (PathPredicates.isDirectory(path) && PathPredicates.hasName(path, "infra")) {
1919
return "\t\t\t// All infra code: database, email service, etc.";
2020
}
21-
if (PathUtils.isFile(path) && PathUtils.hasName(path, "application.properties")) {
21+
if (PathPredicates.isFile(path) && PathPredicates.hasName(path, "application.properties")) {
2222
return "\t// Config file";
2323
}
2424
return null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* <pre>{@code
2727
* var childLimit = ChildLimitBuilder.builder()
2828
* .defaultLimit(ChildLimit.UNLIMITED) // unlimited unless specified
29-
* .limit(path -> PathUtils.hasName("bigDir"), 10) // max 10 children in "bigDir"
30-
* .limit(path -> PathUtils.hasName("emptyDir"), 0) // disallow children in "emptyDir"
29+
* .limit(path -> PathPredicates.hasName(path, "bigDir"), 10) // max 10 children in "bigDir"
30+
* .limit(path -> PathPredicates.hasName(path, "emptyDir"), 0) // disallow children in "emptyDir"
3131
* .build();
3232
*
3333
* }</pre>

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public PathPredicateBuilder fileTest(Predicate<File> predicate) {
8686
* @return this builder for chaining
8787
*/
8888
public PathPredicateBuilder hasName(String name) {
89-
return pathTest(PathPredicates.hasName(name));
89+
Objects.requireNonNull(name, "name is null");
90+
return pathTest(path -> PathPredicates.hasName(path, name));
9091
}
9192

9293
/**
@@ -98,7 +99,8 @@ public PathPredicateBuilder hasName(String name) {
9899
* @return this builder for chaining
99100
*/
100101
public PathPredicateBuilder hasNameIgnoreCase(String name) {
101-
return pathTest(PathPredicates.hasNameIgnoreCase(name));
102+
Objects.requireNonNull(name, "name is null");
103+
return pathTest(path -> PathPredicates.hasNameIgnoreCase(path, name));
102104
}
103105

104106
/**
@@ -109,7 +111,8 @@ public PathPredicateBuilder hasNameIgnoreCase(String name) {
109111
* @return this builder for chaining
110112
*/
111113
public PathPredicateBuilder hasNameMatching(Pattern pattern) {
112-
return pathTest(PathPredicates.hasNameMatching(pattern));
114+
Objects.requireNonNull(pattern, "pattern is null");
115+
return pathTest(path -> PathPredicates.hasNameMatching(path, pattern));
113116
}
114117

115118
/**
@@ -120,7 +123,8 @@ public PathPredicateBuilder hasNameMatching(Pattern pattern) {
120123
* @return this builder for chaining
121124
*/
122125
public PathPredicateBuilder hasNameEndingWith(String suffix) {
123-
return pathTest(PathPredicates.hasNameEndingWith(suffix));
126+
Objects.requireNonNull(suffix, "suffix is null");
127+
return pathTest(path -> PathPredicates.hasNameEndingWith(path, suffix));
124128
}
125129

126130
/**
@@ -135,7 +139,8 @@ public PathPredicateBuilder hasNameEndingWith(String suffix) {
135139
* @return this builder for chaining
136140
*/
137141
public PathPredicateBuilder hasExtension(String extension) {
138-
return pathTest(PathPredicates.hasExtension(extension));
142+
Objects.requireNonNull(extension, "extension is null");
143+
return pathTest(path -> PathPredicates.hasExtension(path, extension));
139144
}
140145

141146
// ---------- Type ----------
@@ -146,7 +151,7 @@ public PathPredicateBuilder hasExtension(String extension) {
146151
* @return this builder for chaining
147152
*/
148153
public PathPredicateBuilder isDirectory() {
149-
return pathTest(PathPredicates.isDirectory());
154+
return pathTest(path -> PathPredicates.isDirectory(path));
150155
}
151156

152157
/**
@@ -155,7 +160,7 @@ public PathPredicateBuilder isDirectory() {
155160
* @return this builder for chaining
156161
*/
157162
public PathPredicateBuilder isFile() {
158-
return pathTest(PathPredicates.isFile());
163+
return pathTest(path -> PathPredicates.isFile(path));
159164
}
160165

161166
// ---------- Hierarchy ----------
@@ -170,7 +175,8 @@ public PathPredicateBuilder isFile() {
170175
* @see PathPredicates#hasParentMatching(Predicate)
171176
*/
172177
public PathPredicateBuilder hasParentMatching(Predicate<Path> parentPredicate) {
173-
return pathTest(PathPredicates.hasParentMatching(parentPredicate));
178+
Objects.requireNonNull(parentPredicate, "parentPredicate is null");
179+
return pathTest(path -> PathPredicates.hasParentMatching(path, parentPredicate));
174180
}
175181

176182
/**
@@ -186,7 +192,8 @@ public PathPredicateBuilder hasParentMatching(Predicate<Path> parentPredicate) {
186192
* @see PathPredicates#hasAncestorMatching(Predicate)
187193
*/
188194
public PathPredicateBuilder hasAncestorMatching(Predicate<Path> ancestorPredicate) {
189-
return pathTest(PathPredicates.hasAncestorMatching(ancestorPredicate));
195+
Objects.requireNonNull(ancestorPredicate, "ancestorPredicate is null");
196+
return pathTest(path -> PathPredicates.hasAncestorMatching(path, ancestorPredicate));
190197
}
191198

192199
/**
@@ -202,7 +209,8 @@ public PathPredicateBuilder hasAncestorMatching(Predicate<Path> ancestorPredicat
202209
* @see PathPredicates#hasDirectChildMatching(Predicate)
203210
*/
204211
public PathPredicateBuilder hasDirectChildMatching(Predicate<Path> childPredicate) {
205-
return pathTest(PathPredicates.hasDirectChildMatching(childPredicate));
212+
Objects.requireNonNull(childPredicate, "childPredicate is null");
213+
return pathTest(path -> PathPredicates.hasDirectChildMatching(path, childPredicate));
206214
}
207215

208216
/**
@@ -218,7 +226,8 @@ public PathPredicateBuilder hasDirectChildMatching(Predicate<Path> childPredicat
218226
* @see PathPredicates#hasDescendantMatching(Predicate)
219227
*/
220228
public PathPredicateBuilder hasDescendantMatching(Predicate<Path> descendantPredicate) {
221-
return pathTest(PathPredicates.hasDescendantMatching(descendantPredicate));
229+
Objects.requireNonNull(descendantPredicate, "descendantPredicate is null");
230+
return pathTest(path -> PathPredicates.hasDescendantMatching(path, descendantPredicate));
222231
}
223232

224233
/**
@@ -234,7 +243,8 @@ public PathPredicateBuilder hasDescendantMatching(Predicate<Path> descendantPred
234243
* @see PathPredicates#hasSiblingMatching(Predicate)
235244
*/
236245
public PathPredicateBuilder hasSiblingMatching(Predicate<Path> siblingPredicate) {
237-
return pathTest(PathPredicates.hasSiblingMatching(siblingPredicate));
246+
Objects.requireNonNull(siblingPredicate, "siblingPredicate is null");
247+
return pathTest(path -> PathPredicates.hasSiblingMatching(path, siblingPredicate));
238248
}
239249

240250
}

0 commit comments

Comments
 (0)