树配置打开/关闭java中的功能

时间:2015-06-10 05:15:04

标签: java configuration configuration-files

我有许多Tree格式的功能,并希望控制使用配置。

假设在树下面,每个节点都是一个特征

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和它的孩子(直到叶子)应该被关闭。

同样,我想使用配置控制所有功能。

有没有办法在JAVA中控制这些配置树?

1 个答案:

答案 0 :(得分:0)

我的实施:

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);
        }
    }

}