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
Modifier and TypeMethodDescriptionaddAndGet(long delta) Atomically adds the given value and return the new value.<T extends CounterListener>
Handle<T> addListener(T listener) Registers aCounterListenerto this counter.default CompletableFuture<Boolean> compareAndSet(long expect, long update) Atomically sets the value to the given updated value if the current value==the expected value.compareAndSwap(long expect, long update) Atomically sets the value to the given updated value if the current value==the expected value.default CompletableFuture<Long> Atomically decrements the counter and returns the new valuegetAndSet(long value) Atomically sets the value to the given updated valuegetName()getValue()It fetches the current value.default CompletableFuture<Long> Atomically increments the counter and returns the new value.remove()It removes this counter from the cluster.reset()Resets the counter to its initial value.sync()It returns a synchronous strong counter for this instance.
-
Method Details
-
getName
String getName()- Returns:
- The counter name.
-
getValue
CompletableFuture<Long> getValue()It fetches the current value.It may go remotely to fetch the current value.
- Returns:
- The current value.
-
incrementAndGet
Atomically increments the counter and returns the new value.- Returns:
- The new value.
-
decrementAndGet
Atomically decrements the counter and returns the new value- Returns:
- The new value.
-
addAndGet
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
CompletableFuture<Void> reset()Resets the counter to its initial value. -
addListener
Registers aCounterListenerto this counter.- Type Parameters:
T- The concrete type of the listener. It must implementCounterListener.- Parameters:
listener- The listener to register.- Returns:
- A
Handlethat allows to remove the listener viaHandle.remove().
-
compareAndSet
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 valueupdate- the new value- Returns:
trueif successful,falseotherwise.
-
compareAndSwap
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.
-
getConfiguration
CounterConfiguration getConfiguration()- Returns:
- the
CounterConfigurationused by this counter.
-
remove
CompletableFuture<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 theCounterManageruseCounterManager.remove(String).- Returns:
- The
CompletableFuturethat is completed when the counter is removed from the cluster.
-
sync
SyncStrongCounter sync()It returns a synchronous strong counter for this instance.- Returns:
- a
SyncStrongCounter.
-
getAndSet
Atomically sets the value to the given updated value- Parameters:
value- the expected value.- Returns:
- the previous counter's value.
-