
run.smt.ktest.util.delegates.threadLocal.kt Maven / Gradle / Ivy
package run.smt.ktest.util.delegates
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun threadLocal(initialValue: () -> T): ReadWriteProperty = ThreadLocalProperty(initialValue)
fun threadLocal(initialValue: T): ReadWriteProperty = ThreadLocalProperty { initialValue }
fun inheritableThreadLocal(initialValue: () -> T): ReadWriteProperty = InheritableThreadLocalProperty(initialValue)
fun inheritableThreadLocal(initialValue: T): ReadWriteProperty = InheritableThreadLocalProperty { initialValue }
private class ThreadLocalProperty(initialValue: () -> T) : ReadWriteProperty {
private val prop = object : ThreadLocal() {
override fun initialValue(): T {
return initialValue()
}
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T = prop.get()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = prop.set(value)
}
private class InheritableThreadLocalProperty(initialValue: () -> T) : ReadWriteProperty {
private val prop = object : InheritableThreadLocal() {
override fun initialValue(): T {
return initialValue()
}
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T = prop.get()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = prop.set(value)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy