T
- type of the completable stagepublic class ConcurrentCompletable<T> extends Object implements Handle<T>, Completable<T>
Completable
.
The callback uses the calling thread to execute result listeners, see
postComplete()
for details.
Constructor and Description |
---|
ConcurrentCompletable(Caller caller)
Setup a concurrent completable that uses a custom caller implementation.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancel()
Cancel the current stage.
|
void |
cancelled()
Handle to be called when the underlying stage is cancelled.
|
Throwable |
cause()
Get the cause of a failed stage.
|
boolean |
complete(T result)
Complete the current stage.
|
void |
completed(T result)
Handle to be called when the underlying stage is completed.
|
boolean |
fail(Throwable cause)
Fail the current stage.
|
void |
failed(Throwable cause)
Handle to be called when the underlying stage is failed.
|
Stage<T> |
handle(Handle<? super T> handle)
Register a listener that is called on all three types of events for the current stage; completed,
failed, and cancelled.
|
boolean |
isCancelled()
Check if stage is in the cancelled state.
|
boolean |
isCompleted()
Check if stage is completed.
|
boolean |
isDone()
Indicates if the stage is done
|
boolean |
isFailed()
Check if the stage was exceptionally completed.
|
T |
join()
Join the result of the current stage.
|
T |
join(long timeout,
TimeUnit unit)
Join the result of the current stage with a timeout.
|
T |
joinNow()
Join the result of the current stage, or fail if it's state is still running.
|
<U> Stage<U> |
thenApply(Function<? super T,? extends U> fn)
Transform the value of the current stage into another type using an immediate function.
|
Stage<T> |
thenApplyFailed(Function<? super Throwable,? extends T> fn)
Apply a failed stage.
|
<U> Stage<U> |
thenCancel()
Build a stage is cancelled, but waits until the current stage completes.
|
<U> Stage<U> |
thenComplete(U result)
Build a stage is completed, but waits until the current stage completes.
|
<U> Stage<U> |
thenCompose(Function<? super T,? extends Stage<U>> fn)
Compose the current stage with the given function.
|
Stage<T> |
thenComposeCaught(Function<? super Throwable,? extends Stage<T>> fn)
Compose a failed stage.
|
<U> Stage<U> |
thenFail(Throwable cause)
Build a stage is failed, but waits until the current stage completes.
|
String |
toString() |
Stage<T> |
whenCancelled(Runnable cancelled)
Register an listener to be called when the current stage is cancelled.
|
Stage<T> |
whenComplete(Consumer<? super T> consumer)
Register a listener to be called when the current stage is completed.
|
Stage<T> |
whenDone(Runnable finishable)
Register a listener to be called when the current stage finishes for any reason.
|
Stage<T> |
whenFailed(Consumer<? super Throwable> consumer)
Register a listener that is called when a stage is failed.
|
Stage<T> |
withCloser(Supplier<? extends Stage<Void>> complete,
Supplier<? extends Stage<Void>> notComplete)
Run one of the provided stages when this stage ends up in a given state.
|
Stage<T> |
withComplete(Supplier<? extends Stage<Void>> supplier)
Run the provided stage when the current stage has completed.
|
Stage<T> |
withNotComplete(Supplier<? extends Stage<Void>> supplier)
Run the provided stage when the current stage ends, but does not complete.
|
public ConcurrentCompletable(Caller caller)
The provided
caller implementation will be called from the calling thread of handle(Handle)
, or
other public methods interacting with this completable.
It is therefore suggested to provide an implementation that supports delegating tasks to a separate thread pool.
caller
- The caller implementation to use.public void completed(T result)
Handle
public void failed(Throwable cause)
Handle
public void cancelled()
Handle
public boolean complete(T result)
Completable
This takes the stage out of the pending state and into the completed state.
complete
in interface Completable<T>
result
- result of the computationtrue
if the completable was completed by this callpublic boolean fail(Throwable cause)
Completable
This takes the stage out of the pending state and into the cancelled state.
fail
in interface Completable<T>
cause
- the cause of the failuretrue
if the completable was completed by this callpublic boolean cancel()
Stage
public Stage<T> handle(Handle<? super T> handle)
Stage
public Stage<T> whenDone(Runnable finishable)
Stage
public Stage<T> whenComplete(Consumer<? super T> consumer)
Stage
whenComplete
in interface Stage<T>
consumer
- listener to registerpublic Stage<T> whenFailed(Consumer<? super Throwable> consumer)
Stage
whenFailed
in interface Stage<T>
consumer
- listener to registerpublic Stage<T> whenCancelled(Runnable cancelled)
Stage
whenCancelled
in interface Stage<T>
cancelled
- listener to registerpublic boolean isDone()
Stage
public boolean isCompleted()
Stage
isCompleted
in interface Stage<T>
true
if the stage is in a completed state, otherwise false
.public boolean isFailed()
Stage
isFailed
in interface Stage<T>
true
if the stage is exceptionally completedCompletable.fail(Throwable)
public boolean isCancelled()
Stage
isCancelled
in interface Stage<T>
true
if the stage is in a cancelled state, otherwise false
.public Throwable cause()
Stage
cause
in interface Stage<T>
Stage.isFailed()
public T join() throws InterruptedException, ExecutionException
Stage
join
in interface Stage<T>
InterruptedException
- when this thread is interruptedExecutionException
- when the underlying computation throws an exceptionpublic T joinNow() throws ExecutionException
Stage
joinNow
in interface Stage<T>
ExecutionException
- when the underlying computation throws an exceptionpublic T join(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Stage
join
in interface Stage<T>
timeout
- timeout after which TimeoutException
will be
thrown.unit
- unit of the timeoutInterruptedException
- when this thread is interruptedExecutionException
- when the underlying computation throws an exceptionTimeoutException
- when the computation does not finish within the given timeoutpublic <U> Stage<U> thenApply(Function<? super T,? extends U> fn)
Stage
Translates the result of a completed stage as it becomes available:
operation().thenApply(result -> result / 2);
public <U> Stage<U> thenCompose(Function<? super T,? extends Stage<U>> fn)
Stage
When the current stage has completed, calls the given function with the result of the current stage.
Stage<A> a = op1();
Stage<B> a.thenCompose(result -> op2(result));
thenCompose
in interface Stage<T>
U
- type of the composed stagefn
- the function to use when transforming the valuepublic Stage<T> thenApplyFailed(Function<? super Throwable,? extends T> fn)
Stage
This acts like a try-catch, where the provided function is the catch operation.
thenApplyFailed
in interface Stage<T>
fn
- the transformation to usepublic Stage<T> thenComposeCaught(Function<? super Throwable,? extends Stage<T>> fn)
Stage
This acts like a try-catch, where the composed stage is the catch operation.
If the intent is to re-throw after running the catch operation, use
Stage.withNotComplete(java.util.function.Supplier)
thenComposeCaught
in interface Stage<T>
fn
- the transformation to usepublic Stage<T> withCloser(Supplier<? extends Stage<Void>> complete, Supplier<? extends Stage<Void>> notComplete)
Stage
This is typically used to implement catch-then-rethrow style operations, like the following example:
beginTransaction().thenCompose(tx -> {
return oper2(tx).withCloser(tx::commit, tx::rollback);
});
withCloser
in interface Stage<T>
complete
- stage to run when this stage ends in a complete statenotComplete
- stage to run when this stage ends in a non-complete state, if the state is
failed, any exception thrown in this block will suppress the original exceptionpublic Stage<T> withComplete(Supplier<? extends Stage<Void>> supplier)
Stage
withComplete
in interface Stage<T>
supplier
- supplier of the stage to runpublic Stage<T> withNotComplete(Supplier<? extends Stage<Void>> supplier)
Stage
withNotComplete
in interface Stage<T>
supplier
- supplier of the stage to runpublic <U> Stage<U> thenFail(Throwable cause)
Stage
public <U> Stage<U> thenComplete(U result)
Stage
thenComplete
in interface Stage<T>
U
- target typeresult
- result to complete withpublic <U> Stage<U> thenCancel()
Stage
thenCancel
in interface Stage<T>
U
- target typeCopyright © 2017. All rights reserved.