Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve generics of promise #158

Merged
merged 3 commits into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void handleMessage(Message msg) {
}
}

protected void triggerDone(DoneCallback<D> callback, D resolved) {
protected void triggerDone(DoneCallback<? super D> callback, D resolved) {
if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI) {
executeInUiThread(MESSAGE_POST_DONE, callback, State.RESOLVED,
resolved, null, null);
Expand All @@ -111,7 +111,7 @@ protected void triggerDone(DoneCallback<D> callback, D resolved) {
}
};

protected void triggerFail(FailCallback<F> callback, F rejected) {
protected void triggerFail(FailCallback<? super F> callback, F rejected) {
if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI) {
executeInUiThread(MESSAGE_POST_FAIL, callback, State.REJECTED,
null, rejected, null);
Expand All @@ -120,7 +120,7 @@ protected void triggerFail(FailCallback<F> callback, F rejected) {
}
};

protected void triggerProgress(ProgressCallback<P> callback, P progress) {
protected void triggerProgress(ProgressCallback<? super P> callback, P progress) {
if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI) {
executeInUiThread(MESSAGE_POST_PROGRESS, callback, State.PENDING,
null, null, progress);
Expand All @@ -129,7 +129,7 @@ protected void triggerProgress(ProgressCallback<P> callback, P progress) {
}
};

protected void triggerAlways(AlwaysCallback<D, F> callback, State state,
protected void triggerAlways(AlwaysCallback<? super D, ? super F> callback, State state,
D resolve, F reject) {
if (determineAndroidExecutionScope(callback) == AndroidExecutionScope.UI) {
executeInUiThread(MESSAGE_POST_ALWAYS, callback, state, resolve,
Expand Down
44 changes: 24 additions & 20 deletions subprojects/jdeferred-core/src/main/java/org/jdeferred/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ enum State {
* @param doneCallback see {@link #done(DoneCallback)}
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> then(DoneCallback<D> doneCallback);
Promise<D, F, P> then(DoneCallback<? super D> doneCallback);

/**
* Equivalent to {@link #done(DoneCallback)}.{@link #fail(FailCallback)}
Expand All @@ -117,7 +117,7 @@ enum State {
* @param failCallback see {@link #fail(FailCallback)}
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> then(DoneCallback<D> doneCallback, FailCallback<F> failCallback);
Promise<D, F, P> then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback);

/**
* Equivalent to {@link #done(DoneCallback)}.{@link #fail(FailCallback)}.{@link #progress(ProgressCallback)}
Expand All @@ -127,8 +127,8 @@ enum State {
* @param progressCallback see {@link #progress(ProgressCallback)}
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> then(DoneCallback<D> doneCallback,
FailCallback<F> failCallback, ProgressCallback<P> progressCallback);
Promise<D, F, P> then(DoneCallback<? super D> doneCallback,
FailCallback<? super F> failCallback, ProgressCallback<? super P> progressCallback);

/**
* Equivalent to {@code then(doneFilter, null, null)}
Expand All @@ -137,7 +137,7 @@ Promise<D, F, P> then(DoneCallback<D> doneCallback,
* @param doneFilter the filter to execute when a result is available
* @return a new promise for the filtered result
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(DoneFilter<D, D_OUT> doneFilter);
<D_OUT> Promise<D_OUT, F, P> then(DoneFilter<? super D, ? extends D_OUT> doneFilter);

/**
* Equivalent to {@code then(doneFilter, failFilter, null)}
Expand All @@ -147,8 +147,9 @@ Promise<D, F, P> then(DoneCallback<D> doneCallback,
* @param failFilter the filter to execute when a failure is available
* @return a new promise for the filtered result and failure.
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DoneFilter<D, D_OUT> doneFilter, FailFilter<F, F_OUT> failFilter);
<D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> then(
DoneFilter<? super D, ? extends D_OUT> doneFilter,
FailFilter<? super F, ? extends F_OUT> failFilter);

/**
* This method will register filters such that when a Deferred object is either
Expand Down Expand Up @@ -196,8 +197,9 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @return a new promise for the filtered result, failure and progress.
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DoneFilter<D, D_OUT> doneFilter, FailFilter<F, F_OUT> failFilter,
ProgressFilter<P, P_OUT> progressFilter);
DoneFilter<? super D, ? extends D_OUT> doneFilter,
FailFilter<? super F, ? extends F_OUT> failFilter,
ProgressFilter<? super P, ? extends P_OUT> progressFilter);

/**
* Equivalent to {#code then(DonePipe, null, null)}
Expand All @@ -206,8 +208,7 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param donePipe the pipe to invoke when a result is available
* @return a new promise for the piped result.
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> donePipe);
<D_OUT> Promise<D_OUT, F, P> then(DonePipe<? super D, ? extends D_OUT, ? extends F, ? extends P> donePipe);

/**
* Equivalent to {@code then(DonePipe, FailPipe, null)}
Expand All @@ -217,8 +218,9 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param failPipe the pipe to invoke when a failure is available
* @return a new promise for the piped result and failure.
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> donePipe, FailPipe<F, D_OUT, F_OUT, P_OUT> failPipe);
<D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> then(
DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P> donePipe,
FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> failPipe);

/**
* This method will register pipes such that when a Deferred object is either
Expand Down Expand Up @@ -264,8 +266,9 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @return a new promise for the piped result, failure and progress.
*/
<D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> donePipe, FailPipe<F, D_OUT, F_OUT, P_OUT> failPipe,
ProgressPipe<P, D_OUT, F_OUT, P_OUT> progressPipe);
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 a pipe such that when a Deferred object is either
Expand Down Expand Up @@ -302,7 +305,8 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param alwaysPipe the pipe to invoke when a result or failure is available.
* @return a new promise for the piped result or failure.
*/
<D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> always(AlwaysPipe<D, F, D_OUT, F_OUT, P> alwaysPipe);
<D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> always(
AlwaysPipe<? super D, ? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> alwaysPipe);

/**
* This method will register {@link DoneCallback} so that when a Deferred object
Expand All @@ -326,7 +330,7 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param callback the callback to be triggered
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> done(DoneCallback<D> callback);
Promise<D, F, P> done(DoneCallback<? super D> callback);

/**
* This method will register {@link FailCallback} so that when a Deferred object
Expand All @@ -350,7 +354,7 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param callback the callback to be triggered
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> fail(FailCallback<F> callback);
Promise<D, F, P> fail(FailCallback<? super F> callback);

/**
* This method will register {@link AlwaysCallback} so that when a Deferred object is either
Expand Down Expand Up @@ -384,7 +388,7 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param callback the callback to be triggered
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> always(AlwaysCallback<D, F> callback);
Promise<D, F, P> always(AlwaysCallback<? super D, ? super F> callback);

/**
* This method will register {@link ProgressCallback} so that when a Deferred object
Expand All @@ -407,7 +411,7 @@ <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
* @param callback the callback to be triggered
* @return {@code this} for chaining more calls
*/
Promise<D, F, P> progress(ProgressCallback<P> callback);
Promise<D, F, P> progress(ProgressCallback<? super P> callback);

/**
* This method will wait as long as the State is Pending. This method will return fast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public abstract class AbstractPromise<D, F, P> implements Promise<D, F, P> {

protected volatile State state = State.PENDING;

protected final List<DoneCallback<D>> doneCallbacks = new CopyOnWriteArrayList<DoneCallback<D>>();
protected final List<FailCallback<F>> failCallbacks = new CopyOnWriteArrayList<FailCallback<F>>();
protected final List<ProgressCallback<P>> progressCallbacks = new CopyOnWriteArrayList<ProgressCallback<P>>();
protected final List<AlwaysCallback<D, F>> alwaysCallbacks = new CopyOnWriteArrayList<AlwaysCallback<D, F>>();
protected final List<DoneCallback<? super D>> doneCallbacks = new CopyOnWriteArrayList<DoneCallback<? super D>>();
protected final List<FailCallback<? super F>> failCallbacks = new CopyOnWriteArrayList<FailCallback<? super F>>();
protected final List<ProgressCallback<? super P>> progressCallbacks = new CopyOnWriteArrayList<ProgressCallback<? super P>>();
protected final List<AlwaysCallback<? super D, ? super F>> alwaysCallbacks = new CopyOnWriteArrayList<AlwaysCallback<? super D, ? super F>>();

protected D resolveResult;
protected F rejectResult;
Expand All @@ -59,7 +59,7 @@ public State state() {
}

@Override
public Promise<D, F, P> done(DoneCallback<D> callback) {
public Promise<D, F, P> done(DoneCallback<? super D> callback) {
synchronized (this) {
if (isResolved()){
triggerDone(callback, resolveResult);
Expand All @@ -71,7 +71,7 @@ public Promise<D, F, P> done(DoneCallback<D> callback) {
}

@Override
public Promise<D, F, P> fail(FailCallback<F> callback) {
public Promise<D, F, P> fail(FailCallback<? super F> callback) {
synchronized (this) {
if(isRejected()){
triggerFail(callback, rejectResult);
Expand All @@ -83,7 +83,7 @@ public Promise<D, F, P> fail(FailCallback<F> callback) {
}

@Override
public Promise<D, F, P> always(AlwaysCallback<D, F> callback) {
public Promise<D, F, P> always(AlwaysCallback<? super D, ? super F> callback) {
synchronized (this) {
if(isPending()){
alwaysCallbacks.add(callback);
Expand All @@ -95,13 +95,13 @@ public Promise<D, F, P> always(AlwaysCallback<D, F> callback) {
}

protected void triggerDone(D resolved) {
for (DoneCallback<D> callback : doneCallbacks) {
for (DoneCallback<? super D> callback : doneCallbacks) {
triggerDone(callback, resolved);
}
doneCallbacks.clear();
}

protected void triggerDone(DoneCallback<D> callback, D resolved) {
protected void triggerDone(DoneCallback<? super D> callback, D resolved) {
try {
callback.onDone(resolved);
} catch (Exception e) {
Expand All @@ -110,13 +110,13 @@ protected void triggerDone(DoneCallback<D> callback, D resolved) {
}

protected void triggerFail(F rejected) {
for (FailCallback<F> callback : failCallbacks) {
for (FailCallback<? super F> callback : failCallbacks) {
triggerFail(callback, rejected);
}
failCallbacks.clear();
}

protected void triggerFail(FailCallback<F> callback, F rejected) {
protected void triggerFail(FailCallback<? super F> callback, F rejected) {
try {
callback.onFail(rejected);
} catch (Exception e) {
Expand All @@ -125,12 +125,12 @@ protected void triggerFail(FailCallback<F> callback, F rejected) {
}

protected void triggerProgress(P progress) {
for (ProgressCallback<P> callback : progressCallbacks) {
for (ProgressCallback<? super P> callback : progressCallbacks) {
triggerProgress(callback, progress);
}
}

protected void triggerProgress(ProgressCallback<P> callback, P progress) {
protected void triggerProgress(ProgressCallback<? super P> callback, P progress) {
try {
callback.onProgress(progress);
} catch (Exception e) {
Expand All @@ -139,7 +139,7 @@ protected void triggerProgress(ProgressCallback<P> callback, P progress) {
}

protected void triggerAlways(State state, D resolve, F reject) {
for (AlwaysCallback<D, F> callback : alwaysCallbacks) {
for (AlwaysCallback<? super D, ? super F> callback : alwaysCallbacks) {
triggerAlways(callback, state, resolve, reject);
}
alwaysCallbacks.clear();
Expand All @@ -149,7 +149,7 @@ protected void triggerAlways(State state, D resolve, F reject) {
}
}

protected void triggerAlways(AlwaysCallback<D, F> callback, State state, D resolve, F reject) {
protected void triggerAlways(AlwaysCallback<? super D, ? super F> callback, State state, D resolve, F reject) {
try {
callback.onAlways(state, resolve, reject);
} catch (Exception e) {
Expand All @@ -158,74 +158,74 @@ protected void triggerAlways(AlwaysCallback<D, F> callback, State state, D resol
}

@Override
public Promise<D, F, P> progress(ProgressCallback<P> callback) {
public Promise<D, F, P> progress(ProgressCallback<? super P> callback) {
progressCallbacks.add(callback);
return this;
}

@Override
public Promise<D, F, P> then(DoneCallback<D> callback) {
public Promise<D, F, P> then(DoneCallback<? super D> callback) {
return done(callback);
}

@Override
public Promise<D, F, P> then(DoneCallback<D> doneCallback, FailCallback<F> failCallback) {
public Promise<D, F, P> then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback) {
done(doneCallback);
fail(failCallback);
return this;
}

@Override
public Promise<D, F, P> then(DoneCallback<D> doneCallback, FailCallback<F> failCallback,
ProgressCallback<P> progressCallback) {
public Promise<D, F, P> then(DoneCallback<? super D> doneCallback, FailCallback<? super F> failCallback,
ProgressCallback<? super P> progressCallback) {
done(doneCallback);
fail(failCallback);
progress(progressCallback);
return this;
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DoneFilter<D, D_OUT> doneFilter) {
return new FilteredPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, null, null);
public <D_OUT> Promise<D_OUT, F, P> then(
DoneFilter<? super D, ? extends D_OUT> doneFilter) {
return new FilteredPromise<D, F, P, D_OUT, F, P>(this, doneFilter, null, null);
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DoneFilter<D, D_OUT> doneFilter, FailFilter<F, F_OUT> failFilter) {
return new FilteredPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, failFilter, null);
public <D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> then(
DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter) {
return new FilteredPromise<D, F, P, D_OUT, F_OUT, P>(this, doneFilter, failFilter, null);
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DoneFilter<D, D_OUT> doneFilter, FailFilter<F, F_OUT> failFilter,
ProgressFilter<P, P_OUT> progressFilter) {
DoneFilter<? super D, ? extends D_OUT> doneFilter, FailFilter<? super F, ? extends F_OUT> failFilter,
ProgressFilter<? super P, ? extends P_OUT> progressFilter) {
return new FilteredPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, failFilter, progressFilter);
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> doneFilter) {
return new PipedPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, null, null);
public <D_OUT> Promise<D_OUT, F, P> then(
DonePipe<? super D, ? extends D_OUT, ? extends F, ? extends P> doneFilter) {
return new PipedPromise<D, F, P, D_OUT, F, P>(this, doneFilter, null, null);
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> doneFilter,
FailPipe<F, D_OUT, F_OUT, P_OUT> failFilter) {
return new PipedPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, failFilter, null);
public <D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> then(
DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P> doneFilter,
FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> failFilter) {
return new PipedPromise<D, F, P, D_OUT, F_OUT, P>(this, doneFilter, failFilter, null);
}

@Override
public <D_OUT, F_OUT, P_OUT> Promise<D_OUT, F_OUT, P_OUT> then(
DonePipe<D, D_OUT, F_OUT, P_OUT> doneFilter,
FailPipe<F, D_OUT, F_OUT, P_OUT> failFilter,
ProgressPipe<P, D_OUT, F_OUT, P_OUT> progressFilter) {
DonePipe<? super D, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> doneFilter,
FailPipe<? super F, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> failFilter,
ProgressPipe<? super P, ? extends D_OUT, ? extends F_OUT, ? extends P_OUT> progressFilter) {
return new PipedPromise<D, F, P, D_OUT, F_OUT, P_OUT>(this, doneFilter, failFilter, progressFilter);
}

@Override
public <D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> always(AlwaysPipe<D, F, D_OUT, F_OUT, P> alwaysFilter) {
public <D_OUT, F_OUT> Promise<D_OUT, F_OUT, P> always(AlwaysPipe<? super D, ? super F, ? extends D_OUT, ? extends F_OUT, ? extends P> alwaysFilter) {
return new PipedPromise<D, F, P, D_OUT, F_OUT, P>(this, alwaysFilter);
}

Expand Down