forked from adambard/learnxinyminutes-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groovy.html.markdown
434 lines (308 loc) · 9.25 KB
/
groovy.html.markdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
---
language: Groovy
filename: learngroovy.groovy
contributors:
- ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"]
filename: learngroovy.groovy
---
Groovy - A dynamic language for the Java platform [Read more here.](http://www.groovy-lang.org/)
```groovy
/*
Set yourself up:
1) Install SDKMAN - http://sdkman.io/
2) Install Groovy: sdk install groovy
3) Start the groovy console by typing: groovyConsole
*/
// Single line comments start with two forward slashes
/*
Multi line comments look like this.
*/
// 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
// Check for multiple contents
technologies.containsAll(['Groovy','Grails'])
/*** Sorting Lists ***/
// Sort a list (mutates original list)
technologies.sort()
// To sort without mutating original, you can do:
sortedTechnologies = technologies.sort( false )
/*** Manipulating Lists ***/
//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')
//Evaluate if a map contains a value
assert devMap.containsValue('Roberto')
//Get the keys of a map
println devMap.keySet()
//Get the values of a map
println devMap.values()
/*
Groovy Beans
GroovyBeans are JavaBeans but using a much simpler syntax
When Groovy is compiled to bytecode, the following rules are used.
* If the name is declared with an access modifier (public, private or
protected) then a field is generated.
* A name declared with no access modifier generates a private field with
public getter and setter (i.e. a property).
* If a property is declared final the private field is created final and no
setter is generated.
* You can declare a property and also declare your own getter or setter.
* You can declare a property and a field of the same name, the property will
use that field then.
* If you want a private or protected property you have to provide your own
getter and setter which must be declared private or protected.
* If you access a property from within the class the property is defined in
at compile time with implicit or explicit this (for example this.foo, or
simply foo), Groovy will access the field directly instead of going though
the getter and setter.
* If you access a property that does not exist using the explicit or
implicit foo, then Groovy will access the property through the meta class,
which may fail at runtime.
*/
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 = ""
for ( e in map ) {
x += e.value
x += " "
}
assert x.equals("Roberto Grails Groovy ")
/*
Operators
Operator Overloading for a list of the common operators that Groovy supports:
http://www.groovy-lang.org/operators.html#Operator-Overloading
Helpful groovy operators
*/
//Spread operator: invoke an action on all items of an aggregate object.
def technologies = ['Groovy','Grails','Gradle']
technologies*.toUpperCase() // = to technologies.collect { it?.toUpperCase() }
//Safe navigation operator: used to avoid a NullPointerException.
def user = User.get(1)
def username = user?.username
/*
Closures
A Groovy Closure is like a "code block" or a method pointer. It is a piece of
code that is defined and then executed at a later point.
More info at: http://www.groovy-lang.org/closures.html
*/
//Example:
def clos = { println "Hello World!" }
println "Executing the Closure:"
clos()
//Passing parameters to a closure
def sum = { a, b -> println a+b }
sum(2,4)
//Closures may refer to variables not listed in their parameter list.
def x = 5
def multiplyBy = { num -> num * x }
println multiplyBy(10)
// If you have a Closure that takes a single argument, you may omit the
// parameter definition of the Closure
def clos = { print it }
clos( "hi" )
/*
Groovy can memoize closure results [1][2][3]
*/
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)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)
/*
Expando
The Expando class is a dynamic bean so we can add properties and we can add
closures as methods to an instance of this class
http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html
*/
def user = new Expando(name:"Roberto")
assert 'Roberto' == user.name
user.lastName = 'Pérez'
assert 'Pérez' == user.lastName
user.showInfo = { out ->
out << "Name: $name"
out << ", Last name: $lastName"
}
def sw = new StringWriter()
println user.showInfo(sw)
/*
Metaprogramming (MOP)
*/
//Using ExpandoMetaClass to add behaviour
String.metaClass.testAdd = {
println "we added this"
}
String x = "test"
x?.testAdd()
//Intercepting method calls
class Test implements GroovyInterceptable {
def sum(Integer x, Integer y) { x + y }
def invokeMethod(String name, args) {
System.out.println "Invoke method $name with args: $args"
}
}
def test = new Test()
test?.sum(2,3)
test?.multiply(2,3)
//Groovy supports propertyMissing for dealing with property resolution attempts.
class Foo {
def propertyMissing(String name) { name }
}
def f = new Foo()
assertEquals "boo", f.boo
/*
TypeChecked and CompileStatic
Groovy, by nature, is and will always be a dynamic language but it supports
typechecked and compilestatic
More info: http://www.infoq.com/articles/new-groovy-20
*/
//TypeChecked
import groovy.transform.TypeChecked
void testMethod() {}
@TypeChecked
void test() {
testMeethod()
def name = "Roberto"
println naameee
}
//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
```
## Further resources
[Groovy documentation](http://www.groovy-lang.org/documentation.html)
[Groovy web console](http://groovyconsole.appspot.com/)
Join a [Groovy user group](http://www.groovy-lang.org/usergroups.html)
## Books
* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook)
* [Groovy in Action] (http://manning.com/koenig2/)
* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do)
[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/
[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize
[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html