BAEL-2724_Closures_in_groovy (#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
This commit is contained in:
Anshul Bansal 2019-03-23 07:45:04 -05:00 committed by ashleyfrieze
parent 4372c6fa1c
commit c03044e3ef
3 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package com.baeldung.closures
class Closures {
def printWelcome = {
println "Welcome to Closures!"
}
def print = { name ->
println name
}
def formatToLowerCase(name) {
return name.toLowerCase()
}
def formatToLowerCaseClosure = { name ->
return name.toLowerCase()
}
def count=0
def increaseCount = {
count++
}
def greet = {
return "Hello! ${it}"
}
def multiply = { x, y ->
return x*y
}
def calculate = {int x, int y, String operation ->
//log closure
def log = {
println "Performing $it"
}
def result = 0
switch(operation) {
case "ADD":
log("Addition")
result = x+y
break
case "SUB":
log("Subtraction")
result = x-y
break
case "MUL":
log("Multiplication")
result = x*y
break
case "DIV":
log("Division")
result = x/y
break
}
return result
}
def addAll = { int... args ->
return args.sum()
}
def volume(Closure areaCalculator, int... dimensions) {
if(dimensions.size() == 3) {
//consider dimension[0] = length, dimension[1] = breadth, dimension[2] = height
//for cube and cuboid
return areaCalculator(dimensions[0], dimensions[1]) * dimensions[2]
} else if(dimensions.size() == 2) {
//consider dimension[0] = radius, dimension[1] = height
//for cylinder and cone
return areaCalculator(dimensions[0]) * dimensions[1]
} else if(dimensions.size() == 1) {
//consider dimension[0] = radius
//for sphere
return areaCalculator(dimensions[0]) * dimensions[0]
}
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.closures
class Employee {
String fullName
}

View File

@ -0,0 +1,80 @@
package com.baeldung.closures
import spock.lang.Specification
class ClosuresUnitTest extends GroovyTestCase {
Closures closures = new Closures()
void testDeclaration() {
closures.print("Hello! Closure")
closures.formatToLowerCaseClosure("Hello! Closure")
closures.print.call("Hello! Closure")
closures.formatToLowerCaseClosure.call("Hello! Closure")
}
void testClosureVsMethods() {
assert closures.formatToLowerCase("TONY STARK") == closures.formatToLowerCaseClosure("Tony STark")
}
void testParameters() {
//implicit parameter
assert closures.greet("Alex") == "Hello! Alex"
//multiple parameters
assert closures.multiply(2, 4) == 8
assert closures.calculate(12, 4, "ADD") == 16
assert closures.calculate(12, 4, "SUB") == 8
assert closures.calculate(43, 8, "DIV") == 5.375
//varags
assert closures.addAll(12, 10, 14) == 36
}
void testClosureAsAnArgument() {
assert closures.volume({ l, b -> return l*b }, 12, 6, 10) == 720
assert closures.volume({ radius -> return Math.PI*radius*radius/3 }, 5, 10) == Math.PI * 250/3
}
void testGStringsLazyEvaluation() {
def name = "Samwell"
def welcomeMsg = "Welcome! $name"
assert welcomeMsg == "Welcome! Samwell"
// changing the name does not affect original interpolated value
name = "Tarly"
assert welcomeMsg != "Welcome! Tarly"
def fullName = "Tarly Samson"
def greetStr = "Hello! ${-> fullName}"
assert greetStr == "Hello! Tarly Samson"
// this time changing the variable affects the interpolated String's value
fullName = "Jon Smith"
assert greetStr == "Hello! Jon Smith"
}
void testClosureInLists() {
def list = [10, 11, 12, 13, 14, true, false, "BUNTHER"]
list.each {
println it
}
assert [13, 14] == list.findAll{ it instanceof Integer && it >= 13}
}
void testClosureInMaps() {
def map = [1:10, 2:30, 4:5]
assert [10, 60, 20] == map.collect{it.key * it.value}
}
}