Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. /**
  2. * A simple realisation of HierarchicalInterface.
  3. * Created by @author Bogdan Protsenko on 4/10/2017.
  4. *
  5. * Modified by @author Bogdan Protsenko on 4/20/2017.
  6. * HierarchicalInterface methods have been marked as virtual
  7. * because of overriding necessity.
  8. *
  9. * Modified by @author Bogdan Protsenko on 4/24/2017.
  10. * Generic type of children list changed to HierarchicalInterface.
  11. */
  12.  
  13. public virtual class HierarchyNode implements HierarchicalInterface{
  14. //ligthning stops working with two directional linking :(
  15. //@AuraEnabled
  16. public HierarchyNode parent;
  17. @AuraEnabled
  18. public List<HierarchicalInterface> children { get; set; }
  19. @AuraEnabled
  20. public String name { get; set; }
  21. @AuraEnabled
  22. public String id { get; set; }
  23.  
  24. /**
  25. * Constructs an empty HierarchyNode.
  26. */
  27. public HierarchyNode(){
  28. children = new List<HierarchicalInterface>();
  29. }
  30.  
  31. /**
  32. * Constructs a HierarchyNode with name.
  33. * @param name this HierarchyNode name.
  34. */
  35. public HierarchyNode(String name) {
  36. this.name = name;
  37. children = new List<HierarchicalInterface>();
  38. }
  39.  
  40. /**
  41. * Constructs a HierarchyNode with name and Id.
  42. * @param name this HierarchyNode name.
  43. * @param id this HierarchyNode id.
  44. */
  45. public HierarchyNode(String name, String id) {
  46. this.name = name;
  47. this.id = id;
  48. children = new List<HierarchicalInterface>();
  49. }
  50.  
  51. /**
  52. * Adds a child to this HierarchyNode.
  53. * @param a child to add.
  54. */
  55. public virtual void addChild(HierarchicalInterface child){
  56. children.add(child);
  57. }
  58.  
  59. /**
  60. * Removes a child of this HierarchyNode.
  61. * @param a child to remove.
  62. * @return true, if this HierarchyNode had such child. Otherwise - false.
  63. */
  64. public virtual Boolean removeChild(HierarchicalInterface child){
  65. for(Integer i = 0, size = children.size(); i < size; ++i){
  66. if(((HierarchyNode)child).equals((HierarchyNode)children[i])){
  67. children.remove(i);
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73.  
  74. /**
  75. * Returns a list of this HierarchyNode children.
  76. * @return a list of this HierarchyNode children
  77. */
  78. public virtual List<HierarchicalInterface> getChildren(){
  79. return children;
  80. }
  81.  
  82. /**
  83. * Returns this HierarchyNode parent.
  84. * @return this HierarchyNode parent.
  85. */
  86. public virtual HierarchicalInterface getParent(){
  87. return parent;
  88. }
  89.  
  90. /**
  91. * Sets a parent of this HierarchyNode.
  92. * @param a parent to be set.
  93. */
  94. public virtual void setParent(HierarchicalInterface parent){
  95. this.parent = (HierarchyNode)parent;
  96. }
  97.  
  98. /**
  99. * Removes a parent of this HierarchyNode.
  100. */
  101. public virtual void removeParent(){
  102. parent = null;
  103. }
  104.  
  105. /**
  106. * Checks whether this HierarchyNode is a root.
  107. * @return true, if this HierarchyNode is a root. Otherwise - false.
  108. */
  109. public virtual Boolean isRoot(){
  110. return parent == null;
  111. }
  112.  
  113. /**
  114. * Checks whether this HierarchyNode is a leaf.
  115. * @return true, if this HierarchyNode is a leaf. Otherwise - false.
  116. */
  117. public virtual Boolean isLeaf(){
  118. return children.isEmpty();
  119. }
  120.  
  121.  
  122. /**
  123. * An override of the equals method.
  124. * @param obj the reference object with which to compare.
  125. * @return tue if this object is the same as the obj
  126. * argument; false otherwise.
  127. */
  128. public Boolean equals(Object o) {
  129. if (!(o instanceof HierarchyNode)) return false;
  130.  
  131. HierarchyNode that = (HierarchyNode) o;
  132.  
  133. if (parent != null ? !parent.equals(that.parent) : that.parent != null) return false;
  134. //simple check child lists by hashcode to avoid recursive stack depth reach.
  135. if (children != null ? !(children.hashCode() == that.children.hashCode()) : that.children != null) return false;
  136. if (name != null ? !name.equals(that.name) : that.name != null) return false;
  137. return id != null ? id.equals(that.id) : that.id == null;
  138. }
  139.  
  140. /**
  141. * An override of the hashCode method.
  142. * @return a hash code value for this object.
  143. */
  144. public override Integer hashCode() {
  145. Integer result = parent != null ? parent.hashCode() : 0;
  146. //excluded to avoid recursive stack depth reach.
  147. //result = 31 * result + (children != null ? children.hashCode() : 0);
  148. result = 31 * result + (name != null ? name.hashCode() : 0);
  149. result = 31 * result + (id != null ? id.hashCode() : 0);
  150. return result;
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement