Getting Groovy

Doug Sabers, Object Partners
Scott Vlaminck, SmartThings

What is Groovy

  • lightweight
  • dynamic
  • optionally typed
  •  object-oriented
  •  runs on the JVM
  • compiles to Java bytecode
  • runs on Java 1.5 and newer
  • open source
  • inspired by languages like Python, Ruby and Smalltalk


Think of Groovy as an extension of the JDK

Plays nice with Java

Groovy provides seamless integration with the the Java Runtime Environment.
Groovy is only a new way of creating ordinary Java classes -- from a runtime perspective
You can call Java classes from your Groovy code and vice versa

Get Groovy

Almost all Java code is valid Groovy code
Let's convert this Java code to Groovy

public class Hello {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.print("hello ");
}
System.out.println("Code People");
}
}

Groovy magic

Groovy allows to write less code by providing some defualts.
The following packages are imported by default in Groovy
  • java.io.* 
  • java.lang.* 
  • java.math.BigDecimal 
  • java.math.BigInteger 
  • java.net.* 
  • java.util.* 
  • groovy.lang.* 
  • groovy.util.* 


So we can write code like this without an import statement:
def now = new Date()
def file = new File("/Users/Doug/test.txt")
def url = new URL('http://www.groovy.mn")
def list = new ArrayList()


Using Def

def is a keyword to replace a type name when declaring a variable. Basically it means we don't want to define the type ourselves. This is called "duck typing". The type of the variable is determined at runtime. 

Code Examples

Groovy Strings

  • Known as GStrings
  • Surrounded by double quotes
  • regular strings are surrounded by single quotes
  • can contain Groovy expressions inside ${ }


def fname = "Doug"
def lname = "Sabers"

println "Hello, my name is ${fname} ${lname}"

Groovy List and Maps

def myList = [2014, 1, 2, -9, 0, 8987987987]
println myList[0]
println myList.size()

def myMap = ["name": "Doug", "age": 37, "favNumber": 87.13 ]
println myMap.name
println myMap["age"]


Groovy Truth

def a = true
def b = true
def c = false

assert a
assert a && b
assert a || c
assert !c

code examples

closures

You can create chunks of code that can be passed around
def square = {it * it }

square(8)

Reading a file

def file = new File("/Users/Doug/Desktop/dougfile.txt")
file.each {
    println it
}

Regular expressions

I had a problem that could only be solved by regex, now I have 2 problems...

import java.util.regex.Pattern

Pattern myPattern = ~/\p{Digit}{1,3}-\p{Digit}{1,2}-\p{Digit}{1,4}/

def var = "1-2-2"
var.matches(myPattern)


Testing your java code with Spock

  • Already using Java JUnit? converting to Spock is easy and fun
  • Detailed information using Groovy's power asserts
  • Makes tests readable
  • Compatible with JUnit  - uses JUnits reporting capabilities
  • Better Mocking
  • https://code.google.com/p/spock/
  • Spock Web Console for playing -http://meetspock.appspot.com/






Groovy is everywhere

Groovy - http://groovy.codehaus.org/
Grails - full stack web application framework www.grails.org
GPars for concurrency - http://gpars.codehaus.org/
Gradle for build automation - http://www.gradle.org/
Geb - groovy wrapper around WebDriver - http://www.gebish.org/
RatPack - toolkit for building high performance web apps - http://ratpack.io
Gaelyk - Deploy apps on Google App Engine http://gaelyk.appspot.com/
GroovyFX -  Groovy binding for JavaFX 
http://groovyfx.org/
Griffon -  Groovy desktop applications
GVM -  tool for managing parallel Versions of multiple SDKs on most Unix based systems 

Want to learn more?

Groovy User Group of MN - www.groovy.mn


Questions?

Getting Groovy

By Doug Sabers

Getting Groovy

  • 388
Loading comments...

More from Doug Sabers