103 lines
4.2 KiB
Java
Raw Normal View History

2023-06-04 00:00:00 +08:00
package xyz.zhouxy.plusone.commons.util;
import java.util.Collection;
2024-04-16 21:06:02 +08:00
import java.util.Comparator;
2023-06-04 00:00:00 +08:00
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
2024-04-16 21:06:02 +08:00
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
/**
* TreeBuilder
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0
*/
2023-07-19 01:25:21 +08:00
public class TreeBuilder<T, TSubTree extends T, TIdentity> {
2023-06-04 00:00:00 +08:00
private final Function<T, TIdentity> identityGetter;
private final Function<T, Optional<TIdentity>> parentIdentityGetter;
private final BiConsumer<TSubTree, T> addChildMethod;
2024-04-16 21:06:02 +08:00
private final Comparator<? super T> defaultComparator;
2023-06-04 00:00:00 +08:00
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
BiConsumer<TSubTree, T> addChild) {
2024-04-16 21:06:02 +08:00
this(identityGetter, parentIdentityGetter, addChild, null);
}
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
BiConsumer<TSubTree, T> addChild, Comparator<? super T> defaultComparator) {
2023-06-04 00:00:00 +08:00
this.identityGetter = identityGetter;
this.parentIdentityGetter = parentIdentityGetter;
this.addChildMethod = addChild;
2024-04-16 21:06:02 +08:00
this.defaultComparator = defaultComparator;
2023-06-04 00:00:00 +08:00
}
2024-04-16 21:06:02 +08:00
/**
* 将节点构建成树使用 {@link #defaultComparator} 进行排序如果 {@link #defaultComparator}
* <p>
* <b>注意该方法会直接操作 nodes 列表中的节点并没有做深拷贝
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响</b>
*
* @param nodes 平铺的节点列表
*/
public List<T> buildTree(Collection<T> nodes) {
2024-04-16 21:06:02 +08:00
Preconditions.checkNotNull(nodes);
return buildTreeInternal(nodes, this.defaultComparator);
}
/**
* 将节点构建成树
* <p>
* <b>注意该方法会直接操作 nodes 列表中的节点并没有做深拷贝
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响</b>
*
* @param nodes 平铺的节点列表
* @param comparator 用于节点的排序
* 若为 {@code null}则使用 {@link #defaultComparator}
* {@link #defaultComparator} 也为 {@code null}则不排序
*/
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
Preconditions.checkNotNull(nodes);
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
return buildTreeInternal(nodes, c);
}
/**
* 将节点构建成树
* <p>
* <b>注意该方法会直接操作 nodes 列表中的节点并没有做深拷贝
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响</b>
*
* @param nodes 平铺的节点列表
* @param comparator 用于节点的排序若为 {@code null}则不排序
*/
private List<T> buildTreeInternal(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
final Collection<T> allNodes;
if (comparator == null) {
allNodes = nodes;
} else {
allNodes = nodes.stream().sorted(comparator).collect(Collectors.toList());
}
final Map<TIdentity, T> identityNodeMap = allNodes.stream()
.collect(Collectors.toMap(identityGetter, Function.identity(), (n1, n2) -> n1));
2024-04-16 21:06:02 +08:00
final List<T> result = allNodes.stream()
2023-06-04 00:00:00 +08:00
.filter(node -> !this.parentIdentityGetter.apply(node).isPresent())
.collect(Collectors.toList());
2024-04-16 21:06:02 +08:00
allNodes.forEach(node -> parentIdentityGetter.apply(node).ifPresent(parentIdentity -> {
if (identityNodeMap.containsKey(parentIdentity)) {
@SuppressWarnings("unchecked")
TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity);
addChildMethod.accept(parentNode, node);
2023-06-04 00:00:00 +08:00
}
}));
2023-06-04 00:00:00 +08:00
return result;
}
}