How to get Groovy with Java 8
description
Transcript of How to get Groovy with Java 8
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
How to get Groovy with Java 8Peter Ledbrook
w: http://www.cacoethes.co.uk/ e: [email protected] t: @pledbrook
2
Groovy devs tend to look down on Java
Severus Snape - copyright Warner Bros. under fair use
Show me filtering
3
List<String> filterList(List<String items) { List<String> result = new ArrayList<>(items.size()); for (String item : items) { if (item.startsWith("idx_")) { result.add(item); } } return result; }
items.findAll { it.startsWith("idx_") }
compared to
But it’s just syntax sugar!
4
Readable vs comprehensible
5
6
Mrs Dalloway (Virginia Woolf)
vs
Macbeth (Shakespeare)
The Firm (John Grisham)
vs
Any aid to comprehension is good
7
Aside
8
Code as if you’re the poor sod who has to maintain it later
But now…
9
Lambda functions + Streams API
Java is superior !!!! Mwuhaahaahaa
10
has a stupid name is slow is a “poor language” has no type safety
Groovy:
Java vs Groovy
11
Challenge!
Groovy over Java 8
12
Type inferenceUseful literals
Scripts
Regular expressions
Don’t forget
13
Groovy JDKOperator overloading
JSON/XML generation & parsing
Property syntax
Can Java do Gradle builds?
14
Or a Spock test?
15
16
So Java still sucks, right?
Yes, Groovy is more expressive
17
But Project Coin + Java 8 are a big improvement
18
Java advantages
• A pretty stable language and runtime
• Commitment to backwards/forwards compatibility
• Consider erasure and target compatibility
• Less affected by JVM bugs than other languages
• Other JVM languages have to integrate with it
• Few binary incompatibilities
19
JVM bugs
20
In @CompileStatic we trust?
21
1 of 66
Risk assessment is crucial
22
Example approach: Ratpack
• Core library in Java
• Groovy API
• Fully type safe if desired
• Apps can be written in any JVM language
23
Demo
24
Don’t view Groovy & Java as competing languages
25
26
Let’s be friends!
Black and Orange Kittens Hugging By Alarob (Creative Commons)
So what does Java 8 do for Groovy?
27
Closures in place of lambdas
28
With type safety
29
This call satisfies type checker!
Groovy coerces closures to single-method interfaces
30
Lambda functions based on invoke dynamic & method handles
31
Performance (Java 8, no indy)
32
Fibonacci(1000) Ops/s Error
Groovy 14182.438 324.031
@CompileStatic 19773.641 373.737
Java 19944.132 381.222
Performance (Java 8, indy)
33
Fibonacci(1000) Ops/s Error
Groovy 18917.895 272.131
@CompileStatic 19471.877 376.576
Java 19883.443 312.094
Performance (Java 7, indy)
34
Fibonacci(1000) Ops/s Error
Groovy 19031.570 251.189
@CompileStatic 21464.016 246.329
Java 20181.067 82.275
APIs
35
Dates & Times
36
Groovy TimeCategory
37
Groovy JDK enhancements incoming?
38
Enhanced Java 7 File NIO
39
Streams API
40
Streams API
• Richer than Groovy JDK
• Lazy collections
• Simple parallelisation
• Typed functions
• map(), mapToInt(), mapToObj(), etc.
41
What about default methods?
42
Default method in interface
43
Better option: Groovy traits
44
Example
45
Demo
46
Groovy traits
• Supports state (properties & fields)
• Multiple inheritance
• default conflict resolution
• user-specified conflict resolution
• Ideal for object composition
47
And remember, all the Groovy stuff works on JDK 6+
48
Summary
49
50
Thank you!
Q & A
51