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

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

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.client.hotrod.counter.impl;

import static org.infinispan.client.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.
 *
 * @author Pedro Ruivo
 * @since 9.2
 */
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();
   }

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

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

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

   @Override
   public CompletableFuture getAndSet(long value) {
      return factory.newSetOperation(name, value, useConsistentHash()).execute();
   }

   @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());
      }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy