Groovy magic: asType

Every one who used Grails had to deal (or will have) with XML/JSON converters. They allow to transform object by using syntax like:

  1.  
  2. import grails.converters.*
  3.  
  4. class TestController {
  5. def index = {
  6. [a:1, b:2, c:3] as JSON
  7. }
  8. }
  9.  
  10.  

I like this "as" syntax, is very verbose and clear. How we can implement in our code? Simple, just use asType method:

  1.  
  2. class Foo {
  3. def name
  4.  
  5. String toString(){
  6. "Foo name is ${name}"
  7. }
  8. }
  9.  
  10. class Bar {
  11. def name
  12.  
  13. String toString(){
  14. "Bar name is ${name}"
  15. }
  16. }
  17.  
  18. class Bas {
  19.  
  20. def name
  21.  
  22. String toString(){
  23. "Bas name is ${name}"
  24. }
  25.  
  26. Object asType(Class type){
  27. typeWorker[type](this)
  28. }
  29.  
  30. @Lazy
  31. private def typeWorker = [(Foo):asFoo, (Bar):asBar]
  32.  
  33. private def asFoo = {it ->
  34. new Foo(name:it.name)
  35. }
  36.  
  37. private def asBar = {it ->
  38. new Bar(name:it.name)
  39. }
  40. }
  41.  
  42. def bas = new Bas(name:"Gringo")
  43.  
  44. println bas as Foo
  45. println bas as Bar
  46.  
  47.  

and output will be :

Bas name is Gringo
Foo name is Gringo
Bar name is Gringo

Ubuntu 10.04 on Dell Inspiron 1520 how to

Yesterday arrived my new disk, and It fit in time with release of new Ubuntu distribution so natural was to try it on new hardware. Unfortunately there is bug that take couple of hours from life before I menage to solve it. So. if your new Ubuntu freezing during installation try this:

  • boot from Ubuntu DVD
  • press anything on purple screen (in my case there was only funny icon displayed on bottom of screen) to get menu
  • chose language
  • press f6 and select nomodest option
  • edit command that appears and remove quiet and splash parameters
  • add nouveau.modset=0 parameter
  • start installation (it should be ok now)
  • after installation go to System->Administration->Hardware Drivers and install Nvidia drivers
  • tadam!

If problems appears during booting of installed Ubuntu (blank screen, freeze):

  • press e on getting the GRUB bootloader
  • remove quiet and splash parameters and nouveau.modset=0

Everything run smooth after installation of Nvidia drivers. All informations were found on Ubuntu official forum.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.