2015-06-10 26 views
0

我在Tree格式中有许多功能并且想要控制使用配置。树配置打开/关闭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及其子(直到叶)。

同样我想用配置来控制所有的功能。

有什么办法可以在JAVA中控制这些配置树吗?

+0

请告诉我们你已经拥有的,你尝试过什么,哪里也没有工作。 – hoijui

+1

“在JAVA中有没有办法控制这些配置树?” - 是的,你可以实现它。这是你的问题吗? – alfasin

+0

@alfasin。是的,我想用Java控制这些配置树(功能)。任何现有的图书馆? –

回答

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

} 
+0

非常感谢依赖。这是我想要的。 –

+0

@Amaresh Narayanan我的荣幸 –