You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Duncan Jones (JIRA)" <ji...@apache.org> on 2012/09/22 20:20:07 UTC
[jira] [Commented] (LANG-809) Encapsulate trival equal test into a
method to avoid repeating over and over.....
[ https://issues.apache.org/jira/browse/LANG-809?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13461233#comment-13461233 ]
Duncan Jones commented on LANG-809:
-----------------------------------
Having given this some thought, I'm not sure there is an easy solution. Firstly, if we create a method as you've suggested:
{code}
boolean possiblyEquals(Object lhs, Object rhs) {
if (rhs == null) { return false; }
if (lhs == rhs) { return true; } // definitely equal, but how to know?
return (lhs.getClass() == rhs.getClass()); // possibly equal
}
{code}
We must express three possible results (they are equal, they are not equal, we are not sure yet) with a binary value. Not only is this confusing, it also means we will continue to evaluate the EqualsBuilder even if we already know the object is equal.
I then considered if we could add a new appendXXX method, but the best we can do is as follows:
{code}
public boolean equals(Object obj) {
// Have to check this, otherwise we cannot cast
if (obj.getClass() != getClass()) {
return false;
}
// Have to do this, in order to get access to fields
MyClass rhs = (MyClass) obj;
// Have to do this, otherwise NullPointerException when accessing fields
if (rhs == null) {
return false;
}
return new EqualsBuilder()
// this method can then only do a basic (this == obj) check
.appendBasic(this, obj)
.appendSuper(super.equals(obj))
.append(field1, rhs.field1)
.append(field2, rhs.field2)
.append(field3, rhs.field3)
.isEquals();
}
{code}
If you can think of an alternative, please comment! Otherwise, this might need to be closed as won't fix (i.e. can't improve).
> Encapsulate trival equal test into a method to avoid repeating over and over.....
> ---------------------------------------------------------------------------------
>
> Key: LANG-809
> URL: https://issues.apache.org/jira/browse/LANG-809
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.builder.*
> Affects Versions: 3.1
> Environment: Windows 7
> Reporter: Colbert Philippe
> Priority: Minor
> Labels: Encapsulate, EqualsBuilder, in, test, trivial
>
> In class EqualsBuilder, the documentation gives sample code on how to use EqualsBuilder. The proper usage of class EqualsBuilder is a bit long. My suggestion is to encapsulate the following trivial test into a method inside the class EqualsBuilder. You call it the new method trivalTest(Object obj1, Object obj2).
> // This is the code that should be put in a method to avoid repeating....
> if (obj == null) { return false; }
> if (obj == this) { return true; }
> if (obj.getClass() != getClass()) {
> return false;
> }
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira