Provide getOrSet method for ThreadLocal<T: Any>

#KT-7927 Fixed
This commit is contained in:
Ilya Gorbunov
2015-06-05 17:53:39 +03:00
parent cb9d9cb77d
commit a47325cc30
2 changed files with 36 additions and 0 deletions
@@ -80,6 +80,22 @@ public fun thread(start: Boolean = true, daemon: Boolean = false, contextClassLo
return thread
}
/**
* Gets the value in the current thread's copy of this
* thread-local variable or replaces the value with the result of calling
* [default] function in case if that value was `null`.
*
* If the variable has no value for the current thread,
* it is first initialized to the value returned
* by an invocation of the [ThreadLocal.initialValue] method.
* Then if it is still `null`, the provided [default] function is called and its result
* is stored for the current thread and then returned.
*/
public inline fun <T: Any> ThreadLocal<T>.getOrSet(default: () -> T): T {
// TODO: replace let with apply or touch
return get() ?: default().let { set(it); it }
}
/**
* Allows you to use the executor as a function to
* execute the given block on the [Executor].
@@ -28,4 +28,24 @@ class ThreadTest {
assertEquals("Hello", future.get(2, SECONDS))
}
test fun threadLocalGetOrSet() {
val v = ThreadLocal<String>()
assertEquals("v1", v.getOrSet { "v1" })
assertEquals("v1", v.get())
assertEquals("v1", v.getOrSet { "v2" })
v.set(null)
assertEquals("v2", v.getOrSet { "v2" })
v.set("v3")
assertEquals("v3", v.getOrSet { "v2" })
val w = object: ThreadLocal<String>() {
override fun initialValue() = "default"
}
assertEquals("default", w.getOrSet { "v1" })
}
}