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

name.remal.java.util.concurrent.atomic.AtomicInteger.kt Maven / Gradle / Ivy

package name.remal

import java.util.concurrent.atomic.AtomicInteger

operator fun AtomicInteger.inc() = apply { incrementAndGet() }
operator fun AtomicInteger.dec() = apply { decrementAndGet() }

operator fun AtomicInteger.plusAssign(value: AtomicInteger) = plusAssign(value.get())
operator fun AtomicInteger.plusAssign(value: Int) {
    addAndGet(value)
}

operator fun AtomicInteger.minusAssign(value: AtomicInteger) = minusAssign(value.get())
operator fun AtomicInteger.minusAssign(value: Int) {
    addAndGet(-1 * value)
}


fun AtomicInteger.incrementAndGetUnsigned(): Int {
    while (true) {
        val result = incrementAndGet()
        if (0 <= result) return result
        if (compareAndSet(result, 0)) {
            return 0
        }
    }
}

fun AtomicInteger.decrementAndGetUnsigned(): Int {
    while (true) {
        val result = decrementAndGet()
        if (0 <= result) return result
        if (compareAndSet(result, Int.MAX_VALUE)) {
            return Int.MAX_VALUE
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy