forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNodeFilter.java
More file actions
101 lines (90 loc) · 3.53 KB
/
TreeNodeFilter.java
File metadata and controls
101 lines (90 loc) · 3.53 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
package act.cli.tree;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.osgl.$;
import org.osgl.util.S;
import java.util.List;
/**
* A {@code TreeNodeFilter} can be applied to a {@link TreeNode} and
* check if it applied to the filter
*/
public abstract class TreeNodeFilter extends $.F2<List<? extends TreeNode>, TreeNode, Boolean> {
/**
* Apply the filter on a {@code TreeNode} and check the result.
* <p>This method will call the {@link #test(List, TreeNode)} to get the result</p>
* @param path a list of tree node that are ancestors of the node
* @param theNode the tree node to be evaluated
* @return {@code true} if the node applied to the filter or {@code false} otherwise
*/
@Override
public final Boolean apply(List<? extends TreeNode> path, TreeNode theNode) {
return test(path, theNode);
}
/**
* Sub class should implement the filter logic in this method
* @param path a list of tree node that are ancestors of the node
* @param theNode the tree node to be evaluated
* @return {@code true} if the node applied to the filter or {@code false} otherwise
*/
protected abstract boolean test(List<? extends TreeNode> path, TreeNode theNode);
public enum Common {
;
private static String path(List<? extends TreeNode> context, TreeNode theNode) {
S.Buffer sb = S.buffer();
for (TreeNode n : context) {
sb.append(n.id()).append("/");
}
sb.append(theNode.label());
return sb.toString();
}
public static TreeNodeFilter pathMatches(final String pattern) {
return new TreeNodeFilter() {
@Override
protected boolean test(List<? extends TreeNode> path, TreeNode theNode) {
return path(path, theNode).matches(pattern);
}
};
}
public static TreeNodeFilter pathMatches(final $.Function<String, Boolean> predicate) {
return new TreeNodeFilter() {
@Override
protected boolean test(List<? extends TreeNode> path, TreeNode theNode) {
return predicate.apply(path(path, theNode));
}
};
}
public static TreeNodeFilter labelMatches(final String pattern) {
return new TreeNodeFilter() {
@Override
protected boolean test(List<? extends TreeNode> path, TreeNode theNode) {
return theNode.label().matches(pattern);
}
};
}
public static TreeNodeFilter labelMatches(final $.Function<String, Boolean> predicate) {
return new TreeNodeFilter() {
@Override
protected boolean test(List<? extends TreeNode> path, TreeNode theNode) {
return predicate.apply(theNode.label());
}
};
}
}
}