yangys
2024-03-31 2969df3e404db3cd116f27db1495e523ce05bf86
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
package com.qianwen.core.tool.node;
 
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.qianwen.core.tool.node.INode;
import com.qianwen.core.tool.utils.StringPool;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/node/ForestNodeManager.class */
public class ForestNodeManager<T extends INode<T>> {
    private final ImmutableMap<Long, T> nodeMap;
    private final Map<Long, Object> parentIdMap = Maps.newHashMap();
 
    public ForestNodeManager(List<T> nodes) {
        this.nodeMap = Maps.uniqueIndex(nodes, (v0) -> {
            return v0.getId();
        });
    }
 
    public INode<T> getTreeNodeAt(Long id) {
        if (this.nodeMap.containsKey(id))
            return (INode<T>) this.nodeMap.get(id);
 
        return null;
    }
 
    public void addParentId(Long parentId) {
        this.parentIdMap.put(parentId, StringPool.EMPTY);
    }
 
    public List<T> getRoot() {
        
        List<T> roots = new ArrayList<>();
        this.nodeMap.forEach((key, node) -> {
              if (node.getParentId().longValue() == 0L || this.parentIdMap.containsKey(node.getId()))
                roots.add(node); 
            });
        return roots;
    }
}