7. initial idea was to make a little
dynamic language which compiles
directly to Java classes and provides
all the nice (alleged) productivity
benefits
- James Strachan
http://radio-weblogs.com/0112098/2003/08/29.html
39. Bringing in Order
class Person {
String name
int age
}
def geeks = [
new Person(name: 'Raj', age: 30),
new Person(name: 'Leonard', age: 35),
new Person(name: 'Sheldon', age: 32),
new Person(name: 'Sheldon', age: 25),
new Person(name: 'Penny', age: 35)
]
44. geeks.sort(false) { a, b -> a.age <=> b.age}
Original:
[Person(Raj, 30),
Person(Leonard, 35),
Person(Sheldon, 32),
Person(Sheldon, 25),
Person(Penny, 35)]
Sorted:
[Person(Sheldon, 25),
Person(Raj, 30),
Person(Sheldon, 32),
Person(Leonard, 35),
Person(Penny, 35)]
Sort By Age
45. Sort By Age, Name
int compareTo(Person other) {
if (this.is(other)) {
return 0
}
java.lang.Integer value = 0
value = this.age <=> other.age
if ( value != 0) {
return value
}
value = this.name <=> other.name
if ( value != 0) {
return value
}
return 0
}
46. geeks.sort(false, { a, b ->
[{it.age}, {it.name}].findResult { c ->
c(a) <=> c(b) ?: null
}
})
Original:
[Person(Raj, 30),
Person(Leonard, 35),
Person(Sheldon, 32),
Person(Sheldon, 25),
Person(Penny, 35)]
Sorted:
[Person(Sheldon, 25),
Person(Raj, 30),
Person(Sheldon, 32),
Person(Leonard, 35),
Person(Penny, 35)]
Sort By Age, Name
47. @ToString
@Sortable(includes = "age, name")
class Person {
String name
int age
}
Original:
[Person(Raj, 30),
Person(Leonard, 35),
Person(Sheldon, 32),
Person(Sheldon, 25),
Person(Penny, 35)]
Sort By Age, Name
Sorted:
[Person(Sheldon, 25),
Person(Raj, 30),
Person(Sheldon, 32),
Person(Leonard, 35),
Person(Penny, 35)]
69. Rules to make a class immutable
1. Don’t provide any mutators
2. Ensure that the class can’t be extended
3. Make all fields final
4. Make all fields private
5. Ensure exclusive access to any mutable
components
70. class ImmutableClass{
private final def field1
private final def field2
//...
private final def field10
public ImmutableClass(f1, f2,… f10){
//initialization
}
}
82. Take Away
Some of the ‘Effective Java’ already built into
the language
AST Transformations reduce the effort to
implement few more
Effective Java implementations may not always
be effective Groovy implementations (Traps)
83. Take Away
Programming languages can reduce the
friction to implement good practices
The power of AST transformations is
available to the developers