commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <>
Subject [jira] [Resolved] (LANG-1084) ObjectUtils should have type-safe equals() method
Date Thu, 29 Jan 2015 00:11:36 GMT


Sebb resolved LANG-1084.
    Resolution: Won't Fix

> ObjectUtils should have type-safe equals() method
> -------------------------------------------------
>                 Key: LANG-1084
>                 URL:
>             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:
> 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

View raw message