Compare Exchange: How to Get Compare Exchange Value



Syntax

Method:

GetCompareExchangeValueOperation(Class<T> clazz, String key);

Returned object:

public class CompareExchangeValue<T> {
    private String key;
    private long index;
    private T value;

    public CompareExchangeValue(String key, long index, T value) {
        this.key = key;
        this.index = index;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public long getIndex() {
        return index;
    }

    public void setIndex(long index) {
        this.index = index;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }
}
Key String The unique object identifier
Value T The existing value that Key has
Index long The version number of the Value that is stored for the specified Key

Example I - Value is 'long'

CompareExchangeValue<Long> readResult
    = store.operations().send(new GetCompareExchangeValueOperation<>(Long.class, "nextClientId"));

Long value = readResult.getValue();

Example II - Value is a custom object

CompareExchangeValue<User> readResult
    = store.operations().send(
        new GetCompareExchangeValueOperation<>(User.class, "AdminUser"));

User admin = readResult.getValue();