@Equiv annotation is one of the features.
If you annotate fields by @Equiv annotations, you can dynamically generate
proper equals() and hashCode() using specified fields.
If you wrote the following groovy code:
class Sample { @Equiv String name @Equiv String value String ignored }
then you could use equals method and hashCode method:
def s1 = new Sample(name:'John', value:'Good', ignored:'Garbage') def s2 = new Sample(name:'John', value:'Good', ignored:'Gold') def s3 = new Sample(name:'John', value:'Bad', ignored:'Garbage') def s4 = new Sample(name:'Mike', value:'Good', ignored:'Garbage') assert s1 == s1 assert s1 == s2 assert s1 != s3 assert s1 != s4 assert s1.hashCode() == s1.hashCode() assert s1.hashCode() == s2.hashCode() assert s1.hashCode() != s3.hashCode() assert s1.hashCode() != s4.hashCode()
I think it's useful for entities, etc. You can keep your classes very simple.
No comments:
Post a Comment