Created
August 14, 2012 17:27
-
-
Save cquinn/3351017 to your computer and use it in GitHub Desktop.
Extended Groovy Expando class that has equals and hashCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Experimental object that could hold general report results. | |
class Expandy extends Expando { | |
boolean equals(Object o) { | |
if (getProperties().size() != o.getProperties().size()) return false | |
for (def i = getProperties().entrySet().iterator(); i.hasNext(); ) { | |
def e = i.next() | |
if (!o.getProperties().containsKey(e.getKey())) return false | |
def ov = o.getProperty(e.getKey()) | |
if (ov != e.getValue()) return false | |
} | |
return true | |
} | |
int hashCode() { | |
int hc = 29 | |
for (def i = getProperties().entrySet().iterator(); i.hasNext(); ) { | |
def e = i.next() | |
hc = 13*hc + e.hashCode() | |
} | |
return hc | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment