io.github.jinghui70.rainbow.utils.WrapTreeNode Maven / Gradle / Ivy
package io.github.jinghui70.rainbow.utils;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
public class WrapTreeNode extends TreeNode> {
private final T data;
public WrapTreeNode(T data) {
this.data = data;
}
public static List> filter(List> list, Predicate predicate, FilterType filterType) {
List> result = new LinkedList<>();
for (WrapTreeNode wrapNode : list) {
if (predicate.test(wrapNode.data)) {
WrapTreeNode newNode = new WrapTreeNode<>(wrapNode.data);
result.add(newNode);
if (filterType != FilterType.FIRST_MATCH && wrapNode.hasChild()) {
List> newChildren = filterType == FilterType.ALL_MATCH ?
filter(wrapNode.getChildren(), predicate, filterType) :
filter(wrapNode.getChildren(), t -> true, FilterType.ALL_MATCH);
newNode.setChildren(newChildren);
}
} else if (wrapNode.hasChild()) {
List> newChildren = filter(wrapNode.getChildren(), predicate, filterType);
if (newChildren != null) {
WrapTreeNode newNode = new WrapTreeNode<>(wrapNode.data);
newNode.setChildren(newChildren);
result.add(newNode);
}
}
}
return result.isEmpty() ? null : result;
}
public T getData() {
return data;
}
}