This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import groovy.io.FileType | |
| class FileInfo { | |
| def path | |
| def parentFolder | |
| def name | |
| def sizeBytes | |
| def sizeKb | |
| FileInfo(File file) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main.groovy.fp | |
| def isHeader = true | |
| def columns = [] | |
| def records1stFile = [] | |
| println "1st file ACCOUNT-after.csv" | |
| new File("ACCOUNT-after.csv").eachLine { line -> | |
| if (isHeader) { | |
| columns = line.split(",") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main.groovy | |
| Calendar calendar = Calendar.instance | |
| calendar.set(2011, 2, 26) | |
| println calendar.time | |
| calendar.set(2012, 0, 1, 13,13,59) | |
| println calendar.time |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def myCodeBlock1 = {println it; it} //closure with 1 parameter | |
| myCodeBlock1("test") | |
| assert myCodeBlock1("test") == "test" | |
| def myCodeBlock2 = {a,b -> a+b} | |
| assert myCodeBlock2(1,2) == 3 | |
| def list = ['a','b','c','d'] | |
| def newList = [] | |
| def clos = { it.toUpperCase() } //accept 1 argument |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def myRange = 1..3 | |
| assert myRange.size() == 3 | |
| (1..3).each{it -> println it} //iterate range | |
| myRange.each{println it} | |
| assert myRange.from == 1 | |
| assert myRange.to == 3 | |
| assert myRange.contains(2) | |
| assert myRange.reverse() == [3,2,1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def emptyMap = [:] | |
| assert emptyMap.size() == 0 | |
| def notEmptyMap = ["person1":"john", "person2":"mus"] | |
| assert notEmptyMap.size() == 2 | |
| notEmptyMap.put "person3","test" //adding to existing one | |
| assert notEmptyMap.size() == 3 | |
| notEmptyMap["person4"] = "beth" | |
| assert notEmptyMap.size() == 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def teamMembers1 = ["p1", "p2", "p3"] //by default arrayList | |
| teamMembers1 << "p4" | |
| assert teamMembers1.size() == 4 | |
| teamMembers2 = ["b1", "b2"] as Set //setting the type | |
| teamMembers2 << "b1" | |
| assert teamMembers2.size() == 2 | |
| emptyList = [] //empty list | |
| assert emptyList.size() == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| def name | |
| def spouse | |
| def age | |
| } | |
| candidate1 = new Person(name:"bill", spouse: new Person(name:"lengloy")) | |
| candidate1.spouse.spouse = candidate1 | |
| assert candidate1.spouse.spouse != null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| def name //duck typing | |
| def age | |
| def getName() { //override default getter | |
| "will not be printed" | |
| "my name is ${name}" //return is optional, last statement is taken | |
| } | |
| } | |
| person = new Person(name:"goku") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def list = [] | |
| assert list.isEmpty() | |
| list.add("ayam") | |
| list.add "babun" //optional parentheses, except if it doesnt have any argument | |
| list << "cicak" //operator overloading | |
| assert list.size == 3 | |
| println list | |
| def tambah = list.&add //method pointer in action | |
| tambah "dugong" |