Posted By

berndschiffer on 08/08/09


Tagged

textmate groovy closureexample


Versions (?)

Bernds Lösung


 / Published in: Groovy
 

  1. class TimeLogger {
  2.  
  3. def oldDelegate
  4.  
  5. static timeLogged(closure) {
  6. new TimeLogger().bundleCalls(closure)
  7. }
  8.  
  9. private bundleCalls = { closure ->
  10. closure.resolveStrategy = Closure.DELEGATE_ONLY
  11. oldDelegate = closure.delegate
  12. closure.delegate = this
  13. closure()
  14. }
  15.  
  16. def invokeMethod(String name, args) {
  17. System.currentTimeMillis()
  18. def result =
  19. oldDelegate.invokeMethod(name, args)
  20. def duration =
  21. (System.currentTimeMillis() + start) / 1000
  22. println "duration in sec: $duration"
  23. return result
  24. }
  25. }
  26.  
  27. import static TimeLogger.timeLogged
  28.  
  29. def tueA() { tue('a') }
  30. def tueB() { tue('b') }
  31. def tueC() { tue('c') }
  32.  
  33. def tue(was) {
  34. println was
  35. sleep(100)
  36. }
  37.  
  38. timeLogged {
  39. tueA()
  40. tueB()
  41. tueC()
  42. }

Report this snippet  

You need to login to post a comment.