|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
@Documented @Retention(value=RUNTIME) @Target(value=TYPE) public @interface EqualsAndHashCode
Class annotation used to assist in creating appropriate equals()
and hashCode()
methods.
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
Optional Element Summary | |
---|---|
boolean |
callSuper
Whether to include super in equals and hashCode calculations |
java.lang.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 java.lang.String excludes
public abstract boolean callSuper
public abstract boolean includeFields
|
Copyright © 2003-2010 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |