Advertisement
zinch

GPath example from 'Groovy in Action'.

Oct 6th, 2014
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.95 KB | None | 0 0
  1. class Invoice {
  2.     List items
  3.     Date date
  4. }
  5.  
  6. class LineItem {
  7.     Product    product
  8.     int        count
  9.     int total() {
  10.         return product.dollar * count
  11.     }
  12. }
  13.  
  14. class Product {
  15.     String name
  16.     def dollar
  17. }
  18.  
  19. def ulcDate = new Date(107, 0, 1)
  20. def ulc = new Product(dollar:1499, name:'ULC')
  21. def ve = new Product(dollar:499, name:'Visual Editor')
  22.  
  23. def invoices = [
  24.     new Invoice(date:ulcDate, items: [
  25.         new LineItem(count:5, product:ulc),
  26.         new LineItem(count:1,  product:ve)
  27.     ]),
  28.     new Invoice(date:[107,1,2], items: [
  29.         new LineItem(count:4, product:ve)
  30.     ])
  31. ]
  32.  
  33. assert [5*1499, 499, 4*499] == invoices.items.flatten()*.total() //1
  34.  
  35. assert ['ULC'] == invoices.items.flatten().grep{it.total() > 7000}.product.name //2
  36.  
  37. def searchDates = invoices.grep{
  38.     it.items.any{ it.product == ulc }        
  39. }.date*.toString()                        //3
  40.  
  41. assert [ulcDate.toString()] == searchDates
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement