Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getAndUpdate() method to AtomicReference #91

Open
volo-droid opened this issue Feb 21, 2023 · 0 comments
Open

Add getAndUpdate() method to AtomicReference #91

volo-droid opened this issue Feb 21, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@volo-droid
Copy link

volo-droid commented Feb 21, 2023

On JVM the AtomicReference class contains the getAndUpdate() method. It'd be convenient to have it for other targets/common code as well. The non-JVM implementation can look like:

fun getAndUpdate(updateFunction: (V) -> V): V {
    while(true) {
        val prev = get()
        val next = updateFunction(prev)
        if (compareAndSet(prev, next)) return prev
    }
}

The signature of the corresponding JVM method is:

public final V getAndUpdate(UnaryOperator<V> updateFunction)

where UnaryOperator is a functional interface:

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T>

That should place nicely with the SAM conversion

@findjigar findjigar added the enhancement New feature or request label Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants