Created
July 1, 2021 09:10
-
-
Save alexfacciorusso/f9f7ab1c9aec0815ee161443bfb5f178 to your computer and use it in GitHub Desktop.
Kotlin delegate for Atomic reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Delegates a field to an AtomicReference. | |
*/ | |
fun <T> atomicReference(): ReadWriteProperty<Any?, T?> = object : ReadWriteProperty<Any?, T?> { | |
private val atomic = AtomicReference<T>() | |
override operator fun getValue(thisRef: Any?, property: KProperty<*>): T? = atomic.get() | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) { | |
atomic.set(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment