forked from plusone/plusone-commons
允许构建时进行排序。
This commit is contained in:
parent
22687605b2
commit
d3b19c6cb0
@ -1,6 +1,7 @@
|
|||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -8,25 +9,88 @@ import java.util.function.BiConsumer;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TreeBuilder
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||||
private final Function<T, TIdentity> identityGetter;
|
private final Function<T, TIdentity> identityGetter;
|
||||||
private final Function<T, Optional<TIdentity>> parentIdentityGetter;
|
private final Function<T, Optional<TIdentity>> parentIdentityGetter;
|
||||||
private final BiConsumer<TSubTree, T> addChildMethod;
|
private final BiConsumer<TSubTree, T> addChildMethod;
|
||||||
|
private final Comparator<? super T> defaultComparator;
|
||||||
|
|
||||||
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
|
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
|
||||||
BiConsumer<TSubTree, T> addChild) {
|
BiConsumer<TSubTree, T> addChild) {
|
||||||
|
this(identityGetter, parentIdentityGetter, addChild, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeBuilder(Function<T, TIdentity> identityGetter, Function<T, Optional<TIdentity>> parentIdentityGetter,
|
||||||
|
BiConsumer<TSubTree, T> addChild, Comparator<? super T> defaultComparator) {
|
||||||
this.identityGetter = identityGetter;
|
this.identityGetter = identityGetter;
|
||||||
this.parentIdentityGetter = parentIdentityGetter;
|
this.parentIdentityGetter = parentIdentityGetter;
|
||||||
this.addChildMethod = addChild;
|
this.addChildMethod = addChild;
|
||||||
|
this.defaultComparator = defaultComparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将节点构建成树。使用 {@link #defaultComparator} 进行排序。如果 {@link #defaultComparator}
|
||||||
|
* <p>
|
||||||
|
* <b>注意,该方法会直接操作 nodes 列表中的节点,并没有做深拷贝,
|
||||||
|
* 注意避免 nodes 中的元素产生变化所带来的意料之外的影响。</b>
|
||||||
|
*
|
||||||
|
* @param nodes 平铺的节点列表
|
||||||
|
*/
|
||||||
public List<T> buildTree(Collection<T> nodes) {
|
public List<T> buildTree(Collection<T> nodes) {
|
||||||
Map<TIdentity, T> identityNodeMap = nodes.stream()
|
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));
|
.collect(Collectors.toMap(identityGetter, Function.identity(), (n1, n2) -> n1));
|
||||||
List<T> result = nodes.stream()
|
final List<T> result = allNodes.stream()
|
||||||
.filter(node -> !this.parentIdentityGetter.apply(node).isPresent())
|
.filter(node -> !this.parentIdentityGetter.apply(node).isPresent())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
nodes.forEach(node -> parentIdentityGetter.apply(node).ifPresent(parentIdentity -> {
|
allNodes.forEach(node -> parentIdentityGetter.apply(node).ifPresent(parentIdentity -> {
|
||||||
if (identityNodeMap.containsKey(parentIdentity)) {
|
if (identityNodeMap.containsKey(parentIdentity)) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity);
|
TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity);
|
||||||
|
@ -17,29 +17,34 @@ class TreeBuilderTests {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class);
|
private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class);
|
||||||
private final TreeBuilder<Menu, MenuList, String> treeBuilder = new TreeBuilder<>(
|
private final TreeBuilder<Menu, MenuList, String> treeBuilder = new TreeBuilder<>(
|
||||||
Menu::getMenuCode,
|
Menu::getMenuCode,
|
||||||
Menu::getParentMenuCode,
|
menu -> Optional.ofNullable(menu.parentMenuCode),
|
||||||
MenuList::addChild);
|
MenuList::addChild,
|
||||||
|
(a, b) -> Integer.compare(a.getOrderNum(), b.getOrderNum()));
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBuildTree() {
|
void testBuildTree() {
|
||||||
List<Menu> menus = Lists.newArrayList(
|
List<Menu> menus = Lists.newArrayList(
|
||||||
MenuItem.of("A", "首页", "/home"),
|
MenuList.of("B", "系统管理", 3),
|
||||||
MenuList.of("B", "系统管理"),
|
MenuItem.of("A", "首页", "/home", 1),
|
||||||
MenuItem.of("B", "B001", "功能管理", "/sys/function-mgmt"),
|
/**/MenuItem.of("B", "B002", "角色管理", "/sys/role-mgmt", 3),
|
||||||
MenuItem.of("B", "B002", "角色管理", "/sys/role-mgmt"),
|
/**/MenuItem.of("B", "B001", "功能管理", "/sys/function-mgmt", 4),
|
||||||
MenuItem.of("B", "B003", "账号管理", "/sys/account-mgmt"),
|
/**/MenuItem.of("B", "B004", "系统参数管理", "/sys/param-mgmt", 1),
|
||||||
MenuItem.of("B", "B004", "系统参数管理", "/sys/param-mgmt"),
|
/**/MenuItem.of("B", "B003", "账号管理", "/sys/account-mgmt", 2),
|
||||||
MenuList.of("C", "一级菜单C"),
|
MenuList.of("C", "一级菜单C", 2),
|
||||||
MenuList.of("C", "C1", "二级菜单C1"),
|
/**/MenuItem.of("C", "C3", "二级菜单C3", "/c/c3", 2),
|
||||||
MenuItem.of("C1", "C1001", "三级菜单C1001", "/c/c1/c1001"),
|
/**/MenuList.of("C", "C1", "二级菜单C1", 2),
|
||||||
MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002"),
|
/**//**/MenuItem.of("C1", "C1001", "三级菜单C1001", "/c/c1/c1001", 1),
|
||||||
MenuItem.of("C", "C2", "二级菜单C2", "/c/c2"),
|
/**//**/MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002", 2),
|
||||||
MenuItem.of("C", "C3", "二级菜单C3", "/c/c3")
|
/**/MenuItem.of("C", "C2", "二级菜单C2", "/c/c2", 1));
|
||||||
|
|
||||||
|
List<Menu> menuTreeSortedByOrderNum = treeBuilder.buildTree(menus);
|
||||||
|
log.info("menuTreeSortedByOrderNum: {}", new Gson().toJson(menuTreeSortedByOrderNum));
|
||||||
|
|
||||||
|
List<Menu> menuTreeSortedByMenuCode = treeBuilder.buildTree(
|
||||||
|
menus,
|
||||||
|
(a, b) -> a.getMenuCode().compareTo(b.getMenuCode())
|
||||||
);
|
);
|
||||||
|
log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode));
|
||||||
List<Menu> menuTree = treeBuilder.buildTree(menus);
|
|
||||||
log.info("menuTree: {}", new Gson().toJson(menuTree));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,24 +53,30 @@ abstract class Menu {
|
|||||||
protected final String parentMenuCode;
|
protected final String parentMenuCode;
|
||||||
protected final String menuCode;
|
protected final String menuCode;
|
||||||
protected final String title;
|
protected final String title;
|
||||||
|
protected final int orderNum;
|
||||||
|
|
||||||
public Menu(String parentMenuCode, String menuCode, String title) {
|
public Menu(String parentMenuCode, String menuCode, String title, int orderNum) {
|
||||||
this.parentMenuCode = parentMenuCode;
|
this.parentMenuCode = parentMenuCode;
|
||||||
this.menuCode = menuCode;
|
this.menuCode = menuCode;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
|
this.orderNum = orderNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMenuCode() {
|
public String getMenuCode() {
|
||||||
return menuCode;
|
return menuCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<String> getParentMenuCode() {
|
public String getParentMenuCode() {
|
||||||
return Optional.ofNullable(parentMenuCode);
|
return parentMenuCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getOrderNum() {
|
||||||
|
return orderNum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
@ -73,17 +84,17 @@ class MenuItem extends Menu {
|
|||||||
|
|
||||||
private final String url;
|
private final String url;
|
||||||
|
|
||||||
private MenuItem(String parentMenuCode, String menuCode, String title, String url) {
|
private MenuItem(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
|
||||||
super(parentMenuCode, menuCode, title);
|
super(parentMenuCode, menuCode, title, orderNum);
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuItem of(String parentMenuCode, String menuCode, String title, String url) {
|
static MenuItem of(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
|
||||||
return new MenuItem(parentMenuCode, menuCode, title, url);
|
return new MenuItem(parentMenuCode, menuCode, title, url, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuItem of(String menuCode, String title, String url) {
|
static MenuItem of(String menuCode, String title, String url, int orderNum) {
|
||||||
return new MenuItem(null, menuCode, title, url);
|
return new MenuItem(null, menuCode, title, url, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
@ -96,24 +107,24 @@ class MenuList extends Menu {
|
|||||||
|
|
||||||
private List<Menu> children;
|
private List<Menu> children;
|
||||||
|
|
||||||
private MenuList(String parentMenuCode, String menuCode, String title) {
|
private MenuList(String parentMenuCode, String menuCode, String title, int orderNum) {
|
||||||
super(parentMenuCode, menuCode, title);
|
super(parentMenuCode, menuCode, title, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuList of(String parentMenuCode, String menuCode, String title) {
|
static MenuList of(String parentMenuCode, String menuCode, String title, int orderNum) {
|
||||||
return new MenuList(parentMenuCode, menuCode, title);
|
return new MenuList(parentMenuCode, menuCode, title, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuList of(String menuCode, String title) {
|
static MenuList of(String menuCode, String title, int orderNum) {
|
||||||
return new MenuList(null, menuCode, title);
|
return new MenuList(null, menuCode, title, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuList of(String menuCode, String title, Iterable<Menu> children) {
|
static MenuList of(String menuCode, String title, Iterable<Menu> children, int orderNum) {
|
||||||
return of(null, menuCode, title, children);
|
return of(null, menuCode, title, children, orderNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MenuList of(String parentMenuCode, String menuCode, String title, Iterable<Menu> children) {
|
static MenuList of(String parentMenuCode, String menuCode, String title, Iterable<Menu> children, int orderNum) {
|
||||||
MenuList instance = of(parentMenuCode, menuCode, title);
|
MenuList instance = of(parentMenuCode, menuCode, title, orderNum);
|
||||||
for (Menu child : children) {
|
for (Menu child : children) {
|
||||||
instance.addChild(child);
|
instance.addChild(child);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user