package io.vertx.core.shareddata.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.shareddata.Counter;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
public class AsynchronousCounter implements Counter {
private final VertxInternal vertx;
private final AtomicLong counter;
public AsynchronousCounter(VertxInternal vertx) {
this.vertx = vertx;
this.counter = new AtomicLong();
}
public AsynchronousCounter(VertxInternal vertx, AtomicLong counter) {
this.vertx = vertx;
this.counter = counter;
}
@Override
public void get(Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.get())));
}
@Override
public void incrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.incrementAndGet())));
}
@Override
public void getAndIncrement(Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.getAndIncrement())));
}
@Override
public void decrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.decrementAndGet())));
}
@Override
public void addAndGet(long value, Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.addAndGet(value))));
}
@Override
public void getAndAdd(long value, Handler<AsyncResult<Long>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.getAndAdd(value))));
}
@Override
public void compareAndSet(long expected, long value, Handler<AsyncResult<Boolean>> resultHandler) {
Objects.requireNonNull(resultHandler, "resultHandler");
Context context = vertx.getOrCreateContext();
context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.compareAndSet(expected, value))));
}
}