Advertisement
Guest User

Untitled

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