AtomicBigInteger.java
package org.microspace.table;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* An atomic counter.
* @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
* @version 2016-06-26
*/
public class AtomicBigInteger {
private final AtomicReference<BigInteger> valueHolder = new AtomicReference<>();
public AtomicBigInteger(BigInteger bigInteger) {
valueHolder.set(bigInteger);
}
public BigInteger incrementAndGet() {
for (; ; ) {
BigInteger current = valueHolder.get();
BigInteger next = current.add(BigInteger.ONE);
if (valueHolder.compareAndSet(current, next)) {
return next;
}
}
}
public BigInteger get() {
return valueHolder.get();
}
public void set(BigInteger value) {
valueHolder.set(value);
}
}