5.1 GString
groovy.lang.GString是对java String的扩展:常量表示更加方便,提供了更多的方法。
'hello world' "hello $name" '''------- python style string -------''' """ triple double quote with $Gstring aware """ /back slash safe \t, \widows\path/ char c='a'; // declare character explicitly date = "Current London time is ${new Date().toGMTString()}" province = 'Sichuan' city = 'Chengdu' addr = "I am in city: $city province: $province, you?" println "${addr.strings[0]}, ${addr.strings[1]}, ${addr.values[0]}, ${addr.values[1]}" // output: I am in city: , province: , Chengdu, Sichuan println addr[2..3] // output: am sb = 'i am a string' sb <<= ' changed to a stringbuffer' sb << ' now can append' // sb is: i am a string changed to a stringbuffer now can append
5.2 数字对象
groovy为数字对象提供了更多的方法
r = '' 10.times { r += '*' // output: ********** } s = 0 1.upto(10) { i -> // 10 is inclusive s += i } // s= 55 2.downto(-2) { i -> print "$i," // output: 2,1,0,-1,-2, } 0.step(10, 2) { i -> print "$i," // output: 0,2,4,6,8, }
5.3 range
(0..3).contains(2) // result: true (1..<5).size() // result: 4, 5 is excluded for (e in 5..9) { // loop by range print "$e," // output: 5,6,7,8,9, } (5..1).each { i -> // reverse range print "$i," // output: 5,4,3,2,1, } (5..<1).each { i -> print "$i," // output: 5,4,3,2, } (1..5).isCase(3) // true age = 23 switch(age) { case 0..8 : println 'kid'; break case 9..17 : println 'yang'; break case 18..50 : println 'grown up'; break default : println 'old'; break } // result: grown up all = [1,2,3,4,5,6,7,8,9] greater = 5..10 lesser = all.grep(greater) // result: [5, 6, 7, 8, 9]
groovy的类只要:
a. 实现了next和previous,也即重载了:++和--操作符,;
b. 实现了java.lang.Comparable接口,也即重载了<=>操作符;
就可以使用range
class RomaNumber implements Comparable { private static final Numbers = ['z', 'I','II','III','IV','V']; private int digit; RomaNumber(int digit) { this.digit = digit; } RomaNumber next() { return new RomaNumber((digit + 1) % Numbers.size()); } RomaNumber previous() { return new RomaNumber(digit - 1); } int compareTo(Object o) { return this.digit <=> o.digit; // call CompareTo() } String toString() { return Numbers[digit]; } } def one = new RomaNumber(1); def four = new RomaNumber(4); for (num in one..four) { print "$num,"; // output: I,II,III,IV, }
5.4 list
groovy借鉴了很多脚本语言,对list做了很多扩展,使之更方便使用。
list的index可为负数,-1是最后一个元素的索引,-2是倒数第二个,以此类推。并且访问超出范围的索引不会抛异常,而是返回null。
list还重载了操作符。提供了很多支持闭包的方法。
/***** index *****/ lst = ['a','b','c','d',1,2,3,4] // elements with different type println lst[0..2] // get elements in range, output: [a, b, c] println lst[3,5] // get elements of indexes, output: [d, 2] lst[2..3]=['x','y','z'] // put elements in range, replace and add, result: [a, b, x, y, z, 1, 2, 3, 4] lst[-1..<-3]=[] // clear elements in range, result: [a, b, x, y, z, 1, 2] lst[0,1]=[-1,-3] // put elements in indexes, result: [-1, -3, x, y, z, 1, 2] println lst[10] // get elements out of bounds, no exception, result: null /***** operator overload *****/ lst += 'I' // plus(object), result: [-1, -3, x, y, z, 1, 2, I] lst += ['II','IV'] // // plus(collection), result: [-1, -3, x, y, z, 1, 2, I, II, IV] lst -= [-1,-3,'x','y','z',1,2] // minus(collection), result: [I, II, IV] lst * 2 // multiply(integert), result: [I, II, IV, I, II, IV] /***** convient op *****/ println lst.grep(['V', 'II', 'VI']) // intersection, output: [II, II] switch('I') { case lst : println 'true'; break // contains? output: true default : println 'false'; break } lst.pop() // op like stack, result: [I, II, IV, I, II] lst=[] if (!lst) { // empty list is false println 'lst is empty' // output: lst is empty } lst = [[1,3],[-1,2]] lst.sort { a,b -> a[0] <=> b[0] } // compare by first element: output: [[-1, 2], [1, 3]] lst= [1,2,5] lst.remove 2 // remove by index, not value, result: [1, 2] // method accept closure as paramters println lst.collect { item -> item * 2 } // transfer collection to another: output:[2, 4] println lst.findAll { item -> item % 2 == 0 } // find by closure, output: [2]