Advertisement
qeqwew

Untitled

Dec 11th, 2019
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import tanks2019.IoCException
  2. import tanks2019.IoCStrategy
  3. import tanks2019.Scope.Scope
  4. import tanks2019.Scope.ScopeDefault
  5. import tanks2019.Scope.ScopeStub
  6. import tanks2019.ioc.RegisterCommand
  7. import kotlin.concurrent.getOrSet
  8.  
  9. internal class ScopesManager {
  10. companion object {
  11. private val threadStore = ThreadLocal<Scope<IoCStrategy>>()
  12. fun current(): Scope<IoCStrategy> {
  13. return threadStore.getOrSet { default() }
  14. }
  15.  
  16. private fun setCurrent(scope: Scope<IoCStrategy>) {
  17. threadStore.set(scope)
  18. }
  19.  
  20. private val root: Scope<IoCStrategy> = ScopeDefault(ScopeStub())
  21. private var default: () -> Scope<IoCStrategy> = { root }
  22.  
  23. init {
  24. setCurrent(root)
  25.  
  26. root.register("IoC.Register", { k: Array<out Any> ->
  27. try {
  28. @Suppress("UNCHECKED_CAST")
  29. return@register RegisterCommand(
  30. k[0] as String, k[1] as IoCStrategy)
  31. } catch (e: ClassCastException) {
  32. throw IoCException(
  33. "Wrong type of argument(s) for IoC.Register " +
  34. "dependency . Argument 2 should be String . Argument 3 should be IoCStrategy .", e)
  35. } catch (e: IndexOutOfBoundsException) {
  36. throw IoCException(
  37. String.format(
  38. "Wrong number of arguments for IoC." +
  39. "Register dependency . It should be three, instead of { 0 }", k.size
  40. ), e
  41. )
  42. }
  43. })
  44. }
  45.  
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement