groovy.transform
@Documented @Retention(value=RUNTIME) @Target(value=TYPE) public @interface EqualsAndHashCode
equals() and hashCode() methods.
It allows you to write classes in this shortened form:
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
class Person {
String first, last
int age
}
def p1 = new Person(first:'John', last:'Smith', age:21)
def p2 = new Person(first:'John', last:'Smith', age:21)
assert p1 == p2
def map = [:]
map[p1] = 45
assert map[p2] == 45
The @EqualsAndHashCode annotation instructs the compiler to execute an
AST transformation which adds the necessary equals and hashCode methods to the class.
The hashCode() method is calculated using Groovy's HashCodeHelper class
which implements an algorithm similar to the outlined in the book Effective Java.
The equals() method compares the values of the individual properties of the class.HashCodeHelper| Modifier and Type | Optional Element and Description |
|---|---|
boolean |
callSuper
Whether to include super in equals and hashCode calculations
|
String |
excludes
Comma separated list of field and property names to exclude from equals and hashCode calculations
|
boolean |
includeFields
Include fields as well as properties in equals and hashCode calculations
|
public abstract String excludes
public abstract boolean callSuper
public abstract boolean includeFields