public class SimplePromise<RESULT> extends java.lang.Object implements Promise<RESULT>
Promise
that can be retrofitted into any asynchronous flow:
SimplePromise
SimplePromise
instance in your runnable running in the backgroundSimplePromise
in the calling thread
public Promise<MyResult> getResultAsync () {
final Promise<MyResult> promise = new SimplePromise<MyResult> ()
executor.execute (new Runnable () {
public void run () {
try {
promise.success (getResultFromNetworkOrDiskOrWhatever ());
} catch (Exception e) {
promise.failed (e);
}
}
});
return promise;
}
Promise.OnCancelListener, Promise.OnCompleteListener<RESULT>, Promise.OnFailListener
Constructor and Description |
---|
SimplePromise() |
SimplePromise(java.util.concurrent.Executor executor) |
Modifier and Type | Method and Description |
---|---|
void |
cancelled()
Mark task as cancelled
|
void |
failed(java.lang.Throwable t)
Mark task as failed and provide a cause
|
boolean |
isCancelled()
Was the task cancelled?
|
boolean |
isDone()
Are we there yet?
|
boolean |
isFailed()
Did the task fail?
|
SimplePromise<RESULT> |
onCancel(Promise.OnCancelListener listener)
Set cancellation callback
|
SimplePromise<RESULT> |
onComplete(Promise.OnCompleteListener<RESULT> listener)
Set completion callback
|
SimplePromise<RESULT> |
onFail(Promise.OnFailListener listener)
Set failure callback
|
boolean |
succeeded()
Was the promise successfully completed?
|
void |
success(RESULT r)
Report task result and mark task as done
|
public SimplePromise()
public SimplePromise(java.util.concurrent.Executor executor)
public void success(RESULT r)
r
- obtained resultpublic void failed(java.lang.Throwable t)
t
- cause of the failurepublic void cancelled()
public SimplePromise<RESULT> onComplete(Promise.OnCompleteListener<RESULT> listener)
onComplete
in interface Promise<RESULT>
listener
- listener to notify on completionPromise
to chain more callbackspublic SimplePromise<RESULT> onFail(Promise.OnFailListener listener)
public SimplePromise<RESULT> onCancel(Promise.OnCancelListener listener)
public boolean isDone()
public boolean succeeded()
public boolean isFailed()
public boolean isCancelled()
isCancelled
in interface Promise<RESULT>
true
if cancelled and false
otherwise