Interface IAtomicLong

  • All Superinterfaces:
    DistributedObject

    public interface IAtomicLong
    extends DistributedObject
    IAtomicLong is a redundant and highly available distributed alternative to the AtomicLong.

    Asynchronous variants of all methods have been introduced in version 3.7. Async methods immediately return a CompletionStage which can be used to chain further computation stages or can be converted to a CompletableFuture from which the operation's result can be obtained in a blocking manner. For example:

     CompletionStage<Long> stage = atomicLong.addAndGetAsync(13);
     stage.whenCompleteAsync((response, t) -> {
         if (t == null) {
             // do something with the result
         } else {
             // handle failure
         }
     });
     

    Actions supplied for dependent completions of default non-async methods and async methods without an explicit Executor argument are performed by the ForkJoinPool.commonPool() (unless it does not support a parallelism level of at least 2, in which case a new Thread is created per task).

    IAtomicLong is accessed via CPSubsystem.getAtomicLong(String). It works on top of the Raft consensus algorithm. It offers linearizability during crash failures and network partitions. It is CP with respect to the CAP principle. If a network partition occurs, it remains available on at most one side of the partition.

    IAtomicLong impl does not offer exactly-once / effectively-once execution semantics. It goes with at-least-once execution semantics by default and can cause an API call to be committed multiple times in case of CP member failures. It can be tuned to offer at-most-once execution semantics. Please see CPSubsystemConfig.setFailOnIndeterminateOperationState(boolean)

    See Also:
    IAtomicReference
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      long addAndGet​(long delta)
      Atomically adds the given value to the current value.
      java.util.concurrent.CompletionStage<java.lang.Long> addAndGetAsync​(long delta)
      Atomically adds the given value to the current value.
      void alter​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it.
      long alterAndGet​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it and gets the result.
      java.util.concurrent.CompletionStage<java.lang.Long> alterAndGetAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it and gets the result.
      java.util.concurrent.CompletionStage<java.lang.Void> alterAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it.
      <R> R apply​(IFunction<java.lang.Long,​R> function)
      Applies a function on the value, the actual stored value will not change.
      <R> java.util.concurrent.CompletionStage<R> applyAsync​(IFunction<java.lang.Long,​R> function)
      Applies a function on the value, the actual stored value will not change.
      boolean compareAndSet​(long expect, long update)
      Atomically sets the value to the given updated value only if the current value == the expected value.
      java.util.concurrent.CompletionStage<java.lang.Boolean> compareAndSetAsync​(long expect, long update)
      Atomically sets the value to the given updated value only if the current value == the expected value.
      long decrementAndGet()
      Atomically decrements the current value by one.
      java.util.concurrent.CompletionStage<java.lang.Long> decrementAndGetAsync()
      Atomically decrements the current value by one.
      long get()
      Gets the current value.
      long getAndAdd​(long delta)
      Atomically adds the given value to the current value.
      java.util.concurrent.CompletionStage<java.lang.Long> getAndAddAsync​(long delta)
      Atomically adds the given value to the current value.
      long getAndAlter​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it on and gets the old value.
      java.util.concurrent.CompletionStage<java.lang.Long> getAndAlterAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
      Alters the currently stored value by applying a function on it on and gets the old value.
      long getAndDecrement()
      Atomically decrements the current value by one.
      java.util.concurrent.CompletionStage<java.lang.Long> getAndDecrementAsync()
      Atomically decrements the current value by one.
      long getAndIncrement()
      Atomically increments the current value by one.
      java.util.concurrent.CompletionStage<java.lang.Long> getAndIncrementAsync()
      Atomically increments the current value by one.
      long getAndSet​(long newValue)
      Atomically sets the given value and returns the old value.
      java.util.concurrent.CompletionStage<java.lang.Long> getAndSetAsync​(long newValue)
      Atomically sets the given value and returns the old value.
      java.util.concurrent.CompletionStage<java.lang.Long> getAsync()
      Gets the current value.
      java.lang.String getName()
      Returns the name of this IAtomicLong instance.
      long incrementAndGet()
      Atomically increments the current value by one.
      java.util.concurrent.CompletionStage<java.lang.Long> incrementAndGetAsync()
      Atomically increments the current value by one.
      void set​(long newValue)
      Atomically sets the given value.
      java.util.concurrent.CompletionStage<java.lang.Void> setAsync​(long newValue)
      Atomically sets the given value.
    • Method Detail

      • getName

        java.lang.String getName()
        Returns the name of this IAtomicLong instance.
        Specified by:
        getName in interface DistributedObject
        Returns:
        the name of this IAtomicLong instance
      • addAndGet

        long addAndGet​(long delta)
        Atomically adds the given value to the current value.
        Parameters:
        delta - the value to add to the current value
        Returns:
        the updated value, the given value added to the current value
      • compareAndSet

        boolean compareAndSet​(long expect,
                              long update)
        Atomically sets the value to the given updated value only if the current value == the expected value.
        Parameters:
        expect - the expected value
        update - the new value
        Returns:
        true if successful; or false if the actual value was not equal to the expected value.
      • decrementAndGet

        long decrementAndGet()
        Atomically decrements the current value by one.
        Returns:
        the updated value, the current value decremented by one
      • getAndDecrement

        long getAndDecrement()
        Atomically decrements the current value by one.
        Returns:
        the old value
      • get

        long get()
        Gets the current value.
        Returns:
        the current value
      • getAndAdd

        long getAndAdd​(long delta)
        Atomically adds the given value to the current value.
        Parameters:
        delta - the value to add to the current value
        Returns:
        the old value before the add
      • getAndSet

        long getAndSet​(long newValue)
        Atomically sets the given value and returns the old value.
        Parameters:
        newValue - the new value
        Returns:
        the old value
      • incrementAndGet

        long incrementAndGet()
        Atomically increments the current value by one.
        Returns:
        the updated value, the current value incremented by one
      • getAndIncrement

        long getAndIncrement()
        Atomically increments the current value by one.
        Returns:
        the old value
      • set

        void set​(long newValue)
        Atomically sets the given value.
        Parameters:
        newValue - the new value
      • alter

        void alter​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it.
        Parameters:
        function - the function applied to the currently stored value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.2
      • alterAndGet

        long alterAndGet​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it and gets the result.
        Parameters:
        function - the function applied to the currently stored value
        Returns:
        the new value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.2
      • getAndAlter

        long getAndAlter​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it on and gets the old value.
        Parameters:
        function - the function applied to the currently stored value
        Returns:
        the old value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.2
      • apply

        <R> R apply​(IFunction<java.lang.Long,​R> function)
        Applies a function on the value, the actual stored value will not change.
        Type Parameters:
        R - the result type of the function
        Parameters:
        function - the function applied to the value, the value is not changed
        Returns:
        the result of the function application
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.2
      • addAndGetAsync

        java.util.concurrent.CompletionStage<java.lang.Long> addAndGetAsync​(long delta)
        Atomically adds the given value to the current value.

        This method will dispatch a request and return immediately a CompletionStage.

        The operations result can be obtained in a blocking way, or a callback can be provided for execution upon completion, as demonstrated in the following examples:

         CompletionStage<Long> stage = atomicLong.addAndGetAsync(13);
         // do something else, then read the result
        
         // this method will block until the result is available
         Long result = stage.toCompletableFuture().get();
         
         CompletionStage<Long> stage = atomicLong.addAndGetAsync(13);
         stage.whenCompleteAsync((response, t) -> {
             if (t == null) {
                 // do something with the result
             } else {
                 // handle failure
             }
         });
         
        Parameters:
        delta - the value to add
        Returns:
        a CompletionStage bearing the response
        Since:
        3.7
      • compareAndSetAsync

        java.util.concurrent.CompletionStage<java.lang.Boolean> compareAndSetAsync​(long expect,
                                                                                   long update)
        Atomically sets the value to the given updated value only if the current value == the expected value.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        expect - the expected value
        update - the new value
        Returns:
        an CompletionStage with value true if successful; or false if the actual value was not equal to the expected value
        Since:
        3.7
      • decrementAndGetAsync

        java.util.concurrent.CompletionStage<java.lang.Long> decrementAndGetAsync()
        Atomically decrements the current value by one.

        This method will dispatch a request and return immediately a CompletionStage.

        Returns:
        a CompletionStage with the updated value
        Since:
        3.7
      • getAndDecrementAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAndDecrementAsync()
        Atomically decrements the current value by one.

        This method will dispatch a request and return immediately a CompletionStage.

        Returns:
        a CompletionStage with the old value
        Since:
        4.1
      • getAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAsync()
        Gets the current value. This method will dispatch a request and return immediately a CompletionStage.
        Returns:
        a CompletionStage with the current value
        Since:
        3.7
      • getAndAddAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAndAddAsync​(long delta)
        Atomically adds the given value to the current value.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        delta - the value to add
        Returns:
        a CompletionStage with the old value before the addition
        Since:
        3.7
      • getAndSetAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAndSetAsync​(long newValue)
        Atomically sets the given value and returns the old value.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        newValue - the new value
        Returns:
        a CompletionStage with the old value
        Since:
        3.7
      • incrementAndGetAsync

        java.util.concurrent.CompletionStage<java.lang.Long> incrementAndGetAsync()
        Atomically increments the current value by one.

        This method will dispatch a request and return immediately a CompletionStage.

        Returns:
        a CompletionStage with the updated value
        Since:
        3.7
      • getAndIncrementAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAndIncrementAsync()
        Atomically increments the current value by one.

        This method will dispatch a request and return immediately a CompletionStage.

        Returns:
        a CompletionStage with the old value
        Since:
        3.7
      • setAsync

        java.util.concurrent.CompletionStage<java.lang.Void> setAsync​(long newValue)
        Atomically sets the given value.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        newValue - the new value
        Returns:
        a CompletionStage
        Since:
        3.7
      • alterAsync

        java.util.concurrent.CompletionStage<java.lang.Void> alterAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        function - the function
        Returns:
        a CompletionStage with the new value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.7
      • alterAndGetAsync

        java.util.concurrent.CompletionStage<java.lang.Long> alterAndGetAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it and gets the result.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        function - the function
        Returns:
        a CompletionStage with the new value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.7
      • getAndAlterAsync

        java.util.concurrent.CompletionStage<java.lang.Long> getAndAlterAsync​(IFunction<java.lang.Long,​java.lang.Long> function)
        Alters the currently stored value by applying a function on it on and gets the old value.

        This method will dispatch a request and return immediately a CompletionStage.

        Parameters:
        function - the function
        Returns:
        a CompletionStage with the old value
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.7
      • applyAsync

        <R> java.util.concurrent.CompletionStage<R> applyAsync​(IFunction<java.lang.Long,​R> function)
        Applies a function on the value, the actual stored value will not change.

        This method will dispatch a request and return immediately a CompletionStage. For example:

         class IsOneFunction implements IFunction<Long, Boolean> {
             @Override
             public Boolean apply(Long input) {
                 return input.equals(1L);
             }
         }
        
         CompletionStage<Boolean> stage = atomicLong.applyAsync(new IsOneFunction());
         stage.whenCompleteAsync((response, t) -> {
            if (t == null) {
                // do something with the response
            } else {
               // handle failure
            }
         });
         
        Type Parameters:
        R - the result type of the function
        Parameters:
        function - the function
        Returns:
        a CompletionStage with the result of the function application
        Throws:
        java.lang.IllegalArgumentException - if function is null
        Since:
        3.7