groovy.transform
Annotation Type EqualsAndHashCode


@Documented
@Retention(value=RUNTIME)
@Target(value=TYPE)
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.

Since:
1.8.0
Author:
Paul King
See Also:
HashCodeHelper

Optional Element Summary
 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
 

excludes

public abstract String excludes
Comma separated list of field and property names to exclude from equals and hashCode calculations

Default:
""

callSuper

public abstract boolean callSuper
Whether to include super in equals and hashCode calculations

Default:
false

includeFields

public abstract boolean includeFields
Include fields as well as properties in equals and hashCode calculations

Default:
false

Copyright © 2003-2010 The Codehaus. All rights reserved.