Skip to content

Instantly share code, notes, and snippets.

@alexfacciorusso
Created July 1, 2021 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexfacciorusso/f9f7ab1c9aec0815ee161443bfb5f178 to your computer and use it in GitHub Desktop.
Save alexfacciorusso/f9f7ab1c9aec0815ee161443bfb5f178 to your computer and use it in GitHub Desktop.
Kotlin delegate for Atomic reference
/**
* 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