반응형
println
println "Hello World"
println 'Hello World'
println("Hello World")
Variable
def name = "john"
String name = "john"
name = "john"
def (a, b, c) = [30, 40, 50]
println "${a} ${b} ${c}" // 30, 40, 50
println "${a.getClass()} ${b.getClass()} ${c.getClass()}" // class java.lang.Integer class java.lang.Integer class java.lang.Integer
def (String a, int b, Double c) = [30, 40, 50]
println "${a} ${b} ${c}" // 30 40 50.0
println "${a.getClass()} ${b.getClass()} ${c.getClass()}" // class java.lang.String class java.lang.Integer class java.lang.Double
Operator
println 2 ** 3 // 8
println 9.intdiv(5) // 1
println 9 / 5 // 1.8
println Integer.toBinaryString(20) // 10100
println Integer.parseInt("20") // 20
println !"foo" // false
println !"" // true
def output = (1 > 0) ? "1 is greeter" : "1 is not greeter"
println output // 1 is greeter
Conditional Statements
def x = 0.0
switch (x) {
case 0:
println "x is zero"
break
case { x > 0 }:
println "x is +ve"
break
case {x < 0}:
println "x is -ve"
break
default:
println "Invalid number"
}
Loops
for (a in 1..5) {
print "${a} "
} // 1 2 3 4 5
for (i in [0, 1, 2]) {
print "${i} "
} // 0 1 2
def map = [aaa: 1, bbb: 2, ccc: 3]
for (e in map) {
println "${e.key} : ${e.value}"
}
1.upto(5) { print "${it} " } // 1 2 3 4 5
5.times { print "${it} " } // 0 1 2 3 4
1.step(10, 2) { print "${it} " } // 1 3 5 7 9
Exception Handling
try {
int i = 1 / 0
} catch (Exception e) {
println e.getCause()
println e.getMessage()
e.printStackTrace()
} finally {
println "finally"
}
String
String name1 = "john"
def name2 = "Tom"
def name = "john"
def message1 = "Hi ${name}"
def message2 = "Hi $name"
def message3 = "Hi ".concat(name)
def name = "john"
println name[1] // o
println name[-2] // h
println name[0..2] // joh
println name[3..1] // nho
println name[0,2,3] // jhn
def name = "john"
println name.substring(2)
println name.subSequence(0, 2)
def message = "This is a groovy class"
println message.split(" ") // ["This", "is", "a", "groovy", "class"]
println message-("groovy ") // "This is a class"
println message.toList()
println "groovy " * 3
println /Hi "john"/
def name = "john"
println /Hi "${john}"/
println $/Hi "${john}"/$
Methods
def add(int a=0, int b=0) {
return a + b;
}
println add()
Closures
def closure = { name -> println "Hi ${name}" }
closure.call("John")
closure("Tom")
def list = ["A", "B", "C"]
def map = [
"name": "john",
"age": 21
]
println list.each{ it }
println map.each{ it }
def list = [0, 1, 2]
println list.find { it == 1 }
println list.findAll { it >= 1 }
println list.any { it >= 1 } // true
println list.every { it >= 1 } // false
println list.collect { it * 2 }
Lists
def list = ["A", "B", "C", "D"]
println list[0..2]
println list[3..1]
def list = []
list.add(1)
list<<2
list = list + [3, 4]
list = list.plus([5, 6])
list = list.minus([5, 6])
list = list - [3, 4]
list = list.reverse()
list = list.sort()
println 1 in list // true
Maps
def employee = [
name: "John",
age: 20
]
def newEmployee = employee.clone()
employee.each { key, value ->
println "${key} : ${value}"
}
employee.eachWithIndex { key, value, index ->
println "${index} ${key} ${value}"
}
Ranges
def range = 1..10
println range // 1..10
println range.size() // 10
println range.getFrom() // 1
println range.getTo() // 10
println range.from // 1
println range.to // 10
println range[1] // 2
println range.isReverse() // false
println range intanceof java.util.List // true
def range = 1..10
def subRange = range.subList(3, 7)
println subRange.from // 4
println subRange.to // 7
(1..10).each { i ->
println i
}
Read File
File file = new File("test2.groovy")
println file.text
println file.collect { it }
println file as String[]
println file.readLines()
println file.bytes
new File("test2.groovy").eachLine { line, lineNo ->
println "${lineNo} | line : ${line}"
}
def line = ""
new File("test2.groovy").withReader { reader ->
while ((line = reader.readLine()) != null) {
println line
}
}
new File(".").eachFile { file ->
println file.getAbsolutePath()
}
// 하위 디렉토리까지 탐색
new File(".").eachFileRecurse { file ->
println file.getAbsolutePath()
}
new File("test3.groovy") << new File("test2.groovy").text
Write File
File file = new File("test.txt")
file.write("Hello World")
file.text = "Hello World 3"
file.withWriter { writer ->
writer.writeLine("Hello World 4")
}
file.bytes = []
File file = new File("test.txt")
file << "Hello World 1"
file.append("Hello World 2")
Regex
def message = "This is Groovy"
def regex = /(?:[^Groovy]*)/
def match = message =~ regex
println match[0]
Class
class Car {
int color = 0
def getColor() {
return color
}
abstract def getName()
}
class Bmw extends Car {
@Override
public Object getName() {
return "BMW"
}
}
interface Car {
def startEngine()
def stopEngine()
}
class Bmw implements Car {
@Override
public Object startEngine() {
return null
}
@Override
public Object stopEngine() {
return null
}
}
class Foo {
Foo(Integer x) {
println 'my constructor was called'
}
Foo() {}
def a
def b
}
new Foo(1)
foo = new Foo(a: '1', b: '2')
assert foo.a == "1"
Json
def json = JsonOutput.toJson([
student: [
name: "john",
age: 25
]
])
println json
println JsonOutput.prettyPrint(json)
참고
반응형
'Development > Groovy' 카테고리의 다른 글
[Groovy] 자바에서 groovy script 실행 (0) | 2020.12.28 |
---|---|
[Groovy] DSL 예시 (0) | 2020.12.28 |
[Groovy] DSL 공식문서 코드 (0) | 2020.12.28 |
[Groovy] 설치 (0) | 2020.12.28 |