Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.citizens.Properties;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import com.citizens.Utils.Messaging;
- public class SettingsTree {
- private final Map<String, Branch> tree = new HashMap<String, Branch>();
- public void populate(String path) {
- StringBuilder progressive = new StringBuilder();
- int index = 0;
- String[] branches = path.split("\\.");
- String buf = "";
- Branch previous = null, temp = null;
- for (String branch : branches) {
- progressive.append(branch);
- buf = progressive.toString();
- temp = new Branch(buf, previous);
- if (getTree().get(buf) == null)
- getTree().put(buf, temp);
- else {
- get(buf).addBranch(path, temp);
- }
- previous = temp;
- if (index != branches.length - 1)
- progressive.append(".");
- ++index;
- }
- Branch branch = tree.get(progressive.toString());
- branch.updateUpwards();
- }
- public Branch get(String path) {
- return getTree().get(path);
- }
- public Map<String, Branch> getTree() {
- return tree;
- }
- public void set(String path, String value) {
- populate(path);
- get(path).set(value);
- get(path).updateUpwards();
- }
- public void remove(String path) {
- Map<String, Branch> temp = get(path).getTree();
- get(path).removeUpwards();
- for (String key : temp.keySet()) {
- tree.remove(key);
- }
- tree.remove(path);
- }
- public class Branch {
- private final Map<String, Branch> tree = new ConcurrentHashMap<String, Branch>();
- private String value = "";
- private final Branch parent;
- private final String path;
- public Branch(String path, Branch parent) {
- tree.put(path, this);
- this.path = path;
- this.parent = parent;
- this.updateUpwards();
- }
- public void updateUpwards() {
- this.update(this.tree);
- }
- private void update(Map<String, Branch> tree) {
- this.tree.putAll(tree);
- if (this.parent != null) {
- parent.addBranch(path, this);
- this.tree.putAll(parent.tree);
- this.parent.update(this.tree);
- }
- }
- public void removeUpwards() {
- this.remove(this.tree);
- }
- private void remove(Map<String, Branch> tree) {
- for (String string : tree.keySet()) {
- this.tree.remove(string);
- }
- if (this.parent != null) {
- this.parent.remove(tree);
- }
- }
- private void addBranch(String path, Branch branch) {
- this.tree.put(path, branch);
- if (parent != null)
- parent.addBranch(path, branch);
- }
- public Map<String, Branch> getTree() {
- return this.tree;
- }
- public void set(String value) {
- this.value = value;
- }
- public String getValue() {
- return this.value;
- }
- public Branch getBranch(String path) {
- return this.tree.get(path);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement