// Hello World println "Hello world!" /* Variables: You can assign values to variables for later use */ def x = 1 println x x = new java.util.Date() println x x = -3.1499392 println x x = false println x x = "Groovy!" println x /* Collections and maps */ //Creating an empty list def technologies = [] /*** Adding a elements to the list ***/ // As with Java technologies.add("Grails") // Left shift adds, and returns the list technologies << "Groovy" // Add multiple elements technologies.addAll(["Gradle","Griffon"]) /*** Removing elements from the list ***/ // As with Java technologies.remove("Griffon") // Subtraction works also technologies = technologies - 'Grails' /*** Iterating Lists ***/ // Iterate over elements of a list technologies.each { println "Technology: $it"} technologies.eachWithIndex { it, i -> println "$i: $it"} /*** Checking List contents ***/ //Evaluate if a list contains element(s) (boolean) contained = technologies.contains( 'Groovy' ) // Or contained = 'Groovy' in technologies // To sort without mutating original, you can do: sortedTechnologies = technologies.sort( false ) //Replace all elements in the list Collections.replaceAll(technologies, 'Gradle', 'gradle') //Shuffle a list Collections.shuffle(technologies, new Random()) //Clear a list technologies.clear() //Creating an empty map def devMap = [:] //Add values devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] devMap.put('lastName','Perez') //Iterate over elements of a map devMap.each { println "$it.key: $it.value" } devMap.eachWithIndex { it, i -> println "$i: $it"} //Evaluate if a map contains a key assert devMap.containsKey('name') //Get the keys of a map println devMap.keySet() class Foo { // read only property final String name = "Roberto" // read only property with public getter and protected setter String language protected void setLanguage(String language) { this.language = language } // dynamically typed property def lastName } /* Logical Branching and Looping */ //Groovy supports the usual if - else syntax def x = 3 if(x==1) { println "One" } else if(x==2) { println "Two" } else { println "X greater than Two" } //Groovy also supports the ternary operator: def y = 10 def x = (y > 1) ? "worked" : "failed" assert x == "worked" //Groovy supports 'The Elvis Operator' too! //Instead of using the ternary operator: displayName = user.name ? user.name : 'Anonymous' //We can write it: displayName = user.name ?: 'Anonymous' //For loop //Iterate over a range def x = 0 for (i in 0 .. 30) { x += i } //Iterate over a list x = 0 for( i in [5,3,2,1] ) { x += i } //Iterate over an array array = (0..20).toArray() x = 0 for (i in array) { x += i } //Iterate over a map def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] x = 0 for ( e in map ) { x += e.value } def technologies = ['Groovy','Grails','Gradle'] technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() } def user = User.get(1) def username = user?.username def clos = { println "Hello World!" } def sum = { a, b -> println a+b } sum(2,4) def x = 5 def multiplyBy = { num -> num * x } println multiplyBy(10) def clos = { print it } clos( "hi" ) def cl = {a, b -> sleep(3000) // simulate some time consuming processing a + b } mem = cl.memoize() def callClosure(a, b) { def start = System.currentTimeMillis() mem(a, b) println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs." } callClosure(1, 2) //Another example: import groovy.transform.TypeChecked @TypeChecked Integer test() { Integer num = "1" Integer[] numbers = [1,2,3,4] Date date = numbers[1] return "Test" } //CompileStatic example: import groovy.transform.CompileStatic @CompileStatic int sum(int x, int y) { x + y } assert sum(2,5) == 7