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

com.arangodb.shaded.vertx.core.shareddata.impl.AsynchronousCounter Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.shareddata.impl;

import com.arangodb.shaded.vertx.core.Future;
import com.arangodb.shaded.vertx.core.Promise;
import com.arangodb.shaded.vertx.core.impl.ContextInternal;
import com.arangodb.shaded.vertx.core.impl.VertxInternal;
import com.arangodb.shaded.vertx.core.shareddata.Counter;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Tim Fox
 */
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 Future get() {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.get());
    return promise.future();
  }

  @Override
  public Future incrementAndGet() {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.incrementAndGet());
    return promise.future();
  }

  @Override
  public Future getAndIncrement() {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.getAndIncrement());
    return promise.future();
  }

  @Override
  public Future decrementAndGet() {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.decrementAndGet());
    return promise.future();
  }

  @Override
  public Future addAndGet(long value) {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.addAndGet(value));
    return promise.future();
  }

  @Override
  public Future getAndAdd(long value) {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.getAndAdd(value));
    return promise.future();
  }

  @Override
  public Future compareAndSet(long expected, long value) {
    ContextInternal context = vertx.getOrCreateContext();
    Promise promise = context.promise();
    promise.complete(counter.compareAndSet(expected, value));
    return promise.future();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy