I was wondering what are key differences between Guava vs Apache Commons with respect to equals and hashCode builders.
equals:
Apache Commons:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, other.field1)
.append(field2, other.field2)
.isEquals();
}
Guava:
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) { return false; }
MyClass other = (MyClass) obj;
return Objects.equal(this.field1, other.field1)
&& Objects.equal(this.field1, other.field1);
}
hashCode:
Apache Commons:
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(field1)
.append(field2)
.toHashCode();
}
Guava:
public int hashCode() {
return Objects.hashCode(field1, field2);
}
One of the key difference appears to be improved code readability with Guava's version.
I couldn't find more information from https://code.google.com/p/guava-libraries/wiki/CommonObjectUtilitiesExplained. It would be useful to know more differences (especially any performance improvement?) if there are any.