Skip to content

Instantly share code, notes, and snippets.

import groovy.io.FileType
class FileInfo {
def path
def parentFolder
def name
def sizeBytes
def sizeKb
FileInfo(File file) {
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(",")
@peysal
peysal / Date.groovy
Created December 23, 2013 13:54
Setting date and time
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
@peysal
peysal / introduction8.groovy
Created August 25, 2013 23:23
groovy introduction closure 1) default it parameter 2) parameter more than 1 3) passing closure to method
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
@peysal
peysal / introduction7.groovy
Created August 25, 2013 23:01
introduction to groovy 1) range declaration 2) iterate range 3) contains and reverse 4) size, from, to
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]
@peysal
peysal / introduction6.groovy
Created August 25, 2013 10:28
introduction to groovy 1) empty map 2) adding things to map 3) concat map to map 4) iterating
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
@peysal
peysal / introduction5.groovy
Created August 23, 2013 09:45
List in groovy
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
@peysal
peysal / introduction4.groovy
Last active December 21, 2015 13:49
Introduction to groovy. Safe deferencing and auto boxing
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
@peysal
peysal / introduction3.groovy
Created August 23, 2013 04:13
Introduction to groovy 1) duck typing 2) return is optional, last statement will be taken
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")
@peysal
peysal / introduction2.groovy
Last active December 21, 2015 13:49
Groovy introduction: 1) list 2) operator overloading 3) method pointer
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"