Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tanks2019.IoCException
- import tanks2019.IoCStrategy
- import tanks2019.Scope.Scope
- import tanks2019.Scope.ScopeDefault
- import tanks2019.Scope.ScopeStub
- import tanks2019.ioc.RegisterCommand
- import kotlin.concurrent.getOrSet
- internal class ScopesManager {
- companion object {
- private val threadStore = ThreadLocal<Scope<IoCStrategy>>()
- fun current(): Scope<IoCStrategy> {
- return threadStore.getOrSet { default() }
- }
- private fun setCurrent(scope: Scope<IoCStrategy>) {
- threadStore.set(scope)
- }
- private val root: Scope<IoCStrategy> = ScopeDefault(ScopeStub())
- private var default: () -> Scope<IoCStrategy> = { root }
- init {
- setCurrent(root)
- root.register("IoC.Register", { k: Array<out Any> ->
- try {
- @Suppress("UNCHECKED_CAST")
- return@register RegisterCommand(
- k[0] as String, k[1] as IoCStrategy)
- } catch (e: ClassCastException) {
- throw IoCException(
- "Wrong type of argument(s) for IoC.Register " +
- "dependency . Argument 2 should be String . Argument 3 should be IoCStrategy .", e)
- } catch (e: IndexOutOfBoundsException) {
- throw IoCException(
- String.format(
- "Wrong number of arguments for IoC." +
- "Register dependency . It should be three, instead of { 0 }", k.size
- ), e
- )
- }
- })
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement