groovy.transform
[Java] Annotation Type EqualsAndHashCode
java.lang.Object
groovy.transform.EqualsAndHashCode
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.EqualsAndHashCodeASTTransformation")
public @interface EqualsAndHashCode
Class annotation used to assist in creating appropriate 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.
- Authors:
- Paul King
- See Also:
- HashCodeHelper
- Since:
- 1.8.0
Copyright © 2003-2010 The Codehaus. All rights reserved.