Java Reference
In-Depth Information
@EqualsAndHashCode
class Task {
String name
int priority
Date startDate
Date endDate
String toString() { "($name,$priority,$startDate,$endDate)" }
}
Seriously, that's the whole class, and it does include overrides of the
equals
and
hashCode
methods. Groovy classes are public by default, as are Groovy methods. At-
tributes are private by default. Access to an attribute is done through dynamically gener-
ated getter and setter methods, so even though it looks like we're dealing with individual
fields we're actually going through getter and setter methods. Also, Groovy automatically
provides a map-based constructor that eliminates the need for lots of overloaded construct-
ors. The
@EqualsAndHashCode
annotation represents an Abstract Syntax Tree (AST)
transformation that generates the associated methods. Finally, I use a Groovy string with its
parameter substitution capabilities to convert a task into a string.
Groovy Feature
Groovy's dynamic generation capabilities drastically reduce the amount of code required
in a class, letting you focus on the essence rather than the ceremony.
Java also includes checked exceptions, which are a mixed blessing at best. The philosophy
is to catch (no pun intended) problems early in the development cycle, which is also sup-
posed to be an advantage to static typing.
1.1.4. Groovy makes testing Java much easier
Just because a class compiles doesn't mean it's implemented correctly. Just because you've
prepared for various exceptions doesn't mean the code works properly. You've still got to
5
My favorite example of this comes from a friend who used to teach C++ back when that language was shiny and
new. He looked at a student's code, and it was a mess. Then he noticed the first line was
/*
and the last line was
*/
.
He said, “You commented out your entire program.” The student shrugged and said, “That's the only way I could
get it to compile!”