Skip to content

Commit c03044e

Browse files
SmartyAnshashleyfrieze
authored andcommitted
BAEL-2724_Closures_in_groovy (eugenp#6439)
* BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy * BAEL-2724_Closures_in_groovy
1 parent 4372c6f commit c03044e

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.closures
2+
3+
class Closures {
4+
5+
def printWelcome = {
6+
println "Welcome to Closures!"
7+
}
8+
9+
def print = { name ->
10+
println name
11+
}
12+
13+
def formatToLowerCase(name) {
14+
return name.toLowerCase()
15+
}
16+
def formatToLowerCaseClosure = { name ->
17+
return name.toLowerCase()
18+
}
19+
20+
def count=0
21+
22+
def increaseCount = {
23+
count++
24+
}
25+
26+
def greet = {
27+
return "Hello! ${it}"
28+
}
29+
30+
def multiply = { x, y ->
31+
return x*y
32+
}
33+
34+
def calculate = {int x, int y, String operation ->
35+
36+
//log closure
37+
def log = {
38+
println "Performing $it"
39+
}
40+
41+
def result = 0
42+
switch(operation) {
43+
case "ADD":
44+
log("Addition")
45+
result = x+y
46+
break
47+
case "SUB":
48+
log("Subtraction")
49+
result = x-y
50+
break
51+
case "MUL":
52+
log("Multiplication")
53+
result = x*y
54+
break
55+
case "DIV":
56+
log("Division")
57+
result = x/y
58+
break
59+
}
60+
return result
61+
}
62+
63+
def addAll = { int... args ->
64+
return args.sum()
65+
}
66+
67+
def volume(Closure areaCalculator, int... dimensions) {
68+
if(dimensions.size() == 3) {
69+
70+
//consider dimension[0] = length, dimension[1] = breadth, dimension[2] = height
71+
//for cube and cuboid
72+
return areaCalculator(dimensions[0], dimensions[1]) * dimensions[2]
73+
} else if(dimensions.size() == 2) {
74+
75+
//consider dimension[0] = radius, dimension[1] = height
76+
//for cylinder and cone
77+
return areaCalculator(dimensions[0]) * dimensions[1]
78+
} else if(dimensions.size() == 1) {
79+
80+
//consider dimension[0] = radius
81+
//for sphere
82+
return areaCalculator(dimensions[0]) * dimensions[0]
83+
}
84+
85+
}
86+
87+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.closures
2+
3+
class Employee {
4+
5+
String fullName
6+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.closures
2+
3+
import spock.lang.Specification
4+
5+
class ClosuresUnitTest extends GroovyTestCase {
6+
7+
Closures closures = new Closures()
8+
9+
void testDeclaration() {
10+
11+
closures.print("Hello! Closure")
12+
closures.formatToLowerCaseClosure("Hello! Closure")
13+
14+
closures.print.call("Hello! Closure")
15+
closures.formatToLowerCaseClosure.call("Hello! Closure")
16+
17+
}
18+
19+
void testClosureVsMethods() {
20+
assert closures.formatToLowerCase("TONY STARK") == closures.formatToLowerCaseClosure("Tony STark")
21+
}
22+
23+
void testParameters() {
24+
//implicit parameter
25+
assert closures.greet("Alex") == "Hello! Alex"
26+
27+
//multiple parameters
28+
assert closures.multiply(2, 4) == 8
29+
30+
assert closures.calculate(12, 4, "ADD") == 16
31+
assert closures.calculate(12, 4, "SUB") == 8
32+
assert closures.calculate(43, 8, "DIV") == 5.375
33+
34+
//varags
35+
assert closures.addAll(12, 10, 14) == 36
36+
37+
}
38+
39+
void testClosureAsAnArgument() {
40+
assert closures.volume({ l, b -> return l*b }, 12, 6, 10) == 720
41+
42+
assert closures.volume({ radius -> return Math.PI*radius*radius/3 }, 5, 10) == Math.PI * 250/3
43+
}
44+
45+
void testGStringsLazyEvaluation() {
46+
def name = "Samwell"
47+
def welcomeMsg = "Welcome! $name"
48+
49+
assert welcomeMsg == "Welcome! Samwell"
50+
51+
// changing the name does not affect original interpolated value
52+
name = "Tarly"
53+
assert welcomeMsg != "Welcome! Tarly"
54+
55+
def fullName = "Tarly Samson"
56+
def greetStr = "Hello! ${-> fullName}"
57+
58+
assert greetStr == "Hello! Tarly Samson"
59+
60+
// this time changing the variable affects the interpolated String's value
61+
fullName = "Jon Smith"
62+
assert greetStr == "Hello! Jon Smith"
63+
}
64+
65+
void testClosureInLists() {
66+
def list = [10, 11, 12, 13, 14, true, false, "BUNTHER"]
67+
list.each {
68+
println it
69+
}
70+
71+
assert [13, 14] == list.findAll{ it instanceof Integer && it >= 13}
72+
}
73+
74+
void testClosureInMaps() {
75+
def map = [1:10, 2:30, 4:5]
76+
77+
assert [10, 60, 20] == map.collect{it.key * it.value}
78+
}
79+
80+
}

0 commit comments

Comments
 (0)