You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kerby@directory.apache.org by Emmanuel Lécharny <el...@gmail.com> on 2016/01/08 19:59:12 UTC

eqlas() method

Hi guys,

I'm seeing a lot of equals() methods being implemented this way :

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

This is not the best practice. Here is what should be used instead :


    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof EncryptedData) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

The (!(o instanceof <yourClass>)) test will always return false if 'o'
is not an instance of <yourClass> *and* when 'o' is null.




RE: eqlas() method

Posted by "Zheng, Kai" <ka...@intel.com>.
OK. Sounds good. I guess my IDE should be upgraded so it will can generate the better codes ...

-----Original Message-----
From: Emmanuel Lécharny [mailto:elecharny@gmail.com] 
Sent: Saturday, January 09, 2016 2:59 AM
To: kerby@directory.apache.org
Subject: eqlas() method

Hi guys,

I'm seeing a lot of equals() methods being implemented this way :

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

This is not the best practice. Here is what should be used instead :


    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof EncryptedData) {
            return false;
        }

        EncryptedData that = (EncryptedData) o;
        ...

The (!(o instanceof <yourClass>)) test will always return false if 'o'
is not an instance of <yourClass> *and* when 'o' is null.