I implement equals() the Java 7 way:
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
MyClass other = (MyClass) obj;
return Objects.equal(myFirstField, other.myFirstField) &&
Objects.equal(mySecondField, other.mySecondField);
}
Is there a way to reduce the code duplication?
I would prefer something like
@Override
public boolean equals(Object obj)
{
if (Objects.equalsEarlyExit(this, obj)) return Objects.equalstEarlyExitResult(this, obj);
MyClass other = (MyClass) obj;
return Objects.equal(myFirstField, other.myFirstField) &&
Objects.equal(mySecondField, other.mySecondField);
}
Or similar.