The default generated hashCode and equals implementations are ugly at best.
Is it possible to make eclipse generate ones from HashCodeBuilder and EqualsBuilder, and perhaps even a toString with ToStringBuilder?
The default generated hashCode and equals implementations are ugly at best.
Is it possible to make eclipse generate ones from HashCodeBuilder and EqualsBuilder, and perhaps even a toString with ToStringBuilder?
You can do that with Code Templates in Eclipse.
Here's a solution that I found with examples of HashCodeBuilder and EqualsBuilder.
Template EqualsBuilder:
public boolean equals(Object o) {
boolean result = false;
if (this == o) {
result = true;
} else if (o instanceof $CLASSNAME$) {
$CLASSNAME$ other = ($CLASSNAME$) o;
result = new org.apache.commons.lang.builder.EqualsBuilder()
.append($END$
.isEquals();
}
return result;
}
Template HashCodeBuilder:
public int hashCode() {
return new org.apache.commons.lang.builder.HashCodeBuilder()
.append( $END$ )
.toHashCode();
}
I use the Eclipse plugin called "Commonclipse"
After installation, you see a new context menu item "commonclipse" when you right click within a java source file. It can generate equals, hashcode, toString and compareTo methods based on the Apache commons libraries.
To install it, use this from within eclipse update: http://commonclipse.sourceforge.net
Eclipse java code templates for eclipse 3.5.0, derived from Bruno Conde's templates:
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
} else if (obj == this) {
return true;
} else if (obj.getClass() != this.getClass()) {
return false;
}
${enclosing_type} other = (${enclosing_type}) obj;
return new EqualsBuilder()//
.appendSuper(super.equals(other))//
.append(${cursor})//
.isEquals();
}
and
@Override
public int hashCode() {
return new HashCodeBuilder(${cursor})//
.append()//
.toHashCode();
}