About Me

Mein Bild
Freelancing software developer / architect, mobile geek. Topics: Software technologies (java, groovy, agile), politics, web, science, green, mobile, android.
Homepage: www.tutaona.com

"The absence of limitations is the enemy of art." - Orson Welles

Donnerstag, 30. August 2012

EqualsAndHashCode

Today I stumbled over a AST transformation that I'd never noticed before, but I'm sure I'm going to use it frequently from now on. A simple way to make two instances with the same values equal.

http://groovy.codehaus.org/gapi/groovy/transform/EqualsAndHashCode.html


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

Now that's cool.

Keine Kommentare:

Kommentar veröffentlichen