1 of 56

Getting

2 of 56

Hubert Klein Ikkink (aka mrhaki)

  • Java developer for many years (started in 1996)
  • Live in the Netherlands (Tilburg)
  • Work at JDriven
  • Enjoying Groovy since 2008
  • Groovy Goodness (and other Gr8 Goodness) blog at http://mrhaki.blogspot.com
  • Follow me on twitter: @mrhaki
  • Book: Gradle Effective Implementation Guide at Packt Publishing

assert ['Hubert','Alexander','Klein','Ikkink'].inject('mr'){ c,n -> c += n[0].toLowerCase() } == 'mrhaki'

3 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

4 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

5 of 56

Workshop structure

  • Partly slideshow
  • Partly hands-on
    • Lots of snippets
    • No "big" Groovy project at the end.
  • All slides and speakers-notes are available here:
    • Presentation: http://bit.ly/GettingGroovyPresentation2013
    • Notes: http://bit.ly/GettingGroovyNotes2013 

6 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

7 of 56

Groovy history

  • Groovy started in August 2003 by James Strachan
  • Open Source (BSD / Apache license)
  • It's a dynamic language compiling to Java Bytecode and executable in the JVM.
  • Language grammer extends Java, with heavy inspiration from  Python, Ruby and Smalltalk
  • Extremely flat learning curve for the Java developer

Groovy is Java on Steroids...

8 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

9 of 56

Getting started

  • How to install
  • Compiling and running
  • Groovy scripts
  • Groovy shell and GroovyConsole
  • Groovy Web Console
  • IDE Support

10 of 56

How to install - requirements

  • Installed JDK >= 6.0
  • A zip-file with Groovy 2.1.x (on your USB stick)

11 of 56

How to install

  • Uncompress the zip file to c:\gr8conf\groovy\ (Windows) or ~/gr8conf/groovy (Mac/Linux).
  • Set a GROOVY_HOME environment variable to the directory from the previous step.
  • Add the [GROOVY_HOME]\bin directory to your system path.
    • Windows: Use your computer's properties to set the environment variable PATH and add %GROOVY_HOME%\bin
    • Linux/Mac OS X: open your shell and set the PATH variable (you can also add it to your profile/bashrc for later)
      • > export PATH=$PATH:$GROOVY_HOME/bin          

12 of 56

How to validate installation

  • To validate your installation, open a console and type the following:
    • groovy -version
    • groovyConsole

13 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

14 of 56

Transforming Java to Groovy

  • Short demo....

public class UsingJava {

    private String name;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Using Java " + getName();

    }

    public static void main(String[] args) {

        UsingJava usingJava = new UsingJava();

        usingJava.setName("Geeks");

        System.out.println(usingJava);

    }

}

15 of 56

What we got

class UsingJava {

    def name

    String toString() {

        "Using Java $name"    }

    static void main(String[] args) {

        def usingJava = new UsingJava(name: ‘Geeks’)

       println usingJava

    }

}

60% less code

16 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

17 of 56

Types

  • Basic types
    • Like Java, but (un)boxed to object counterparts
    • BigDecimal
  • Dynamic types

18 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

19 of 56

String variations including GString

  • Basic strings
  • GStrings
  • Multi-line Strings

20 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

21 of 56

Regular Expressions

  • Pattern Operator (~)
  • Find Operator (=~)
  • Match Operator (==~)

22 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

23 of 56

Objects

  • GroovyBeans
  • Methods / Multimethods
    • default argument value
  • GPath
  • Exceptions

24 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

25 of 56

Control structures

  • Groovy Truth
  • Elvis Operator
  • Switch
  • For-in Loop

26 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

27 of 56

Operators

  • Operator overloading
  • Spaceship Operator
  • Spread-Dot Operator
  • Spread Operator

28 of 56

Operator overloading

Operator

Method

a + b

a.plus(b)

a - b

a.minus(b)

a * b

a.multiply(b)

a ** b

a.power(b)

a / b

a.div(b)

a % b

a.mod(b)

a | b

a.or(b)

a & b

a.and(b)

a ^ b

a.xor(b)

a++ or ++a

a.next()

a-- or --a

a.previous()

a[b]

a.getAt(b)

a[b] = c

a.putAt(b, c)

a << b

a.leftShift(b)

a >> b

a.rightShift(b)

a >>> b

a.rightShiftUnsigned(b)

switch(a) { case(b) : }

b.isCase(a)

29 of 56

Operator overloading

Operator

Method

~a

a.negate()

-a

a.negative()

+a

a.positive()

a == b

a.equals(b)

a != b

! a.equals(b)

a <=> b

a.compareTo(b)

a > b

a.compareTo(b) > 0

a >= b

a.compareTo(b) >= 0

a < b

a.compareTo(b) < 0

a <= b

a.compareTo(b) <= 0

as as type

a.asType(typeClass)

30 of 56

Break

Coffee in the Atrium

31 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

32 of 56

Closures

  • Basic
    • Arguments (it)
  • Turn methods into closures
  • Closures as method parameters
    • Special syntax for last argument
  • Currying

33 of 56

Closures: Hands-on!

  1. Write a three (String) argument closure, where the third argument is optional (defaulting to 'gr8conf'). The result is a concatenation of the three aguments
  2. Print the value of the closure call closure("I'm","going to")
  3. Make a second closure, that curries the first closure, so that the second argument is set to 'love'
  4. Make a method, taking two parameters (the closure and one value), where the value could be  'I', 'we' or 'you'. The method should print out the result of the closure we created at 3)

34 of 56

Closures: Possible solution

def gr8 = { arg1, arg2, arg3 = 'gr8conf' -> 

    "$arg1 $arg2 $arg3"

}

println gr8("I'm","going to")

def cl = gr8.ncurry(1,'love')

def m(arg, closure) {

    println closure.call(arg)

}

m('I',cl)

35 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

36 of 56

Collections

  • Ranges
  • Lists
  • Maps
  • Looping
    • upTo
    • times
    • each/eachWithIndex
    • inject
  • Finding Data
    • find/findAll
    • any/every
  • Grouping Elements

37 of 56

Collections: Hands-on

  1. print the numbers from 1 though 100 one line at the time
  2. make a list of the even numbers between 1 and 100
  3. group the list from 2) by tenth (0,10,20... etc). Tip intdiv() does integer division.
  4. group this map by the value:

        def map = [a:1,b:2,c:1,d:3,e:7,f:2,g:5]

38 of 56

Collections: Possible solution

(1..100).each { println it }

def even = (1..100).findAll { it % 2 == 0 }

println even.groupBy { it.intdiv(10) * 10 }

def map = [a:1,b:2,c:1,d:3,e:7,f:2,g:5]

println map.groupBy { key, value -> value } 

                 // { it.value }

39 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

40 of 56

File I/O

  • No boilerplate
    • text
    • write / <<
  • Resource handling
  • Getting data from the net

41 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

42 of 56

XML

  • Creating XML
  • Slurping XML
  • Creating and Slurping JSON

43 of 56

XML: Hands-on

  1. Print the list of the book title from this url: http://mrhaki.com/books.xml
  2. Get the title of the book with id == 2

44 of 56

XML: Possible solutions

def url = "http://mrhaki.com/books.xml".toURL()

def slurper = new XmlSlurper()

def xml = slurper.parseText(url.text)

println xml.book.collect  {it.title }

println xml.book.find { it.@id == '2' }.title

45 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

46 of 56

AST

  • What is it
  • Examples of build-in transformation
  • @Delegate
  • @Singleton
  • @InheritConstructors
  • @Immutable
  • @EqualsAndHashCode
  • @ToString
  • @Log

47 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

48 of 56

Metaprogramming

  • Category 
  • Mixin
  • MetaClass

49 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

50 of 56

Unittesting

  • Power-asserts
  • Mocks and Stubs
  • Alternative testing frameworks

51 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

52 of 56

Integrate with Java

  • GroovyShell
  • Call Java from Groovy
  • Call Groovy from Java

53 of 56

Integrating with Java

Small demo

54 of 56

Agenda

  • Introduction
  • Workshop structure
  • Groovy language history
  • Get started
  • Transforming Java to Groovy
  • Types
  • String variations including GString
  • Regular Expressions
  • Objects
  • Control structures

  • Operators
  • Closures 
  • Collections 
  • File I/O
  • XML
  • AST
  • Metaprogramming 
  • Unit testing
  • Integrate with Java
  • GR8 stuff in the Groovy Ecosystem

55 of 56

GR8 stuff in the Groovy Ecosystem

  • Grails (Full web stack)
  • Griffon (Full Swing client stack)
  • Gradle (Your Maven replacement)
  • Spock, Easyb, Gmock (Testing/mocking frameworks)
  • GPars (Concurrency made easy)
  • GAELYK (Google App Engine framework)
  • HTTPBuilder (REST), GroovyWS (SOAP)
  • Groovy embeded into products like
    • Hyperic
    • SoapUI
    • Logback
    • Spring

56 of 56

Q & A

Thank you!