2023-06-04 00:00:00 +08:00
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
2024-03-02 14:48:19 +08:00
|
|
|
import com.google.gson.Gson;
|
2023-06-04 00:00:00 +08:00
|
|
|
|
|
|
|
|
import lombok.ToString;
|
|
|
|
|
|
|
|
|
|
class TreeBuilderTests {
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(TreeBuilderTests.class);
|
2024-03-02 14:48:19 +08:00
|
|
|
private final TreeBuilder<Menu, MenuList, String> treeBuilder = new TreeBuilder<>(
|
|
|
|
|
Menu::getMenuCode,
|
2024-04-16 21:06:02 +08:00
|
|
|
menu -> Optional.ofNullable(menu.parentMenuCode),
|
|
|
|
|
MenuList::addChild,
|
|
|
|
|
(a, b) -> Integer.compare(a.getOrderNum(), b.getOrderNum()));
|
2023-06-04 00:00:00 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testBuildTree() {
|
|
|
|
|
List<Menu> menus = Lists.newArrayList(
|
2024-04-16 21:06:02 +08:00
|
|
|
MenuList.of("B", "系统管理", 3),
|
|
|
|
|
MenuItem.of("A", "首页", "/home", 1),
|
|
|
|
|
/**/MenuItem.of("B", "B002", "角色管理", "/sys/role-mgmt", 3),
|
|
|
|
|
/**/MenuItem.of("B", "B001", "功能管理", "/sys/function-mgmt", 4),
|
|
|
|
|
/**/MenuItem.of("B", "B004", "系统参数管理", "/sys/param-mgmt", 1),
|
|
|
|
|
/**/MenuItem.of("B", "B003", "账号管理", "/sys/account-mgmt", 2),
|
|
|
|
|
MenuList.of("C", "一级菜单C", 2),
|
|
|
|
|
/**/MenuItem.of("C", "C3", "二级菜单C3", "/c/c3", 2),
|
|
|
|
|
/**/MenuList.of("C", "C1", "二级菜单C1", 2),
|
|
|
|
|
/**//**/MenuItem.of("C1", "C1001", "三级菜单C1001", "/c/c1/c1001", 1),
|
|
|
|
|
/**//**/MenuItem.of("C1", "C1002", "三级菜单C1002", "/c/c1/c1002", 2),
|
|
|
|
|
/**/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())
|
2023-06-04 00:00:00 +08:00
|
|
|
);
|
2024-04-16 21:06:02 +08:00
|
|
|
log.info("menuTreeSortedByMenuCode: {}", new Gson().toJson(menuTreeSortedByMenuCode));
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ToString
|
|
|
|
|
abstract class Menu {
|
|
|
|
|
protected final String parentMenuCode;
|
|
|
|
|
protected final String menuCode;
|
|
|
|
|
protected final String title;
|
2024-04-16 21:06:02 +08:00
|
|
|
protected final int orderNum;
|
2023-06-04 00:00:00 +08:00
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
public Menu(String parentMenuCode, String menuCode, String title, int orderNum) {
|
2023-06-04 00:00:00 +08:00
|
|
|
this.parentMenuCode = parentMenuCode;
|
|
|
|
|
this.menuCode = menuCode;
|
|
|
|
|
this.title = title;
|
2024-04-16 21:06:02 +08:00
|
|
|
this.orderNum = orderNum;
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getMenuCode() {
|
|
|
|
|
return menuCode;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
public String getParentMenuCode() {
|
|
|
|
|
return parentMenuCode;
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getTitle() {
|
|
|
|
|
return title;
|
|
|
|
|
}
|
2024-04-16 21:06:02 +08:00
|
|
|
|
|
|
|
|
public int getOrderNum() {
|
|
|
|
|
return orderNum;
|
|
|
|
|
}
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ToString(callSuper = true)
|
|
|
|
|
class MenuItem extends Menu {
|
|
|
|
|
|
|
|
|
|
private final String url;
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
private MenuItem(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
|
|
|
|
|
super(parentMenuCode, menuCode, title, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
this.url = url;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuItem of(String parentMenuCode, String menuCode, String title, String url, int orderNum) {
|
|
|
|
|
return new MenuItem(parentMenuCode, menuCode, title, url, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuItem of(String menuCode, String title, String url, int orderNum) {
|
|
|
|
|
return new MenuItem(null, menuCode, title, url, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getUrl() {
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ToString(callSuper = true)
|
|
|
|
|
class MenuList extends Menu {
|
|
|
|
|
|
|
|
|
|
private List<Menu> children;
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
private MenuList(String parentMenuCode, String menuCode, String title, int orderNum) {
|
|
|
|
|
super(parentMenuCode, menuCode, title, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuList of(String parentMenuCode, String menuCode, String title, int orderNum) {
|
|
|
|
|
return new MenuList(parentMenuCode, menuCode, title, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuList of(String menuCode, String title, int orderNum) {
|
|
|
|
|
return new MenuList(null, menuCode, title, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuList of(String menuCode, String title, Iterable<Menu> children, int orderNum) {
|
|
|
|
|
return of(null, menuCode, title, children, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:06:02 +08:00
|
|
|
static MenuList of(String parentMenuCode, String menuCode, String title, Iterable<Menu> children, int orderNum) {
|
|
|
|
|
MenuList instance = of(parentMenuCode, menuCode, title, orderNum);
|
2023-06-04 00:00:00 +08:00
|
|
|
for (Menu child : children) {
|
|
|
|
|
instance.addChild(child);
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addChild(Menu child) {
|
|
|
|
|
if (this.children == null) {
|
|
|
|
|
this.children = Lists.newArrayList();
|
|
|
|
|
}
|
|
|
|
|
this.children.add(child);
|
|
|
|
|
}
|
|
|
|
|
}
|