mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
1ee2d5d257
commit
3fefdf5151
@ -215,7 +215,7 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Node<T> {
|
||||
|
||||
/**
|
||||
* 递归过滤当前树,注意此方法会修改当前树<br>
|
||||
* 通过{@link Filter}指定的过滤规则,本节点或子节点满足过滤条件,则保留当前节点,否则抛弃节点及其子节点
|
||||
* 通过{@link Filter}指定的过滤规则,本节点或子节点满足过滤条件,则保留当前节点及其所有子节点,否则抛弃节点及其子节点
|
||||
*
|
||||
* @param filter 节点过滤规则函数,只需处理本级节点本身即可
|
||||
* @return 过滤后的节点,{@code null} 表示不满足过滤要求,丢弃之
|
||||
@ -223,6 +223,11 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Node<T> {
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public Tree<T> filter(Filter<Tree<T>> filter) {
|
||||
if(filter.accept(this)){
|
||||
// 本节点满足,则包括所有子节点都保留
|
||||
return this;
|
||||
}
|
||||
|
||||
final List<Tree<T>> children = getChildren();
|
||||
if (CollUtil.isNotEmpty(children)) {
|
||||
// 递归过滤子节点
|
||||
@ -243,7 +248,7 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Node<T> {
|
||||
}
|
||||
|
||||
// 子节点都不符合过滤条件,检查本节点
|
||||
return filter.accept(this) ? this : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.lang.tree;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -109,12 +110,12 @@ public class TreeTest {
|
||||
// 经过过滤,生成新的树
|
||||
Tree<String> newTree = tree.filterNew((t)->{
|
||||
final CharSequence name = t.getName();
|
||||
return null != name && name.toString().contains("管理");
|
||||
return null != name && name.toString().contains("店铺");
|
||||
});
|
||||
|
||||
List<String> ids = new ArrayList<>();
|
||||
newTree.walk((tr)-> ids.add(tr.getId()));
|
||||
Assert .assertEquals(6, ids.size());
|
||||
Assert .assertEquals(4, ids.size());
|
||||
|
||||
List<String> ids2 = new ArrayList<>();
|
||||
tree.walk((tr)-> ids2.add(tr.getId()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user