优化 MenuViewObject。

dev
ZhouXY108 2023-07-07 15:33:49 +08:00
parent f83a05e429
commit bd42da6777
1 changed files with 34 additions and 11 deletions

View File

@ -2,9 +2,10 @@ package xyz.zhouxy.plusone.system.application.query.result;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.SortedSet;
import java.util.TreeSet;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -81,18 +82,18 @@ public class MenuViewObject implements IWithOrderNumber {
List<Action> actions; List<Action> actions;
// MENU_LIST // MENU_LIST
List<MenuViewObject> children; SortedSet<MenuViewObject> children;
public void addChild(MenuViewObject child) { public void addChild(MenuViewObject child) {
if (this.children == null) { if (this.children == null) {
this.children = new ArrayList<>(); this.children = new TreeSet<>();
} }
this.children.add(child); this.children.add(child);
} }
public void addChildren(Collection<MenuViewObject> children) { public void addChildren(Collection<MenuViewObject> children) {
if (this.children == null) { if (this.children == null) {
this.children = new ArrayList<>(); this.children = new TreeSet<>();
} }
this.children.addAll(children); this.children.addAll(children);
} }
@ -121,11 +122,33 @@ public class MenuViewObject implements IWithOrderNumber {
} }
public List<MenuViewObject> getChildren() { public List<MenuViewObject> getChildren() {
return Objects.nonNull(this.children) return this.children == null || this.children.isEmpty()
? this.children ? Collections.emptyList()
.stream() : new ArrayList<>(this.children);
.sorted(Comparator.comparing(IWithOrderNumber::getOrderNumber)) }
.toList()
: null; @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MenuViewObject other = (MenuViewObject) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }
} }