Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did I implement equals and hashCode correctly using Google Guava?

I am using hibernate and need to override equals and hashCode(). I chose to use google-guava's equals and hashCode helpers.

I wanted to know if I am missing something here.

I have get/set methods for idImage and filePath.

@Entity
@Table(name = "IMAGE")
public class ImageEntity {
    private Integer idImage;
    private String filePath;

    @Override
    public int hashCode() {
        return Objects.hashCode(getFilePath());
    }

    @Override
    public boolean equals(final Object obj) {
        if(obj == this) return true;
        if(obj == null) return false;

        if(obj instanceof ImageEntity){
            final ImageEntity otherImage = (ImageEntity) obj;
            return Objects.equal(getFilePath(), otherImage.getFilePath());
        }
        return false;
    }
}

EDIT:

Ran into inheritance and have a sample here

like image 210
brainydexter Avatar asked Feb 13 '12 08:02

brainydexter


2 Answers

You could actually use the Guava EqualsTester to test your equals and hashCode implementation:

new EqualsTester()
     .addEqualityGroup("hello", "h" + "ello")
     .addEqualityGroup("world", "wor" + "ld")
     .addEqualityGroup(2, 1 + 1)
     .testEquals();

It's in the guava testlib.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava-testlib</artifactId>
    <scope>test</scope>
</dependency>

Minor change to your implementation:

@Override public boolean equals(final Object obj) {
    if(obj == this) return true;
    return obj instanceof ImageEntity &&
        Objects.equal(getFilePath(), ((ImageEntity) obj).getFilePath());
}
like image 51
Thomas Jung Avatar answered Oct 27 '22 15:10

Thomas Jung


The problem with the instanceof operator is that it works taking into account polymorphism, if I may say so.

Let's say, for example, that you do this:

public class AdvancedImageEntity extends ImageEntity
{
    //some code here
}

and then you do this:

ImageEntity ie = new ImageEntity ();
AdvancedImageEntity advanced_ie = new AdvancedImageEntity ();

boolean this_will_be_true = ie.equals (advanced_ie);

As the name suggests, that equals call will return true, because of the instanceof operator.

I know this sounds like basic stuff and most people know it, but it's SOOOO damn easy to forget it. Now, if you want that behaviour, then it's fine, you implemented equals correctly. But if you consider that an ImageEntity object must not be equal to an (hypothetical) AdvancedImageEntity object, then either declare ImageEntity to be final OR forget about instanceof and implement your equals method like this:

@Override public boolean equals(final Object obj)
{
    if(obj == this) return true;
    if(obj == null) return false;

    if (getClass ().equals (obj.getClass ()))
    {
        final ImageEntity otherImage = (ImageEntity) obj;

        return Object.equals (getFilePath(), otherImage.getFilePath());
    }

    return false;
}

This will check the object's true type, no matter what type the reference is. If the obj parameter is an instance of a subclass, it would "slip" by instanceof. But getClass is a lot more strict and won't allow it.

PS: I'm not saying that instanceof is bad and should not be used. I'm just saying that you must be aware of this particular situation and decide whether to use it taking this into account.

like image 35
Radu Murzea Avatar answered Oct 27 '22 17:10

Radu Murzea