T
- the type of the atom's valuepublic class AsynchronousAtom<T> extends Object
If an error occurs while applying a change, the future that represents that change fails. Additionally, the atom will consult an error handler to decide what to do. There are three options:
restart(Object, boolean)
) and then immediately
resumed. This is similar to ignoring the error, except that the value is re-seeded.
If a Transaction
is in progress, then a mutation is not actually submitted to the
thread pool until the transaction is committed. So, like TransactionalAtom
s but unlike
other types of atoms, mutations to asynchronous atoms can be rolled back. When a transaction is
rolled back, all futures that correspond to rolled back asynchronous mutations are cancelled.
Ordering of operations cannot be guaranteed as asynchronous operations could be submitted and interleaved with those submitted from a given thread. Thus this form of atom is most useful when only commutative functions are applied to it.
Modifier and Type | Class and Description |
---|---|
static class |
AsynchronousAtom.ErrorAction
One of the possible actions taken when an error occurs during a mutation operation.
|
static interface |
AsynchronousAtom.ErrorHandler<T>
Handles errors that occur during a mutation operation on an atom.
|
Atom.Watcher<T>
Constructor and Description |
---|
AsynchronousAtom()
Constructs a new asynchronous atom with a
null value and no validator. |
AsynchronousAtom(T value)
Constructs a new asynchronous atom with the specified value and no validator.
|
AsynchronousAtom(T value,
Predicate<? super T> validator)
Constructs a new asynchronous atom with the specified value and the specified validator.
|
Modifier and Type | Method and Description |
---|---|
FluentFuture<T> |
accumulateAndGet(T t,
BiFunction<? super T,? super T,? extends T> function)
Submits a mutation that will combine the atom's value with the given value, using the given
function, and set the atom's value to the result.
|
boolean |
addWatcher(Atom.Watcher<? super T> watcher)
Registers a watcher, which will receive notifications when the atom's value changes.
|
T |
get()
Retrieves the current value for this atom.
|
FluentFuture<T> |
getAndAccumulate(T t,
BiFunction<? super T,? super T,? extends T> function) |
FluentFuture<T> |
getAndUpdate(Function<? super T,? extends T> function)
Applies a function to the atom's value.
|
AsynchronousAtom.ErrorHandler<? super T> |
getErrorHandler()
Returns the atom's error handler.
|
FluentFuture<T> |
getPending()
Returns a future that completes with the atom's value once all currently pending operations
complete.
|
int |
getQueueLength()
Returns the number of queued mutation operations.
|
Predicate<? super T> |
getValidator()
Return the atom's validator, or
null if there is no validator. |
boolean |
isBlocked()
Returns true if the atom is blocked due to an earlier failure.
|
protected void |
notify(T oldValue,
T newValue)
Notifies the atom's watchers that the value has changed.
|
boolean |
removeWatcher(Atom.Watcher<? super T> watcher)
Unregisters a watcher.
|
boolean |
restart()
Restarts mutation operations in a blocked atom.
|
boolean |
restart(T newSeed,
boolean cancelPending)
Restarts mutation operations in a blocked atom with the given new seed value.
|
void |
resume()
Resumes mutation operations in a blocked atom.
|
FluentFuture<T> |
set(T newValue)
Submits a mutation that will set the atom to the specified value.
|
void |
setErrorHandler(AsynchronousAtom.ErrorHandler<? super T> errorHandler)
Sets the atom's error handler.
|
FluentFuture<T> |
updateAndGet(Function<? super T,? extends T> function)
Applies a function to the atom's value.
|
protected void |
validate(T value)
Validates the specified value.
|
public AsynchronousAtom()
null
value and no validator.public AsynchronousAtom(T value)
public AsynchronousAtom.ErrorHandler<? super T> getErrorHandler()
public void setErrorHandler(AsynchronousAtom.ErrorHandler<? super T> errorHandler)
errorHandler
- the error handler that this atom should useNullPointerException
- if the specified handler is null
public boolean isBlocked()
public void resume()
public boolean restart()
restart(Object, boolean)
operation.public boolean restart(T newSeed, boolean cancelPending)
newSeed
- the new value with which to seed the restarted atomcancelPending
- if true, any queued mutations will be discarded; otherwise, queued
mutations will be applied to the new seeded valueIllegalArgumentException
- if the given restart value is invalid according to the atom's
current validatorpublic int getQueueLength()
public T get()
This is a volatile read of the atom's value. There may be pending and/or concurrently
executing operations that will change and/or are changing this value. To get the value of
this atom after all such pending operations are complete, use getPending()
instead.
public FluentFuture<T> getPending()
restart()
that cancels pending operations, then this future will be
cancelled.public FluentFuture<T> set(T newValue)
newValue
- the new value for the atomIllegalArgumentException
- if the specified value is not valid for this atompublic FluentFuture<T> updateAndGet(Function<? super T,? extends T> function)
Since validation cannot be done immediately, validation failure manifests as a failed
future. Depending on the atom's error handler
, a validation failure
could block subsequent mutations.
function
- the function to applypublic FluentFuture<T> getAndUpdate(Function<? super T,? extends T> function)
Since validation cannot be done immediately, validation failure manifests as a failed
future. Depending on the atom's error handler
, a validation failure
could block subsequent mutations.
function
- the function to applypublic FluentFuture<T> accumulateAndGet(T t, BiFunction<? super T,? super T,? extends T> function)
error handler
, a validation failure could block subsequent
mutations.t
- the value to combinefunction
- the function to applypublic FluentFuture<T> getAndAccumulate(T t, BiFunction<? super T,? super T,? extends T> function)
protected void validate(T value)
value
- the value to validateIllegalArgumentException
- if the specified value is invalidpublic Predicate<? super T> getValidator()
null
if there is no validator. The validator is a
predicate that returns true when invoked on valid values for the item and false otherwise.protected void notify(T oldValue, T newValue)
oldValue
- the atom's old valuenewValue
- the atom's new valuepublic boolean addWatcher(Atom.Watcher<? super T> watcher)
Atom
addWatcher
in interface Atom<T>
watcher
- the watcherpublic boolean removeWatcher(Atom.Watcher<? super T> watcher)
Atom
removeWatcher
in interface Atom<T>
watcher
- the watcher