Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A simple realisation of HierarchicalInterface.
- * Created by @author Bogdan Protsenko on 4/10/2017.
- *
- * Modified by @author Bogdan Protsenko on 4/20/2017.
- * HierarchicalInterface methods have been marked as virtual
- * because of overriding necessity.
- *
- * Modified by @author Bogdan Protsenko on 4/24/2017.
- * Generic type of children list changed to HierarchicalInterface.
- */
- public virtual class HierarchyNode implements HierarchicalInterface{
- //ligthning stops working with two directional linking :(
- //@AuraEnabled
- public HierarchyNode parent;
- @AuraEnabled
- public List<HierarchicalInterface> children { get; set; }
- @AuraEnabled
- public String name { get; set; }
- @AuraEnabled
- public String id { get; set; }
- /**
- * Constructs an empty HierarchyNode.
- */
- public HierarchyNode(){
- children = new List<HierarchicalInterface>();
- }
- /**
- * Constructs a HierarchyNode with name.
- * @param name this HierarchyNode name.
- */
- public HierarchyNode(String name) {
- this.name = name;
- children = new List<HierarchicalInterface>();
- }
- /**
- * Constructs a HierarchyNode with name and Id.
- * @param name this HierarchyNode name.
- * @param id this HierarchyNode id.
- */
- public HierarchyNode(String name, String id) {
- this.name = name;
- this.id = id;
- children = new List<HierarchicalInterface>();
- }
- /**
- * Adds a child to this HierarchyNode.
- * @param a child to add.
- */
- public virtual void addChild(HierarchicalInterface child){
- children.add(child);
- }
- /**
- * Removes a child of this HierarchyNode.
- * @param a child to remove.
- * @return true, if this HierarchyNode had such child. Otherwise - false.
- */
- public virtual Boolean removeChild(HierarchicalInterface child){
- for(Integer i = 0, size = children.size(); i < size; ++i){
- if(((HierarchyNode)child).equals((HierarchyNode)children[i])){
- children.remove(i);
- return true;
- }
- }
- return false;
- }
- /**
- * Returns a list of this HierarchyNode children.
- * @return a list of this HierarchyNode children
- */
- public virtual List<HierarchicalInterface> getChildren(){
- return children;
- }
- /**
- * Returns this HierarchyNode parent.
- * @return this HierarchyNode parent.
- */
- public virtual HierarchicalInterface getParent(){
- return parent;
- }
- /**
- * Sets a parent of this HierarchyNode.
- * @param a parent to be set.
- */
- public virtual void setParent(HierarchicalInterface parent){
- this.parent = (HierarchyNode)parent;
- }
- /**
- * Removes a parent of this HierarchyNode.
- */
- public virtual void removeParent(){
- parent = null;
- }
- /**
- * Checks whether this HierarchyNode is a root.
- * @return true, if this HierarchyNode is a root. Otherwise - false.
- */
- public virtual Boolean isRoot(){
- return parent == null;
- }
- /**
- * Checks whether this HierarchyNode is a leaf.
- * @return true, if this HierarchyNode is a leaf. Otherwise - false.
- */
- public virtual Boolean isLeaf(){
- return children.isEmpty();
- }
- /**
- * An override of the equals method.
- * @param obj the reference object with which to compare.
- * @return tue if this object is the same as the obj
- * argument; false otherwise.
- */
- public Boolean equals(Object o) {
- if (!(o instanceof HierarchyNode)) return false;
- HierarchyNode that = (HierarchyNode) o;
- if (parent != null ? !parent.equals(that.parent) : that.parent != null) return false;
- //simple check child lists by hashcode to avoid recursive stack depth reach.
- if (children != null ? !(children.hashCode() == that.children.hashCode()) : that.children != null) return false;
- if (name != null ? !name.equals(that.name) : that.name != null) return false;
- return id != null ? id.equals(that.id) : that.id == null;
- }
- /**
- * An override of the hashCode method.
- * @return a hash code value for this object.
- */
- public override Integer hashCode() {
- Integer result = parent != null ? parent.hashCode() : 0;
- //excluded to avoid recursive stack depth reach.
- //result = 31 * result + (children != null ? children.hashCode() : 0);
- result = 31 * result + (name != null ? name.hashCode() : 0);
- result = 31 * result + (id != null ? id.hashCode() : 0);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement