Groovy:生成equals和hashCode方法 [英] Groovy: generate equals and hashCode methods

查看:203
本文介绍了Groovy:生成equals和hashCode方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个简单的Groovy类,例如

  class Address {

Integer streetNumber
字符串streetName
字符串状态
字符串zip
国家国家
}

虽然我可以编写(或使用IDE来生成) hashCode 等于方法:

  boolean equals(o){
if(this.is(o))return true;

if(!o || getClass()!= o.class)return false;

地址=(地址)o;

if(streetNumber?!streetNumber.equals(that.streetNumber):that.streetNumber!= null)return false;
if(streetName?!streetName.equals(that.streetName):that.streetName!= null)return false;
if(state?!state.equals(that.state):that.state!= null)return false;
if(zip?!zip.equals(that.zip):that.zip!= null)return false;
if(country?!zip.equals(that.zip):that.zip!= null)return false;

返回true;


int hashCode(){
int result =(streetNumber?streetNumber.hashCode():0);
result = 31 * result +(streetName?streetName.hashCode():0);
result = 31 * result +(state?state.hashCode():0);
result = 31 * result +(zip?zip.hashCode():0);
return 31 * result +(country?country.hashCode():0);
}

尽管这样可以很好地工作,但我觉得我可以更好地使用Groovy的活力在少得多的代码中实现相同的目标。想到的一个方法是使用 .properties 来获取对象属性名称和值的映射。然后我可以迭代这些属性,在每个属性上调用 hashCode() equals()来获得相同的结果如上所述。



在我走下这条路之前,我只想检查其他人是否找到了解决这个问题的好办法。我对自己的解决方案有点小心,因为弄乱了 equals()或 hashCode()

谢谢,
Don

解决方案

我不是一个时髦的开发人员,但我从groovy 1.8了解到,您可以使用类型上的@EqualsAndHashCode调用AST转换。


If I have a simple Groovy class such as

class Address {

  Integer streetNumber
  String streetName
  String state
  String zip
  Country country    
}

Although I could write (or use an IDE to generate) hashCode and equals methods like:

boolean equals(o) {
    if (this.is(o)) return true;

    if (!o || getClass() != o.class) return false;

    Address that = (Address) o;

    if (streetNumber? !streetNumber.equals(that.streetNumber) : that.streetNumber!= null) return false;
    if (streetName? !streetName.equals(that.streetName) : that.streetName!= null) return false;
    if (state? !state.equals(that.state) : that.state!= null) return false;
    if (zip? !zip.equals(that.zip) : that.zip!= null) return false;
    if (country? !zip.equals(that.zip) : that.zip!= null) return false;

    return true;
}

int hashCode() {
    int result = (streetNumber ? streetNumber.hashCode() : 0);
    result = 31 * result + (streetName ? streetName.hashCode() : 0);
    result = 31 * result + (state ? state.hashCode() : 0);
    result = 31 * result + (zip ? zip.hashCode() : 0);
    return 31 * result + (country ? country.hashCode() : 0);
}

Although this will work fine, I feel I could be making better use of Groovy's dynamism to achieve the same thing in a lot less code. One approach that springs to mind is using .properties to get a map of an object's property names and values. I can then iterate over these properties, calling hashCode() or equals() on each one to achieve the same result as above.

Before I go down this path, I just want to check whether anyone else has found a good solution to this problem. I'm a bit wary of rolling my own solution, because the consequences of messing up equals() or hashCode() are potentially dire and hard to track down.

Thanks, Don

解决方案

I'm not a groovy developer, but I understood that from groovy 1.8 you can invoke the AST transformation using @EqualsAndHashCode on the type.

这篇关于Groovy:生成equals和hashCode方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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