@Getter
public enum LevelEnum {
ONE_NODE("ONE_NODE", null),
TWO_NODE("TWO_NODE", ONE_NODE),
THREE_NODE("THREE_NODE", TWO_NODE),
FOUR_NODE("FOUR_NODE", THREE_NODE);
@EnumValue
private final String value;
private final LevelEnum parent;
private LevelEnum child;
LevelEnum (String value, LevelEnum parent) {
this.value = value;
this.parent = parent;
this.child = null;
if (parent != null) {
parent.child = this;
}
}
public boolean isRoot() {
return parent == null;
}
public boolean isLeaf() {
return child == null;
}
public static LevelEnum fromValue(String value) {
for (LevelEnum node : values()) {
if (Objects.equals(node.value, value)) {
return node;
}
}
throw new IllegalArgumentException("无效数值: " + value);
}
public List<LevelEnum > getPathToRoot() {
List<LevelEnum > path = new ArrayList<>();
LevelEnum current = this;
while (current != null) {
path.add(current);
current = current.parent;
}
Collections.reverse(path);
return path;
}
public List<LevelEnum > getPathToLeaf() {
List<LevelEnum > path = new ArrayList<>();
LevelEnum current = this;
while (current != null) {
path.add(current);
current = current.child;
}
return path;
}
public String getChildValue() {
if (child != null) {
return child.getValue();
}
return null;
}
public static LevelEnum getFirstLevel() {
for (LevelEnum node : values()) {
if (node.isRoot()) {
return node;
}
}
return null;
}
public static String getFirstLevelValue() {
for (LevelEnum node : values()) {
if (node.isRoot()) {
return node.getValue();
}
}
return null;
}
}