-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildLimitBuilder.java
More file actions
114 lines (101 loc) · 3.3 KB
/
ChildLimitBuilder.java
File metadata and controls
114 lines (101 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package io.github.computerdaddyguy.jfiletreeprettyprinter;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.ToIntFunction;
import org.jspecify.annotations.NullMarked;
/**
* A builder for creating a {@code ToIntFunction<Path>} that defines
* how many child entries (files or directories) are allowed under a given path.
* <p>
* The resulting function evaluates added rules in the order they were defined.
* The first matching rule determines the child limit for a given path.
* If no rules match, the default limit is applied.
* </p>
*
* <p>
* A limit of {@link #UNLIMITED} ({@code -1}) means no restriction.
* A limit of {@code 0} means the directory cannot have any children.
* </p>
*
* <h2>Example usage:</h2>
* <pre>{@code
* var childLimit = ChildLimitBuilder.builder()
* .defaultLimit(ChildLimit.UNLIMITED) // unlimited unless specified
* .limit(PathMatchers.hasName("bigDir"), 10) // max 10 children in "bigDir"
* .limit(PathMatchers.hasName("emptyDir"), 0) // disallow children in "emptyDir"
* .build();
*
* }</pre>
*/
@NullMarked
public class ChildLimitBuilder {
/**
* Unlimited children.
*/
public static final int UNLIMITED = -1;
private static final ChildControl UNLIMITED_CONTROL = new ChildControl(p -> true, UNLIMITED);
private List<ChildControl> controls;
private ChildControl defaultControl;
/**
* Creates a new builder.
*/
private ChildLimitBuilder() {
this.controls = new ArrayList<>();
this.defaultControl = UNLIMITED_CONTROL;
}
private record ChildControl(PathMatcher pathMatcher, int limit) {
}
public static ChildLimitBuilder builder() {
return new ChildLimitBuilder();
}
/**
* Builds the child limit function based on the configured rules.
* <p>
* Rules are tested in the order they were added. The first matching rule
* provides the limit. If no rule matches, the default limit is used.
* </p>
*
* @return a function mapping a {@link Path} to its maximum number of children
*/
public ToIntFunction<Path> build() {
var immutControls = List.copyOf(controls);
var immutDefaultControl = this.defaultControl;
return p -> immutControls.stream()
.filter(control -> control.pathMatcher().matches(p))
.findFirst()
.orElse(immutDefaultControl)
.limit();
}
/**
* Sets the default child limit to apply when no specific rule matches.
*
* @param limit the default limit (use {@link #UNLIMITED} for no restriction)
*
* @return this builder for chaining
*/
public ChildLimitBuilder defaultLimit(int limit) {
this.defaultControl = new ChildControl(p -> true, limit);
return this;
}
/**
* Adds a child limit rule for paths matching the given matcher.
* <p>
* Rules are evaluated in the order they are added. The first matching rule wins.
* </p>
*
* @param pathMatcher the condition for paths
* @param limit the maximum number of children (use {@link #UNLIMITED} for no restriction)
*
* @return this builder for chaining
*
* @throws NullPointerException if {@code pathMatcher} is null
*/
public ChildLimitBuilder limit(PathMatcher pathMatcher, int limit) {
Objects.requireNonNull(pathMatcher, "pathMatcher is null");
this.controls.add(new ChildControl(pathMatcher, limit));
return this;
}
}