树配置以 on/off java 中的功能
Tree configuration to turn on/off the features in java
我有很多树格式的功能,想控制使用配置。
假设下面的树,每个节点是一个特征
A --- root
A1 & A2 --- are child of A
A1a, A1b and A1c --- are child of A1
A2a, A2b and A2c --- are child of A2
如果我关闭A,那么所有的功能都应该关闭。
如果我关闭 A2,那么只有 A2 和它的子节点(直到叶子)应该被关闭。
如果我关闭 A1a,那么应该只关闭 A1a 功能。
如果我打开 A2a 并关闭 A2,那么 A2 会被赋予更高的优先级,并且 A2 及其子节点(till leaf)应该被关闭。
同样,我想使用配置来控制所有功能。
在JAVA中有什么方法可以控制这些配置树吗?
我的实现:
import java.util.List;
public class CtrlNode {
private String name;
private boolean status;
private CtrlNode parent;
private List<CtrlNode> kids;
public CtrlNode(String name, boolean status, CtrlNode parent, List<CtrlNode> kids) {
super();
this.name = name;
this.status = status;
this.parent = parent;
this.kids = kids;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public CtrlNode getParent() {
return parent;
}
public void setParent(CtrlNode parent) {
this.parent = parent;
}
public List<CtrlNode> getKids() {
return kids;
}
public void setKids(List<CtrlNode> kids) {
this.kids = kids;
}
public void off() {
recurOff(this);
}
private void recurOff(CtrlNode node) {
if (node != null && node.getStatus()) {
node.setStatus(false);
for (CtrlNode kid : node.getKids()) {
recurOff(kid);
}
}
}
public void on() {
if(!this.getStatus() && this.getParent().getStatus()) {
this.setStatus(true);
}
}
}
我有很多树格式的功能,想控制使用配置。
假设下面的树,每个节点是一个特征
A --- root
A1 & A2 --- are child of A
A1a, A1b and A1c --- are child of A1
A2a, A2b and A2c --- are child of A2
如果我关闭A,那么所有的功能都应该关闭。
如果我关闭 A2,那么只有 A2 和它的子节点(直到叶子)应该被关闭。
如果我关闭 A1a,那么应该只关闭 A1a 功能。
如果我打开 A2a 并关闭 A2,那么 A2 会被赋予更高的优先级,并且 A2 及其子节点(till leaf)应该被关闭。
同样,我想使用配置来控制所有功能。
在JAVA中有什么方法可以控制这些配置树吗?
我的实现:
import java.util.List;
public class CtrlNode {
private String name;
private boolean status;
private CtrlNode parent;
private List<CtrlNode> kids;
public CtrlNode(String name, boolean status, CtrlNode parent, List<CtrlNode> kids) {
super();
this.name = name;
this.status = status;
this.parent = parent;
this.kids = kids;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public CtrlNode getParent() {
return parent;
}
public void setParent(CtrlNode parent) {
this.parent = parent;
}
public List<CtrlNode> getKids() {
return kids;
}
public void setKids(List<CtrlNode> kids) {
this.kids = kids;
}
public void off() {
recurOff(this);
}
private void recurOff(CtrlNode node) {
if (node != null && node.getStatus()) {
node.setStatus(false);
for (CtrlNode kid : node.getKids()) {
recurOff(kid);
}
}
}
public void on() {
if(!this.getStatus() && this.getParent().getStatus()) {
this.setStatus(true);
}
}
}