Transcript
Page 1: 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

Page 2: How to get Groovy with Java 8

2

Groovy devs tend to look down on Java

Severus Snape - copyright Warner Bros. under fair use

Page 3: How to get Groovy with Java 8

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

Page 4: How to get Groovy with Java 8

But it’s just syntax sugar!

4

Page 5: How to get Groovy with Java 8

Readable vs comprehensible

5

Page 6: How to get Groovy with Java 8

6

Mrs Dalloway (Virginia Woolf)

vs

Macbeth (Shakespeare)

The Firm (John Grisham)

vs

Page 7: How to get Groovy with Java 8

Any aid to comprehension is good

7

Page 8: How to get Groovy with Java 8

Aside

8

Code as if you’re the poor sod who has to maintain it later

Page 9: How to get Groovy with Java 8

But now…

9

Lambda functions + Streams API

Java is superior !!!! Mwuhaahaahaa

Page 10: How to get Groovy with Java 8

10

has a stupid name is slow is a “poor language” has no type safety

Groovy:

Page 11: How to get Groovy with Java 8

Java vs Groovy

11

Challenge!

Page 12: How to get Groovy with Java 8

Groovy over Java 8

12

Type inferenceUseful literals

Scripts

Regular expressions

Page 13: How to get Groovy with Java 8

Don’t forget

13

Groovy JDKOperator overloading

JSON/XML generation & parsing

Property syntax

Page 14: How to get Groovy with Java 8

Can Java do Gradle builds?

14

Page 15: How to get Groovy with Java 8

Or a Spock test?

15

Page 16: How to get Groovy with Java 8

16

So Java still sucks, right?

Page 17: How to get Groovy with Java 8

Yes, Groovy is more expressive

17

Page 18: How to get Groovy with Java 8

But Project Coin + Java 8 are a big improvement

18

Page 19: How to get Groovy with Java 8

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

Page 20: How to get Groovy with Java 8

JVM bugs

20

Page 21: How to get Groovy with Java 8

In @CompileStatic we trust?

21

1 of 66

Page 22: How to get Groovy with Java 8

Risk assessment is crucial

22

Page 23: How to get Groovy with Java 8

Example approach: Ratpack

• Core library in Java

• Groovy API

• Fully type safe if desired

• Apps can be written in any JVM language

23

Page 24: How to get Groovy with Java 8

Demo

24

Page 25: How to get Groovy with Java 8

Don’t view Groovy & Java as competing languages

25

Page 26: How to get Groovy with Java 8

26

Let’s be friends!

Black and Orange Kittens Hugging By Alarob (Creative Commons)

Page 27: How to get Groovy with Java 8

So what does Java 8 do for Groovy?

27

Page 28: How to get Groovy with Java 8

Closures in place of lambdas

28

Page 29: How to get Groovy with Java 8

With type safety

29

This call satisfies type checker!

Page 30: How to get Groovy with Java 8

Groovy coerces closures to single-method interfaces

30

Page 31: How to get Groovy with Java 8

Lambda functions based on invoke dynamic & method handles

31

Page 32: How to get Groovy with Java 8

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

Page 33: How to get Groovy with Java 8

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

Page 34: How to get Groovy with Java 8

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

Page 35: How to get Groovy with Java 8

APIs

35

Page 36: How to get Groovy with Java 8

Dates & Times

36

Page 37: How to get Groovy with Java 8

Groovy TimeCategory

37

Page 38: How to get Groovy with Java 8

Groovy JDK enhancements incoming?

38

Page 39: How to get Groovy with Java 8

Enhanced Java 7 File NIO

39

Page 40: How to get Groovy with Java 8

Streams API

40

Page 41: How to get Groovy with Java 8

Streams API

• Richer than Groovy JDK

• Lazy collections

• Simple parallelisation

• Typed functions

• map(), mapToInt(), mapToObj(), etc.

41

Page 42: How to get Groovy with Java 8

What about default methods?

42

Page 43: How to get Groovy with Java 8

Default method in interface

43

Page 44: How to get Groovy with Java 8

Better option: Groovy traits

44

Page 45: How to get Groovy with Java 8

Example

45

Page 46: How to get Groovy with Java 8

Demo

46

Page 47: How to get Groovy with Java 8

Groovy traits

• Supports state (properties & fields)

• Multiple inheritance

• default conflict resolution

• user-specified conflict resolution

• Ideal for object composition

47

Page 48: How to get Groovy with Java 8

And remember, all the Groovy stuff works on JDK 6+

48

Page 49: How to get Groovy with Java 8

Summary

49

Page 50: How to get Groovy with Java 8

50

Thank you!

Page 51: How to get Groovy with Java 8

Q & A

51


Top Related