Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. package com.citizens.Properties;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6.  
  7. import com.citizens.Utils.Messaging;
  8.  
  9. public class SettingsTree {
  10. private final Map<String, Branch> tree = new HashMap<String, Branch>();
  11.  
  12. public void populate(String path) {
  13. StringBuilder progressive = new StringBuilder();
  14. int index = 0;
  15. String[] branches = path.split("\\.");
  16. String buf = "";
  17. Branch previous = null, temp = null;
  18. for (String branch : branches) {
  19. progressive.append(branch);
  20. buf = progressive.toString();
  21. temp = new Branch(buf, previous);
  22. if (getTree().get(buf) == null)
  23. getTree().put(buf, temp);
  24. else {
  25. get(buf).addBranch(path, temp);
  26. }
  27. previous = temp;
  28. if (index != branches.length - 1)
  29. progressive.append(".");
  30. ++index;
  31. }
  32. Branch branch = tree.get(progressive.toString());
  33. branch.updateUpwards();
  34. }
  35.  
  36. public Branch get(String path) {
  37. return getTree().get(path);
  38. }
  39.  
  40. public Map<String, Branch> getTree() {
  41. return tree;
  42. }
  43.  
  44. public void set(String path, String value) {
  45. populate(path);
  46. get(path).set(value);
  47. get(path).updateUpwards();
  48. }
  49.  
  50. public void remove(String path) {
  51. Map<String, Branch> temp = get(path).getTree();
  52. get(path).removeUpwards();
  53. for (String key : temp.keySet()) {
  54. tree.remove(key);
  55. }
  56. tree.remove(path);
  57. }
  58.  
  59. public class Branch {
  60. private final Map<String, Branch> tree = new ConcurrentHashMap<String, Branch>();
  61. private String value = "";
  62. private final Branch parent;
  63. private final String path;
  64.  
  65. public Branch(String path, Branch parent) {
  66. tree.put(path, this);
  67. this.path = path;
  68. this.parent = parent;
  69. this.updateUpwards();
  70. }
  71.  
  72. public void updateUpwards() {
  73. this.update(this.tree);
  74. }
  75.  
  76. private void update(Map<String, Branch> tree) {
  77. this.tree.putAll(tree);
  78. if (this.parent != null) {
  79. parent.addBranch(path, this);
  80. this.tree.putAll(parent.tree);
  81. this.parent.update(this.tree);
  82. }
  83. }
  84.  
  85. public void removeUpwards() {
  86. this.remove(this.tree);
  87. }
  88.  
  89. private void remove(Map<String, Branch> tree) {
  90. for (String string : tree.keySet()) {
  91. this.tree.remove(string);
  92. }
  93. if (this.parent != null) {
  94. this.parent.remove(tree);
  95. }
  96. }
  97.  
  98. private void addBranch(String path, Branch branch) {
  99. this.tree.put(path, branch);
  100. if (parent != null)
  101. parent.addBranch(path, branch);
  102. }
  103.  
  104. public Map<String, Branch> getTree() {
  105. return this.tree;
  106. }
  107.  
  108. public void set(String value) {
  109. this.value = value;
  110. }
  111.  
  112. public String getValue() {
  113. return this.value;
  114. }
  115.  
  116. public Branch getBranch(String path) {
  117. return this.tree.get(path);
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement