我是否使用Google Guava正确实现了equals和hashCode? [英] Did I implement equals and hashCode correctly using Google Guava?

查看:186
本文介绍了我是否使用Google Guava正确实现了equals和hashCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate并需要覆盖equals和hashCode()。我选择使用google-guava的equals和hashCode助手。

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.

我已经获得/设置了 idImage 和 filePath

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;
    }
}

编辑:

进入继承并获得样本这里

推荐答案

instanceof <的问题/ code>运算符是考虑到多态性的,如果我可以这样说的话。

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

比如说,你这样做:

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

然后你这样做:

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

boolean this_will_be_true = ie.equals (advanced_ie);

顾名思义,等于来电将返回true,因为 instanceof 运算符。

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

我知道这听起来像基本的东西,大多数人都知道它,但它很容易忘记它。现在,如果您想要这种行为,那么没关系,您正确地实现了等于。但是如果你认为 ImageEntity 对象不能等于(假设的) AdvancedImageEntity 对象,那么要么声明 ImageEntity 最终或忘记 instanceof 并实施等于这样的方法:

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;
}

这将检查对象的真实类型,无论引用的类型是什么。如果 obj 参数是子类的实例,它将滑动 instanceof 。但是 getClass 要严格得多,不允许这样做。

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:我不是说 instanceof 是坏的,不应该使用。我只是说你必须意识到这种特殊情况并决定是否使用它考虑到这一点。

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.

这篇关于我是否使用Google Guava正确实现了equals和hashCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆