Tree configuration to turn on/off the features in java

185 views Asked by At

I have many features in Tree format and want to control the using configuration.

Assume below tree, each node is one feature

A --- root
A1 & A2 ---  are child of A
A1a, A1b and A1c ---  are child of A1
A2a, A2b and A2c ---  are child of A2

If I turn off A, then all the features should be turned off.
If I turn off A2, then only A2 and its child(till the leaf) should be turned off.
If I turn off A1a, then only A1a feature should be turned off.
If I turned on A2a and turned off A2, then A2 is given higher preference and A2 and its child(till leaf) should be turned off.

Similarly I want to control all the features using configuration.

Is there any a way to control these configuration tree in JAVA?

1

There are 1 answers

2
Peter Pan On BEST ANSWER

My Implementation:

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

}