2024-10-12 00:57:35 +08:00
|
|
|
|
/*
|
2025-01-30 19:23:25 +08:00
|
|
|
|
* Copyright 2023-2025 the original author or authors.
|
2024-10-12 00:57:35 +08:00
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* https://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.
|
|
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2024-03-02 14:48:19 +08:00
|
|
|
|
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
|
|
|
|
|
2024-03-02 14:48:19 +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;
|
2024-03-02 14:48:19 +08:00
|
|
|
|
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>
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-04-16 21:06:02 +08:00
|
|
|
|
* @param nodes 平铺的节点列表
|
|
|
|
|
*/
|
2024-03-02 14:48:19 +08:00
|
|
|
|
public List<T> buildTree(Collection<T> nodes) {
|
2024-12-04 01:17:20 +08:00
|
|
|
|
AssertTools.checkNotNull(nodes);
|
2024-04-16 21:06:02 +08:00
|
|
|
|
return buildTreeInternal(nodes, this.defaultComparator);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将节点构建成树。
|
|
|
|
|
* <p>
|
2024-08-08 14:55:42 +08:00
|
|
|
|
* <b>!!注意:该方法会直接操作 nodes 列表中的节点,并没有做深拷贝,
|
2024-04-16 21:06:02 +08:00
|
|
|
|
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响。</b>
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-04-16 21:06:02 +08:00
|
|
|
|
* @param nodes 平铺的节点列表
|
|
|
|
|
* @param comparator 用于节点的排序。
|
|
|
|
|
* 若为 {@code null},则使用 {@link #defaultComparator};
|
|
|
|
|
* 若 {@link #defaultComparator} 也为 {@code null},则不排序。
|
2024-08-08 14:55:42 +08:00
|
|
|
|
* <b>仅影响调用 addChild 的顺序,如果操作对象本身对应的控制了子节点的顺序,无法影响其相关逻辑。</b>
|
2024-04-16 21:06:02 +08:00
|
|
|
|
*/
|
|
|
|
|
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
|
2024-12-04 01:17:20 +08:00
|
|
|
|
AssertTools.checkNotNull(nodes);
|
2024-04-16 21:06:02 +08:00
|
|
|
|
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
|
|
|
|
|
return buildTreeInternal(nodes, c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将节点构建成树。
|
|
|
|
|
* <p>
|
|
|
|
|
* <b>注意,该方法会直接操作 nodes 列表中的节点,并没有做深拷贝,
|
|
|
|
|
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响。</b>
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-04-16 21:06:02 +08:00
|
|
|
|
* @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()
|
2024-03-02 23:25:23 +08:00
|
|
|
|
.collect(Collectors.toMap(identityGetter, Function.identity(), (n1, n2) -> n1));
|
2024-08-26 17:37:30 +08:00
|
|
|
|
// 根节点
|
|
|
|
|
final List<T> rootNodes = 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 -> {
|
2024-03-02 14:48:19 +08:00
|
|
|
|
if (identityNodeMap.containsKey(parentIdentity)) {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity);
|
|
|
|
|
addChildMethod.accept(parentNode, node);
|
2023-06-04 00:00:00 +08:00
|
|
|
|
}
|
2024-03-02 14:48:19 +08:00
|
|
|
|
}));
|
2024-08-26 17:37:30 +08:00
|
|
|
|
return rootNodes;
|
2023-06-04 00:00:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|