You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Sahil Soni (Jira)" <ji...@apache.org> on 2020/02/28 09:05:00 UTC

[jira] [Created] (LANG-1520) ObjectUtils.equals can use comparable if that's present.

Sahil Soni created LANG-1520:
--------------------------------

             Summary: ObjectUtils.equals can use comparable if that's present.
                 Key: LANG-1520
                 URL: https://issues.apache.org/jira/browse/LANG-1520
             Project: Commons Lang
          Issue Type: Improvement
          Components: lang.*
    Affects Versions: 3.9
            Reporter: Sahil Soni


{{ObjectUtils.equals(Object, Object)}} is marked for deprecation in favour of {{java.util.Objects.equals(Object, Object)}}

We can improve {{ObjectUtils.equals}} to handle comparable objects,
 For example, class A does not implement equals method, but does implements Comparable interface. ObjectUtils.equals() will say its different, but compareTo will say they are equal.

To handle this, we can change the implementation to something like:
{code:java}
//instanceof checks for null, `null instanceof Comparable` is false, so this condition is null safe
        if (firstValue instanceof Comparable && secondValue instanceof Comparable) {
            return ((Comparable) firstValue).compareTo(secondValue) == 0;
        }
        return Objects.equals(firstValue, secondValue);
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)