【问题标题】:Tree configuration to turn on/off the features in java树配置以打开/关闭java中的功能
【发布时间】:2015-06-10 05:15:03
【问题描述】:

我有很多 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 中控制这些配置树?

【问题讨论】:

  • 请告诉我们您已经拥有什么,您尝试过什么,以及哪些地方没有奏效。
  • "有没有办法在 JAVA 中控制这些配置树?" - 是的,你可以实现它。这是你的问题吗?
  • @alfasin。是的,我想用 Java 控制这些配置树(功能)。任何现有的图书馆可用?

标签: java configuration configuration-files


【解决方案1】:

我的实现:

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

}

【讨论】:

  • 非常感谢您的信赖。这就是我想要的。
  • @Amaresh Narayanan 我的荣幸
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多