Entity Equals
Posted by evanx on June 28, 2007 at 8:01 AM PDT
href="http://aptframework.dev.java.net/jelly/entityEquals.html">
border="0" width="32" height="32" align="left" hspace="8"/>Click here to read "Entity Equals"
Part of the "Jelly Beans" part of a trilogy in 42 parts
Let's look at neatening up those messy equals() and hashCode() methods in our entities.
Code Snippet
public class Feed {
private int id;
private String uri;
...
private Comparable[] values() {
return new Comparable[] {id, uri};
}
public boolean equals(Object object) {
if (object instanceof Feed) {
Feed feed = (Feed) object;
return typeHelper.equals(values(), feed.values());
}
return false;
}
public int hashCode() {
return typeHelper.hashCode(values());
}
}
where we implement a values() method to return an array of our IDs et al
for hashCode() and equals(), and utilise a helper class.
Blog Links >>
- Login or register to post comments
- Printer-friendly version
- evanx's blog
- 1156 reads





