commonMain.com.arkivanov.mvikotlin.utils.internal.AtomicExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-internal-js Show documentation
Show all versions of utils-internal-js Show documentation
Kotlin Multiplatform MVI framework
package com.arkivanov.mvikotlin.utils.internal
import kotlin.reflect.KProperty
fun AtomicRef.initialize(value: T) {
if (!compareAndSet(null, value)) {
throw IllegalStateException("Value is already initialized: $this")
}
}
fun AtomicRef.requireValue(): T = requireNotNull(value) { "Value was not initialized" }
operator fun AtomicBoolean.getValue(thisRef: Any?, property: KProperty<*>): Boolean = value
operator fun AtomicBoolean.setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) {
this.value = value
}
operator fun AtomicInt.getValue(thisRef: Any?, property: KProperty<*>): Int = value
operator fun AtomicInt.setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
this.value = value
}
operator fun AtomicRef.getValue(thisRef: Any?, property: KProperty<*>): T = value
operator fun AtomicRef.setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this.value = value
}
fun AtomicRef.getAndUpdate(block: (T) -> T): T {
var oldValue: T
do {
oldValue = value
} while (!compareAndSet(oldValue, block(oldValue)))
return oldValue
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy