wtorek, 29 lipca 2008

Groovy w praktyce - part 3 - listy i kilka operatorów

Najpierw parę operatorów.
  • *. - pozwala na zgrupowanie wyników pewnej operacji a*.b() odpowiada a.collect{ it.b()}, domyślny sposób nawigacji w OCL i XPath
  • ?. - bezpieczna nawigacja - jeśli a= null to a?.b też wynosi null
  • ?: - doskonały dla wartości domyślny - zwraca wartość po prawej jeśli po lewej jest null, pusty string, false lub 0
//
// Operator .*
//
groovy> "abc".toList()
===> [a, b, c]
groovy> a = "abc".toList()
===> [a, b, c]
groovy> a*.toUpperCase()
===> [A, B, C]
groovy> a.collect{ it.toUpperCase()}
===> [A, B, C]

//
// Operator ?.
//
groovy> class A{ def a }
===> true

groovy> a = new A()
===> A@c1a0eb
groovy> a.a = new A()
===> A@ed9f47
groovy> a.a.a = new A()
===> A@1762027

groovy> a.a?.a
===> A@1762027

groovy> a.a = null
===> null
groovy> a.a?.a
===> null
groovy> a.a.a
ERROR java.lang.NullPointerException: null
at groovysh_evaluate.run (groovysh_evaluate:1)
...

//
// Operator ?:
//
groovy> null?:2
===> 2

groovy> 1?:2
===> 1
groovy> 0?:2
===> 2

groovy> true?:2
===> true
groovy> false?:2
===> 2

groovy> ''?:2
===> 2
groovy> 'x'?:2
===> x


A teraz listy, niech kod mówi sam za siebie.


Groovy Shell (, JVM: 10.0-b22)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------

groovy> a = [1,2,3]
===> [1, 2, 3]
groovy> a.class
===> class java.util.ArrayList

groovy> a + 4
===> [1, 2, 3, 4]
groovy> a
===> [1, 2, 3]

groovy> a << 4
===> [1, 2, 3, 4]
groovy> a
===> [1, 2, 3, 4]

groovy> a - 3
===> [1, 2, 4]
groovy> a
===> [1, 2, 3, 4]

groovy> a -= 3
===> [1, 2, 4]
groovy> a += 3
===> [1, 2, 4, 3]
groovy> a
===> [1, 2, 4, 3]

groovy> a.sort()
===> [1, 2, 3, 4]

groovy> a + [5,6,7]
===> [1, 2, 3, 4, 5, 6, 7]
groovy> a
===> [1, 2, 3, 4]


groovy> a << [5,6,7]
===> [1, 2, 3, 4, [5, 6, 7]]


groovy> a.pop()
===> [5, 6, 7]

groovy> a
===> [1, 2, 3, 4]

groovy> a += [5,6,7]
===> [1, 2, 3, 4, 5, 6, 7]

groovy> a += (8..10)
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

groovy> a[0]
===> 1
groovy> a.get(0)
===> 1
groovy> a.getAt(0)
===> 1
groovy> a.getAt([0,-3..-1])
===> [1, 8, 9, 10]
groovy> a[-1]
===> 10
groovy> a[4..-1]
===> [5, 6, 7, 8, 9, 10]
groovy> a == [*1..10]
===> true

groovy> a.each{println it}
1
2
3
4
5
6
7
8
9
10
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

groovy>a.reverseEach{ println it}
10
9
8
7
6
5
4
3
2
1
===> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
groovy> a.reverse().eachWithIndex{it,i -> println "$i: $it"}
0: 10
1: 9
2: 8
3: 7
4: 6
5: 5
6: 4
7: 3
8: 2
9: 1
===> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
groovy> a
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

groovy> a.find{it > 3}
===> 4
groovy> a.findAll{it > 3}
===> [4, 5, 6, 7, 8, 9, 10]

groovy> a.every{it > 3}
===> false
groovy> a.any{it > 3}
===> true

groovy> a.max()
===> 10
groovy> a.min()
===> 1
groovy> a.sum()
===> 55

groovy> a.join()
===> 12345678910
groovy> a.join('-')
===> 1-2-3-4-5-6-7-8-9-10

groovy> a.intersect([8,9,10,11,12,13])
===> [8, 9, 10]
groovy> a.contains(1)
===> true
groovy> a.containsAll([1,3,5])
===> true
groovy> a.disjoint([10,11])
===> false
groovy> a.disjoint([11,12])
===> true

groovy> a.multiply(2)
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
groovy> a
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
groovy> a * 2
===> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

groovy> a = "abc".toList()
===> [a, b, c]


Więcej: Groovy - Kolekcje

Brak komentarzy: