Thibault Kruse created LANG-1084:
------------------------------------

             Summary: ObjectUtils should have type-safe equals() method
                 Key: LANG-1084
                 URL: https://issues.apache.org/jira/browse/LANG-1084
             Project: Commons Lang
          Issue Type: Improvement
            Reporter: Thibault Kruse
            Priority: Minor


Commonly implementation of Object.equals() perform an instanceof check before 
casting and then comparing properties.

That means that any code like a.equals(b) does not imply any compile-time type 
checking.

However it can be very desirable to check types at compiletime in many (though 
not all) situations. E.g. consider this code:

Person a = getPersonViaX();
Person b = getPersonViaY();
assert !a.equals(b);

this code is typesafe at compiletime. If getPersonViaY() changes the return 
type to something that does not extend Person, this will fail to compile.

Now consider this refactored:

assert !getPersonViaX().equals(getPersonViaY());

In this case the change to getPersonViaY() would go unnoticed, both at 
compiletime AND at runtime (if equals merely returns false in the instanceof 
check of the most common equals method design).

Based on this blogpost:
http://rickyclarkson.blogspot.de/2006/12/making-equalsobject-type-safe.html

I suggest a typesafe equals method for situations like above:
public static <T,U extends T> boolean equalT(T t,U u)
{ return t.equals(u); }

and possible a typesafe notEquals method as well.

I am dispassionate about naming of the method, might as well be 
typeSafeEquals() or whatever fits best into apache commons.




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to