Package org.jdeferred2.impl
Class DelegatingPromise<D,F,P>
- java.lang.Object
-
- org.jdeferred2.impl.DelegatingPromise<D,F,P>
-
- Type Parameters:
D
- Type used fordone(DoneCallback)
F
- Type used forfail(FailCallback)
P
- Type used forprogress(ProgressCallback)
- All Implemented Interfaces:
Promise<D,F,P>
- Direct Known Subclasses:
DeferredPromise
public abstract class DelegatingPromise<D,F,P> extends java.lang.Object implements Promise<D,F,P>
Promise implementation of the Decorator Pattern.- Since:
- 1.2.5
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jdeferred2.Promise
Promise.State
-
-
Constructor Summary
Constructors Constructor Description DelegatingPromise(Promise<D,F,P> delegate)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Promise<D,F,P>
always(AlwaysCallback<? super D,? super F> callback)
This method will registerAlwaysCallback
so that when a Deferred object is either resolved (Deferred.resolve(Object)
) or rejected (Deferred.reject(Object)
),AlwaysCallback
will be triggered.Promise<D,F,P>
done(DoneCallback<? super D> callback)
This method will registerDoneCallback
so that when a Deferred object is resolved (Deferred.resolve(Object)
),DoneCallback
will be triggered.Promise<D,F,P>
fail(FailCallback<? super F> callback)
This method will registerFailCallback
so that when a Deferred object is rejected (Deferred.reject(Object)
),FailCallback
will be triggered.<D_OUT> Promise<D_OUT,F,P>
filter(DoneFilter<? super D,? extends D_OUT> doneFilter)
Equivalent tofilter(doneFilter, null, null)
<D_OUT,F_OUT>
Promise<D_OUT,F_OUT,P>filter(DoneFilter<? super D,? extends D_OUT> doneFilter, FailFilter<? super F,? extends F_OUT> failFilter)
Equivalent tofilter(doneFilter, failFilter, null)
<D_OUT,F_OUT,P_OUT>
Promise<D_OUT,F_OUT,P_OUT>filter(DoneFilter<? super D,? extends D_OUT> doneFilter, FailFilter<? super F,? extends F_OUT> failFilter, ProgressFilter<? super P,? extends P_OUT> progressFilter)
This method will register filters such that when a Deferred object is either resolved (Deferred.resolve(Object)
), rejected (Deferred.reject(Object)
) or is notified of progress (Deferred.notify(Object)
), the corresponding filter will be invoked.protected Promise<D,F,P>
getDelegate()
Returns the delegate Promise wrapped by this Promise.boolean
isPending()
Queries the state of this promise, returningtrue
iff it isState.PENDING
.boolean
isRejected()
Queries the state of this promise, returningtrue
iff it isState.REJECTED
.boolean
isResolved()
Queries the state of this promise, returningtrue
iff it isState.RESOLVED
.<D_OUT,F_OUT,P_OUT>
Promise<D_OUT,F_OUT,P_OUT>pipe(DonePipe<? super D,? extends D_OUT,? extends F_OUT,? extends P_OUT> donePipe, FailPipe<? super F,? extends D_OUT,? extends F_OUT,? extends P_OUT> failPipe, ProgressPipe<? super P,? extends D_OUT,? extends F_OUT,? extends P_OUT> progressPipe)
This method will register pipes such that when a Deferred object is either resolved (Deferred.resolve(Object)
), rejected (Deferred.reject(Object)
) or is notified of progress (Deferred.notify(Object)
), the corresponding pipe will be invoked.<D_OUT,F_OUT>
Promise<D_OUT,F_OUT,P>pipe(DonePipe<? super D,? extends D_OUT,? extends F_OUT,? extends P> donePipe, FailPipe<? super F,? extends D_OUT,? extends F_OUT,? extends P> failPipe)
Equivalent topipe(DonePipe, FailPipe, null)
<D_OUT> Promise<D_OUT,F,P>
pipe(DonePipe<? super D,? extends D_OUT,? extends F,? extends P> donePipe)
Equivalent to {#code pipe(DonePipe, null, null)}<D_OUT,F_OUT>
Promise<D_OUT,F_OUT,P>pipeAlways(AlwaysPipe<? super D,? super F,? extends D_OUT,? extends F_OUT,? extends P> alwaysPipe)
This method will register a pipe such that when a Deferred object is either resolved (Deferred.resolve(Object)
) or rejected (Deferred.reject(Object)
) the pipe will be invoked.Promise<D,F,P>
progress(ProgressCallback<? super P> callback)
This method will registerProgressCallback
so that when a Deferred object is notified of progress (Deferred.notify(Object)
),ProgressCallback
will be triggered.Promise.State
state()
Promise<D,F,P>
then(DoneCallback<? super D> doneCallback)
Equivalent toPromise.done(DoneCallback)
Promise<D,F,P>
then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback)
Equivalent toPromise.done(DoneCallback)
.Promise.fail(FailCallback)
Promise<D,F,P>
then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback, ProgressCallback<? super P> progressCallback)
void
waitSafely()
This method will wait as long as the State is Pending.void
waitSafely(long timeout)
This method will wait when the State is Pending, and return when timeout has reached.
-
-
-
Method Detail
-
getDelegate
protected Promise<D,F,P> getDelegate()
Returns the delegate Promise wrapped by this Promise.- Returns:
-
state
public Promise.State state()
-
isPending
public boolean isPending()
Description copied from interface:Promise
Queries the state of this promise, returningtrue
iff it isState.PENDING
.
-
isResolved
public boolean isResolved()
Description copied from interface:Promise
Queries the state of this promise, returningtrue
iff it isState.RESOLVED
.- Specified by:
isResolved
in interfacePromise<D,F,P>
- Returns:
true
if the current state of this promise isState.RESOLVED
,false
otherwise.- See Also:
Promise.State.RESOLVED
-
isRejected
public boolean isRejected()
Description copied from interface:Promise
Queries the state of this promise, returningtrue
iff it isState.REJECTED
.- Specified by:
isRejected
in interfacePromise<D,F,P>
- Returns:
true
if the current state of this promise isState.REJECTED
,false
otherwise.- See Also:
Promise.State.REJECTED
-
then
public Promise<D,F,P> then(DoneCallback<? super D> doneCallback)
Description copied from interface:Promise
Equivalent toPromise.done(DoneCallback)
-
then
public Promise<D,F,P> then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback)
Description copied from interface:Promise
Equivalent toPromise.done(DoneCallback)
.Promise.fail(FailCallback)
- Specified by:
then
in interfacePromise<D,F,P>
- Parameters:
doneCallback
- seePromise.done(DoneCallback)
failCallback
- seePromise.fail(FailCallback)
- Returns:
this
for chaining more calls
-
then
public Promise<D,F,P> then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback, ProgressCallback<? super P> progressCallback)
Description copied from interface:Promise
Equivalent toPromise.done(DoneCallback)
.Promise.fail(FailCallback)
.Promise.progress(ProgressCallback)
- Specified by:
then
in interfacePromise<D,F,P>
- Parameters:
doneCallback
- seePromise.done(DoneCallback)
failCallback
- seePromise.fail(FailCallback)
progressCallback
- seePromise.progress(ProgressCallback)
- Returns:
this
for chaining more calls
-
filter
public <D_OUT> Promise<D_OUT,F,P> filter(DoneFilter<? super D,? extends D_OUT> doneFilter)
Description copied from interface:Promise
Equivalent tofilter(doneFilter, null, null)
-
filter
public <D_OUT,F_OUT> Promise<D_OUT,F_OUT,P> filter(DoneFilter<? super D,? extends D_OUT> doneFilter, FailFilter<? super F,? extends F_OUT> failFilter)
Description copied from interface:Promise
Equivalent tofilter(doneFilter, failFilter, null)
- Specified by:
filter
in interfacePromise<D,F,P>
- Parameters:
doneFilter
- the filter to execute when a result is availablefailFilter
- the filter to execute when a failure is available- Returns:
- a new promise for the filtered result and failure.
- See Also:
Promise.filter(DoneFilter, FailFilter, ProgressFilter)
-
filter
public <D_OUT,F_OUT,P_OUT> Promise<D_OUT,F_OUT,P_OUT> filter(DoneFilter<? super D,? extends D_OUT> doneFilter, FailFilter<? super F,? extends F_OUT> failFilter, ProgressFilter<? super P,? extends P_OUT> progressFilter)
Description copied from interface:Promise
This method will register filters such that when a Deferred object is either resolved (Deferred.resolve(Object)
), rejected (Deferred.reject(Object)
) or is notified of progress (Deferred.notify(Object)
), the corresponding filter will be invoked. The result of the filter will be used to invoke the same action on the returned promise.DoneFilter
andFailFilter
will be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. Filters allow to transform the outcome of a promise into something else. This concept is equivalent to the map() method of the java stream API. If any of the filter is not specified (null
), a default No Op filter is used. If your filter is returning aPromise
consider usingPromise.pipe(DonePipe, FailPipe, ProgressPipe)
.Deferred deferred = new DeferredObject(); Promise promise = deferred.promise(); Promise filtered = promise.filter(new DoneFilter
() { Integer filterDone(Integer result) { return result * 10; } }); filtered.then(new DoneCallback () { void onDone(Integer result) { System.out.println(result); } }); deferred.resolve(1); // prints 10 - Specified by:
filter
in interfacePromise<D,F,P>
- Parameters:
doneFilter
- the filter to execute when a result is available. Ifnull
, useFilteredPromise.NoOpDoneFilter
failFilter
- the filter to execute when a failure is available. Ifnull
, useFilteredPromise.NoOpFailFilter
progressFilter
- the filter to execute when progress info is available. Ifnull
, useFilteredPromise.NoOpProgressFilter
- Returns:
- a new promise for the filtered result, failure and progress.
-
pipe
public <D_OUT> Promise<D_OUT,F,P> pipe(DonePipe<? super D,? extends D_OUT,? extends F,? extends P> donePipe)
Description copied from interface:Promise
Equivalent to {#code pipe(DonePipe, null, null)}
-
pipe
public <D_OUT,F_OUT> Promise<D_OUT,F_OUT,P> pipe(DonePipe<? super D,? extends D_OUT,? extends F_OUT,? extends P> donePipe, FailPipe<? super F,? extends D_OUT,? extends F_OUT,? extends P> failPipe)
Description copied from interface:Promise
Equivalent topipe(DonePipe, FailPipe, null)
-
pipe
public <D_OUT,F_OUT,P_OUT> Promise<D_OUT,F_OUT,P_OUT> pipe(DonePipe<? super D,? extends D_OUT,? extends F_OUT,? extends P_OUT> donePipe, FailPipe<? super F,? extends D_OUT,? extends F_OUT,? extends P_OUT> failPipe, ProgressPipe<? super P,? extends D_OUT,? extends F_OUT,? extends P_OUT> progressPipe)
Description copied from interface:Promise
This method will register pipes such that when a Deferred object is either resolved (Deferred.resolve(Object)
), rejected (Deferred.reject(Object)
) or is notified of progress (Deferred.notify(Object)
), the corresponding pipe will be invoked.DonePipe
andFailPipe
will be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. This method is similar to JQuery's pipe() method, where a newPromise
is returned by the the pipe filter instead of the original. This is useful to handle return values and then rewiring it to different callbacks. Pipes start a newDeferred
object. This allows to chain asynchronous calls. If your pipe does not do any asynchronous work consider usingPromise.filter(DoneFilter, FailFilter, ProgressFilter)
promise.pipe(new DonePipe
() { @Override Deferred pipeDone(Integer result) { // Reject values greater than 100 if (result > 100) { return new DeferredObject ().reject("Failed"); } else { return new DeferredObject ().resolve(result); } } }).done(...) .fail(...); - Specified by:
pipe
in interfacePromise<D,F,P>
- Parameters:
donePipe
- the pipe to invoke when a result is available. Ifnull
, result is piped unchangedfailPipe
- the pipe to invoke when a failure is available. Ifnull
, failure is piped unchangedprogressPipe
- the pipe to execute when progress info is available. Ifnull
, progress is piped unchanged- Returns:
- a new promise for the piped result, failure and progress.
-
pipeAlways
public <D_OUT,F_OUT> Promise<D_OUT,F_OUT,P> pipeAlways(AlwaysPipe<? super D,? super F,? extends D_OUT,? extends F_OUT,? extends P> alwaysPipe)
Description copied from interface:Promise
This method will register a pipe such that when a Deferred object is either resolved (Deferred.resolve(Object)
) or rejected (Deferred.reject(Object)
) the pipe will be invoked.AlwaysPipe
will be triggered at the time the Deferred object is resolved or rejected. If the Deferred object is already resolved or rejected the filter is triggered immediately. This method is similar to JQuery's pipe() method, where a newPromise
is returned by the the pipe filter instead of the original. This is useful to handle return values and then rewiring it to different callbacks. Pipes start a newDeferred
object. This allows to chain asynchronous calls.promise.pipeAlways(new pipe
() { @Override Promise pipeAlways(State state, Integer resolved, Integer rejected) { if (state == State.RESOLVED) { return new DeferredObject ().resolve("Success"); } else { return new DeferredObject ().reject("Failed"); } } }).done(...) .fail(...); - Specified by:
pipeAlways
in interfacePromise<D,F,P>
- Parameters:
alwaysPipe
- the pipe to invoke when a result or failure is available.- Returns:
- a new promise for the piped result or failure.
-
done
public Promise<D,F,P> done(DoneCallback<? super D> callback)
Description copied from interface:Promise
This method will registerDoneCallback
so that when a Deferred object is resolved (Deferred.resolve(Object)
),DoneCallback
will be triggered. If the Deferred object is already resolved then theDoneCallback
is triggered immediately. You can register multipleDoneCallback
by calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.progress(new DoneCallback(){ public void onDone(Object done) { ... } });
-
fail
public Promise<D,F,P> fail(FailCallback<? super F> callback)
Description copied from interface:Promise
This method will registerFailCallback
so that when a Deferred object is rejected (Deferred.reject(Object)
),FailCallback
will be triggered. If the Deferred object is already rejected then theFailCallback
is triggered immediately. You can register multipleFailCallback
by calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.fail(new FailCallback(){ public void onFail(Object rejection) { ... } });
-
always
public Promise<D,F,P> always(AlwaysCallback<? super D,? super F> callback)
Description copied from interface:Promise
This method will registerAlwaysCallback
so that when a Deferred object is either resolved (Deferred.resolve(Object)
) or rejected (Deferred.reject(Object)
),AlwaysCallback
will be triggered. If the Deferred object is already resolved or rejected then theAlwaysCallback
is triggered immediately. You can register multipleAlwaysCallback
by calling the method multiple times. The order of callback trigger is based on the order they have been registered.AlwaysCallback
s are triggered after anyDoneCallback
orFailCallback
respectively.promise.always(new AlwaysCallback(){ public void onAlways(State state, Object result, Object rejection) { if (state == State.RESOLVED) { // do something with result } else { // do something with rejection } } });
- Specified by:
always
in interfacePromise<D,F,P>
- Parameters:
callback
- the callback to be triggered- Returns:
this
for chaining more calls- See Also:
Deferred.resolve(Object)
,Deferred.reject(Object)
-
progress
public Promise<D,F,P> progress(ProgressCallback<? super P> callback)
Description copied from interface:Promise
This method will registerProgressCallback
so that when a Deferred object is notified of progress (Deferred.notify(Object)
),ProgressCallback
will be triggered. You can register multipleProgressCallback
by calling the method multiple times. The order of callback trigger is based on the order they have been registered.promise.progress(new ProgressCallback(){ public void onProgress(Object progress) { // e.g., update progress in the GUI while the background task is still running. } });
-
waitSafely
public void waitSafely() throws java.lang.InterruptedException
Description copied from interface:Promise
This method will wait as long as the State is Pending. This method will return fast when State is not Pending.- Specified by:
waitSafely
in interfacePromise<D,F,P>
- Throws:
java.lang.InterruptedException
- if thread is interrupted while waiting
-
waitSafely
public void waitSafely(long timeout) throws java.lang.InterruptedException
Description copied from interface:Promise
This method will wait when the State is Pending, and return when timeout has reached. This method will return fast when State is not Pending.- Specified by:
waitSafely
in interfacePromise<D,F,P>
- Parameters:
timeout
- the maximum time to wait in milliseconds- Throws:
java.lang.InterruptedException
- if thread is interrupted while waiting
-
-