diff --git a/ts-core/src/main/kotlin/cn/tursom/core/context/ArrayContextEnv.kt b/ts-core/src/main/kotlin/cn/tursom/core/context/ArrayContextEnv.kt index 73c0802..b800e8b 100644 --- a/ts-core/src/main/kotlin/cn/tursom/core/context/ArrayContextEnv.kt +++ b/ts-core/src/main/kotlin/cn/tursom/core/context/ArrayContextEnv.kt @@ -6,17 +6,17 @@ import java.util.concurrent.atomic.AtomicInteger class ArrayContextEnv : ContextEnv { val envId = ContextEnv.newEnvId() private val idGenerator = AtomicInteger() + private val emptyContext = EmptyArrayContext(envId, idGenerator) - override fun emptyContext(): Context = ArrayContext(envId, idGenerator, empty = true) + override fun emptyContext(): Context = emptyContext override fun newContext(): Context = ArrayContext(envId, idGenerator) override fun <T> newKey() = ContextKey<T>(envId, idGenerator.getAndIncrement()) private class ArrayContext( override val envId: Int, private val idGenerator: AtomicInteger, - empty: Boolean = false, ) : Context { - private var array = if (empty) emptyArray() else arrayOfNulls<Any?>(idGenerator.get()) + private var array = arrayOfNulls<Any?>(idGenerator.get()) override operator fun <T> get(key: ContextKey<T>): T? { checkEnv(key) @@ -27,12 +27,23 @@ class ArrayContextEnv : ContextEnv { } } - override operator fun <T> set(key: ContextKey<T>, value: T) { + override fun <T> set(key: ContextKey<T>, value: T): ArrayContext { checkEnv(key) if (array.size < key.id) { array = array.copyOf(idGenerator.get()) } array[key.id] = value + return this } } + + private class EmptyArrayContext( + override val envId: Int, + private val idGenerator: AtomicInteger, + ) : Context { + override fun <T> get(key: ContextKey<T>): T? = null + override fun <T> get(key: DefaultContextKey<T>): T = key.provider() + override fun <T> set(key: ContextKey<T>, value: T) = + ArrayContext(envId, idGenerator).set(key, value) + } } diff --git a/ts-core/src/main/kotlin/cn/tursom/core/context/Context.kt b/ts-core/src/main/kotlin/cn/tursom/core/context/Context.kt index 0475d5e..10a549e 100644 --- a/ts-core/src/main/kotlin/cn/tursom/core/context/Context.kt +++ b/ts-core/src/main/kotlin/cn/tursom/core/context/Context.kt @@ -15,7 +15,7 @@ interface Context { } operator fun <T> get(key: ContextKey<T>): T? - operator fun <T> set(key: ContextKey<T>, value: T) + fun <T> set(key: ContextKey<T>, value: T): Context fun checkEnv(key: ContextKey<*>) { if (envId != key.envId) { diff --git a/ts-core/src/main/kotlin/cn/tursom/core/context/HashMapContextEnv.kt b/ts-core/src/main/kotlin/cn/tursom/core/context/HashMapContextEnv.kt index 23b2b02..60c3cb7 100644 --- a/ts-core/src/main/kotlin/cn/tursom/core/context/HashMapContextEnv.kt +++ b/ts-core/src/main/kotlin/cn/tursom/core/context/HashMapContextEnv.kt @@ -15,14 +15,15 @@ class HashMapContextEnv : ContextEnv { ) : Context { private var map = HashMap<Int, Any?>() - override operator fun <T> get(key: ContextKey<T>): T? { + override fun <T> get(key: ContextKey<T>): T? { checkEnv(key) return map[key.id]?.uncheckedCast<T>() } - override operator fun <T> set(key: ContextKey<T>, value: T) { + override fun <T> set(key: ContextKey<T>, value: T): HashMapContext { checkEnv(key) map[key.id] = value + return this } } }