All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.arkivanov.mvikotlin.utils.internal.AtomicExt.kt Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
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