Groovy:生成equals和hashCode方法 [英] Groovy: generate equals and hashCode methods
问题描述
如果我有一个简单的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()
来获得相同的结果如上所述。
在我走下这条路之前,我只想检查其他人是否找到了解决这个问题的好办法。我对自己的解决方案有点小心,因为弄乱了 谢谢, 我不是一个时髦的开发人员,但我从groovy 1.8了解到,您可以使用类型上的@EqualsAndHashCode调用AST转换。 If I have a simple Groovy class such as Although I could write (or use an IDE to generate) 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 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 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屋! equals()或
hashCode()
Don class Address {
Integer streetNumber
String streetName
String state
String zip
Country country
}
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);
}
.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.equals()
or hashCode()
are potentially dire and hard to track down.