Created
April 7, 2012 16:04
-
-
Save takezoe/2329981 to your computer and use it in GitHub Desktop.
Simple wrapper of PicoContainer for Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.picocontainer.DefaultPicoContainer | |
/** | |
* Simple wrapper of PicoContainer for Scala. | |
* | |
* {{{ | |
* val pico = new Pico() | |
* .add[Apple] | |
* .add[Juicer] | |
* .add[Peeler] | |
* | |
* val peeler = pico.get[Peeler] | |
* }}} | |
*/ | |
class Pico { | |
val pico = new DefaultPicoContainer(); | |
def add[T](implicit m: scala.reflect.Manifest[T]): Pico = { | |
val clazz = m.erasure.asInstanceOf[Class[Any]] | |
pico.addComponent(clazz) | |
this | |
} | |
def add(c: Any): Pico = { | |
pico.addComponent(c) | |
this | |
} | |
def get[T](implicit m: scala.reflect.Manifest[T]): T = { | |
val clazz = m.erasure.asInstanceOf[Class[Any]] | |
pico.getComponent(clazz).asInstanceOf[T] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment