完善 TreeBuilder。

This commit is contained in:
zhouxy108 2023-07-19 01:25:21 +08:00
parent 47eb91d015
commit 201cf39e0c
2 changed files with 6 additions and 14 deletions

View File

@ -8,14 +8,14 @@ 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;
public class TreeBuilder<T, TIdentity> { public class TreeBuilder<T, TSubTree extends T, TIdentity> {
private final Collection<T> nodes; private final Collection<T> nodes;
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<T, T> addChildrenMethod; private final BiConsumer<TSubTree, T> addChildrenMethod;
public TreeBuilder(Collection<T> nodes, Function<T, TIdentity> identityGetter, public TreeBuilder(Collection<T> nodes, Function<T, TIdentity> identityGetter,
Function<T, Optional<TIdentity>> parentIdentityGetter, BiConsumer<T, T> addChildren) { Function<T, Optional<TIdentity>> parentIdentityGetter, BiConsumer<TSubTree, T> addChildren) {
this.nodes = nodes; this.nodes = nodes;
this.identityGetter = identityGetter; this.identityGetter = identityGetter;
this.parentIdentityGetter = parentIdentityGetter; this.parentIdentityGetter = parentIdentityGetter;
@ -30,7 +30,8 @@ public class TreeBuilder<T, TIdentity> {
for (T node : this.nodes) { for (T node : this.nodes) {
Optional<TIdentity> parentIdentity = parentIdentityGetter.apply(node); Optional<TIdentity> parentIdentity = parentIdentityGetter.apply(node);
if (parentIdentity.isPresent() && identityNodeMap.containsKey(parentIdentity.get())) { if (parentIdentity.isPresent() && identityNodeMap.containsKey(parentIdentity.get())) {
T parentNode = identityNodeMap.get(parentIdentity.get()); @SuppressWarnings("all")
TSubTree parentNode = (TSubTree) identityNodeMap.get(parentIdentity.get());
addChildrenMethod.accept(parentNode, node); addChildrenMethod.accept(parentNode, node);
} }
} }

View File

@ -35,7 +35,7 @@ class TreeBuilderTests {
menus, menus,
Menu::getMenuCode, Menu::getMenuCode,
Menu::getParentMenuCode, Menu::getParentMenuCode,
Menu::addChild) MenuList::addChild)
.buildTree(); .buildTree();
log.info("menuTree: {}", menuTree); log.info("menuTree: {}", menuTree);
} }
@ -64,8 +64,6 @@ abstract class Menu {
public String getTitle() { public String getTitle() {
return title; return title;
} }
public abstract void addChild(Menu child);
} }
@ToString(callSuper = true) @ToString(callSuper = true)
@ -89,12 +87,6 @@ class MenuItem extends Menu {
public String getUrl() { public String getUrl() {
return url; return url;
} }
@Override
@Deprecated
public void addChild(Menu child) {
throw new UnsupportedOperationException("Unimplemented method 'addChild'");
}
} }
@ToString(callSuper = true) @ToString(callSuper = true)
@ -126,7 +118,6 @@ class MenuList extends Menu {
return instance; return instance;
} }
@Override
public void addChild(Menu child) { public void addChild(Menu child) {
if (this.children == null) { if (this.children == null) {
this.children = Lists.newArrayList(); this.children = Lists.newArrayList();