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

org.infinispan.hotrod.impl.counter.StrongCounterImpl Maven / Gradle / Ivy

package org.infinispan.hotrod.impl.counter;

import static org.infinispan.hotrod.impl.Util.await;

import java.util.concurrent.CompletableFuture;

import org.infinispan.counter.api.CounterConfiguration;
import org.infinispan.counter.api.StrongCounter;
import org.infinispan.counter.api.SyncStrongCounter;

/**
 * A {@link StrongCounter} implementation for Hot Rod clients.
 *
 * @since 14.0
 */
class StrongCounterImpl extends BaseCounter implements StrongCounter {
   private final SyncStrongCounter syncCounter;

   StrongCounterImpl(String name, CounterConfiguration configuration, CounterOperationFactory operationFactory,
                     NotificationManager notificationManager) {
      super(configuration, name, operationFactory, notificationManager);
      this.syncCounter = new Sync();
   }

   public CompletableFuture getValue() {
      return factory.newGetValueOperation(name, useConsistentHash()).execute().toCompletableFuture();
   }

   public CompletableFuture addAndGet(long delta) {
      return factory.newAddOperation(name, delta, useConsistentHash()).execute().toCompletableFuture();
   }

   @Override
   public CompletableFuture compareAndSwap(long expect, long update) {
      return factory.newCompareAndSwapOperation(name, expect, update, super.getConfiguration()).execute().toCompletableFuture();
   }

   @Override
   public SyncStrongCounter sync() {
      return syncCounter;
   }

   @Override
   boolean useConsistentHash() {
      return true;
   }

   private class Sync implements SyncStrongCounter {

      @Override
      public long addAndGet(long delta) {
         return await(StrongCounterImpl.this.addAndGet(delta));
      }

      @Override
      public void reset() {
         await(StrongCounterImpl.this.reset());
      }

      @Override
      public long getValue() {
         return await(StrongCounterImpl.this.getValue());
      }

      @Override
      public long compareAndSwap(long expect, long update) {
         return await(StrongCounterImpl.this.compareAndSwap(expect, update));
      }

      @Override
      public String getName() {
         return name;
      }

      @Override
      public CounterConfiguration getConfiguration() {
         return configuration;
      }

      @Override
      public void remove() {
         await(StrongCounterImpl.this.remove());
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy