From 5832ef0286430d04484b70d49c73e081a80ec9c7 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 17 Mar 2017 19:28:59 -0400 Subject: Add more toString/hashCode/equals --- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 36 +++++++++++++---------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Tree.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index 52414d2..68da7ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -301,28 +301,34 @@ public class Tree implements ITree { } @Override - public boolean equals(Object other) { - if(!(other instanceof Tree)) return false; + public int hashCode() { + final int prime = 31; + int result = 1; - @SuppressWarnings("unchecked") - Tree otr = (Tree) other; + result = prime * result + childCount; + result = prime * result + ((children == null) ? 0 : children.hashCode()); + result = prime * result + ((data == null) ? 0 : data.hashCode()); - if(!otr.data.equals(data)) return false; - - if(children == null && otr.children == null) return true; + return result; + } - if(children == null && otr.children != null) return false; - if(children != null && otr.children == null) return false; + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; - if(children.getSize() != otr.children.getSize()) return false; + Tree other = (Tree) obj; - int childNo = 0; + if(data == null) { + if(other.data != null) return false; + } else if(!data.equals(other.data)) return false; - for(ITree child : children) { - if(!otr.children.getByIndex(childNo).equals(child)) return false; + if(childCount != other.childCount) return false; - childNo += 1; - } + if(children == null) { + if(other.children != null) return false; + } else if(!children.equals(other.children)) return false; return true; } -- cgit v1.2.3