Interface StrongCounter


  • public interface StrongCounter
    The strong consistent counter interface.

    It provides atomic updates for the counter. All the operations are perform asynchronously and they complete the CompletableFuture when completed.

    Since:
    9.0
    Author:
    Pedro Ruivo
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<java.lang.Long> addAndGet​(long delta)
      Atomically adds the given value and return the new value.
      <T extends CounterListener>
      Handle<T>
      addListener​(T listener)
      Registers a CounterListener to this counter.
      default java.util.concurrent.CompletableFuture<java.lang.Boolean> compareAndSet​(long expect, long update)
      Atomically sets the value to the given updated value if the current value == the expected value.
      java.util.concurrent.CompletableFuture<java.lang.Long> compareAndSwap​(long expect, long update)
      Atomically sets the value to the given updated value if the current value == the expected value.
      default java.util.concurrent.CompletableFuture<java.lang.Long> decrementAndGet()
      Atomically decrements the counter and returns the new value
      CounterConfiguration getConfiguration()  
      java.lang.String getName()  
      java.util.concurrent.CompletableFuture<java.lang.Long> getValue()
      It fetches the current value.
      default java.util.concurrent.CompletableFuture<java.lang.Long> incrementAndGet()
      Atomically increments the counter and returns the new value.
      java.util.concurrent.CompletableFuture<java.lang.Void> remove()
      It removes this counter from the cluster.
      java.util.concurrent.CompletableFuture<java.lang.Void> reset()
      Resets the counter to its initial value.
      SyncStrongCounter sync()
      It returns a synchronous strong counter for this instance.
    • Method Detail

      • getName

        java.lang.String getName()
        Returns:
        The counter name.
      • getValue

        java.util.concurrent.CompletableFuture<java.lang.Long> getValue()
        It fetches the current value.

        It may go remotely to fetch the current value.

        Returns:
        The current value.
      • incrementAndGet

        default java.util.concurrent.CompletableFuture<java.lang.Long> incrementAndGet()
        Atomically increments the counter and returns the new value.
        Returns:
        The new value.
      • decrementAndGet

        default java.util.concurrent.CompletableFuture<java.lang.Long> decrementAndGet()
        Atomically decrements the counter and returns the new value
        Returns:
        The new value.
      • addAndGet

        java.util.concurrent.CompletableFuture<java.lang.Long> addAndGet​(long delta)
        Atomically adds the given value and return the new value.
        Parameters:
        delta - The non-zero value to add. It can be negative.
        Returns:
        The new value.
      • reset

        java.util.concurrent.CompletableFuture<java.lang.Void> reset()
        Resets the counter to its initial value.
      • compareAndSet

        default java.util.concurrent.CompletableFuture<java.lang.Boolean> compareAndSet​(long expect,
                                                                                        long update)
        Atomically sets the value to the given updated value if the current value == the expected value. It is the same as return compareAndSwap(expect, update).thenApply(value -> value == expect);
        Parameters:
        expect - the expected value
        update - the new value
        Returns:
        true if successful, false otherwise.
      • compareAndSwap

        java.util.concurrent.CompletableFuture<java.lang.Long> compareAndSwap​(long expect,
                                                                              long update)
        Atomically sets the value to the given updated value if the current value == the expected value. The operation is successful if the return value is equals to the expected value.
        Parameters:
        expect - the expected value.
        update - the new value.
        Returns:
        the previous counter's value.
      • remove

        java.util.concurrent.CompletableFuture<java.lang.Void> remove()
        It removes this counter from the cluster.

        Note that it doesn't remove the counter from the CounterManager. If you want to remove the counter from the CounterManager use CounterManager.remove(String).

        Returns:
        The CompletableFuture that is completed when the counter is removed from the cluster.