这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 62 additions & 99 deletions Bolts/src/main/java/bolts/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ public class Task<TResult> {
private Exception error;
private List<Continuation<TResult, Void>> continuations;

private Task() {
continuations = new ArrayList<Continuation<TResult, Void>>();
/* package */ Task() {
continuations = new ArrayList<>();
}

/**
* Creates a TaskCompletionSource that orchestrates a Task. This allows the creator of a task to
* be solely responsible for its completion.
*
* @return A new TaskCompletionSource.
* @deprecated Please use {@link bolts.TaskCompletionSource()} instead.
*/
public static <TResult> Task<TResult>.TaskCompletionSource create() {
Task<TResult> task = new Task<TResult>();
Task<TResult> task = new Task<>();
return task.new TaskCompletionSource();
}

Expand Down Expand Up @@ -129,8 +126,9 @@ public void waitForCompletion() throws InterruptedException {
/**
* Creates a completed task with the given value.
*/
@SuppressWarnings("unchecked")
public static <TResult> Task<TResult> forResult(TResult value) {
Task<TResult>.TaskCompletionSource tcs = Task.create();
bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
tcs.setResult(value);
return tcs.getTask();
}
Expand All @@ -139,16 +137,17 @@ public static <TResult> Task<TResult> forResult(TResult value) {
* Creates a faulted task with the given error.
*/
public static <TResult> Task<TResult> forError(Exception error) {
Task<TResult>.TaskCompletionSource tcs = Task.create();
bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
tcs.setError(error);
return tcs.getTask();
}

/**
* Creates a cancelled task.
*/
@SuppressWarnings("unchecked")
public static <TResult> Task<TResult> cancelled() {
Task<TResult>.TaskCompletionSource tcs = Task.create();
bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
tcs.setCancelled();
return tcs.getTask();
}
Expand Down Expand Up @@ -184,7 +183,7 @@ public static Task<Void> delay(long delay, CancellationToken cancellationToken)
return Task.forResult(null);
}

final Task<Void>.TaskCompletionSource tcs = Task.create();
final bolts.TaskCompletionSource<Void> tcs = new bolts.TaskCompletionSource<>();
final ScheduledFuture<?> scheduled = executor.schedule(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -265,7 +264,7 @@ public static <TResult> Task<TResult> call(final Callable<TResult> callable, Exe
*/
public static <TResult> Task<TResult> call(final Callable<TResult> callable, Executor executor,
final CancellationToken ct) {
final Task<TResult>.TaskCompletionSource tcs = Task.create();
final bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
executor.execute(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -320,7 +319,7 @@ public static <TResult> Task<Task<TResult>> whenAnyResult(Collection<? extends T
return Task.forResult(null);
}

final Task<Task<TResult>>.TaskCompletionSource firstCompleted = Task.create();
final bolts.TaskCompletionSource<Task<TResult>> firstCompleted = new bolts.TaskCompletionSource<>();
final AtomicBoolean isAnyTaskComplete = new AtomicBoolean(false);

for (Task<TResult> task : tasks) {
Expand Down Expand Up @@ -355,7 +354,7 @@ public static Task<Task<?>> whenAny(Collection<? extends Task<?>> tasks) {
return Task.forResult(null);
}

final Task<Task<?>>.TaskCompletionSource firstCompleted = Task.create();
final bolts.TaskCompletionSource<Task<?>> firstCompleted = new bolts.TaskCompletionSource<>();
final AtomicBoolean isAnyTaskComplete = new AtomicBoolean(false);

for (Task<?> task : tasks) {
Expand Down Expand Up @@ -439,8 +438,8 @@ public static Task<Void> whenAll(Collection<? extends Task<?>> tasks) {
return Task.forResult(null);
}

final Task<Void>.TaskCompletionSource allFinished = Task.create();
final ArrayList<Exception> causes = new ArrayList<Exception>();
final bolts.TaskCompletionSource<Void> allFinished = new bolts.TaskCompletionSource<>();
final ArrayList<Exception> causes = new ArrayList<>();
final Object errorLock = new Object();
final AtomicInteger count = new AtomicInteger(tasks.size());
final AtomicBoolean isCancelled = new AtomicBoolean(false);
Expand Down Expand Up @@ -520,7 +519,7 @@ public Task<Void> continueWhile(final Callable<Boolean> predicate,
final Continuation<Void, Task<Void>> continuation, final Executor executor,
final CancellationToken ct) {
final Capture<Continuation<Void, Task<Void>>> predicateContinuation =
new Capture<Continuation<Void, Task<Void>>>();
new Capture<>();
predicateContinuation.set(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
Expand Down Expand Up @@ -557,7 +556,7 @@ public <TContinuationResult> Task<TContinuationResult> continueWith(
final Continuation<TResult, TContinuationResult> continuation, final Executor executor,
final CancellationToken ct) {
boolean completed;
final Task<TContinuationResult>.TaskCompletionSource tcs = Task.create();
final bolts.TaskCompletionSource<TContinuationResult> tcs = new bolts.TaskCompletionSource<>();
synchronized (lock) {
completed = this.isCompleted();
if (!completed) {
Expand Down Expand Up @@ -611,7 +610,7 @@ public <TContinuationResult> Task<TContinuationResult> continueWithTask(
final Continuation<TResult, Task<TContinuationResult>> continuation, final Executor executor,
final CancellationToken ct) {
boolean completed;
final Task<TContinuationResult>.TaskCompletionSource tcs = Task.create();
final bolts.TaskCompletionSource<TContinuationResult> tcs = new bolts.TaskCompletionSource<>();
synchronized (lock) {
completed = this.isCompleted();
if (!completed) {
Expand Down Expand Up @@ -769,7 +768,7 @@ public <TContinuationResult> Task<TContinuationResult> onSuccessTask(
* scheduled on a different thread).
*/
private static <TContinuationResult, TResult> void completeImmediately(
final Task<TContinuationResult>.TaskCompletionSource tcs,
final bolts.TaskCompletionSource<TContinuationResult> tcs,
final Continuation<TResult, TContinuationResult> continuation, final Task<TResult> task,
Executor executor, final CancellationToken ct) {
executor.execute(new Runnable() {
Expand Down Expand Up @@ -809,7 +808,7 @@ public void run() {
* scheduled on a different thread).
*/
private static <TContinuationResult, TResult> void completeAfterTask(
final Task<TContinuationResult>.TaskCompletionSource tcs,
final bolts.TaskCompletionSource<TContinuationResult> tcs,
final Continuation<TResult, Task<TContinuationResult>> continuation,
final Task<TResult> task, final Executor executor,
final CancellationToken ct) {
Expand Down Expand Up @@ -870,95 +869,59 @@ private void runContinuations() {
}

/**
* Allows safe orchestration of a task's completion, preventing the consumer from prematurely
* completing the task. Essentially, it represents the producer side of a Task<TResult>, providing
* access to the consumer side through the getTask() method while isolating the Task's completion
* mechanisms from the consumer.
* Sets the cancelled flag on the Task if the Task hasn't already been completed.
*/
public class TaskCompletionSource {
private TaskCompletionSource() {
}

/**
* @return the Task associated with this TaskCompletionSource.
*/
public Task<TResult> getTask() {
return Task.this;
}

/**
* Sets the cancelled flag on the Task if the Task hasn't already been completed.
*/
public boolean trySetCancelled() {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
cancelled = true;
lock.notifyAll();
runContinuations();
return true;
}
}

/**
* Sets the result on the Task if the Task hasn't already been completed.
*/
public boolean trySetResult(TResult result) {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
Task.this.result = result;
lock.notifyAll();
runContinuations();
return true;
/* package */ boolean trySetCancelled() {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
cancelled = true;
lock.notifyAll();
runContinuations();
return true;
}
}

/**
* Sets the error on the Task if the Task hasn't already been completed.
*/
public boolean trySetError(Exception error) {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
Task.this.error = error;
lock.notifyAll();
runContinuations();
return true;
/**
* Sets the result on the Task if the Task hasn't already been completed.
*/
/* package */ boolean trySetResult(TResult result) {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
Task.this.result = result;
lock.notifyAll();
runContinuations();
return true;
}
}

/**
* Sets the cancelled flag on the task, throwing if the Task has already been completed.
*/
public void setCancelled() {
if (!trySetCancelled()) {
throw new IllegalStateException("Cannot cancel a completed task.");
/**
* Sets the error on the Task if the Task hasn't already been completed.
*/
/* package */ boolean trySetError(Exception error) {
synchronized (lock) {
if (complete) {
return false;
}
complete = true;
Task.this.error = error;
lock.notifyAll();
runContinuations();
return true;
}
}

/**
* Sets the result of the Task, throwing if the Task has already been completed.
*/
public void setResult(TResult result) {
if (!trySetResult(result)) {
throw new IllegalStateException("Cannot set the result of a completed task.");
}
}
/**
* @deprecated Please use {@link bolts.TaskCompletionSource} instead.
*/
public class TaskCompletionSource extends bolts.TaskCompletionSource<TResult> {

/**
* Sets the error of the Task, throwing if the Task has already been completed.
*/
public void setError(Exception error) {
if (!trySetError(error)) {
throw new IllegalStateException("Cannot set the error on a completed task.");
}
/* package */ TaskCompletionSource() {
}
}
}
75 changes: 75 additions & 0 deletions Bolts/src/main/java/bolts/TaskCompletionSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package bolts;

/**
* Allows safe orchestration of a task's completion, preventing the consumer from prematurely
* completing the task. Essentially, it represents the producer side of a Task<TResult>, providing
* access to the consumer side through the getTask() method while isolating the Task's completion
* mechanisms from the consumer.
*/
public class TaskCompletionSource<TResult> {

private final Task<TResult> task;

/**
* Creates a TaskCompletionSource that orchestrates a Task. This allows the creator of a task to
* be solely responsible for its completion.
*/
public TaskCompletionSource() {
task = new Task<>();
}

/**
* @return the Task associated with this TaskCompletionSource.
*/
public Task<TResult> getTask() {
return task;
}

/**
* Sets the cancelled flag on the Task if the Task hasn't already been completed.
*/
public boolean trySetCancelled() {
return task.trySetCancelled();
}

/**
* Sets the result on the Task if the Task hasn't already been completed.
*/
public boolean trySetResult(TResult result) {
return task.trySetResult(result);
}

/**
* Sets the error on the Task if the Task hasn't already been completed.
*/
public boolean trySetError(Exception error) {
return task.trySetError(error);
}

/**
* Sets the cancelled flag on the task, throwing if the Task has already been completed.
*/
public void setCancelled() {
if (!trySetCancelled()) {
throw new IllegalStateException("Cannot cancel a completed task.");
}
}

/**
* Sets the result of the Task, throwing if the Task has already been completed.
*/
public void setResult(TResult result) {
if (!trySetResult(result)) {
throw new IllegalStateException("Cannot set the result of a completed task.");
}
}

/**
* Sets the error of the Task, throwing if the Task has already been completed.
*/
public void setError(Exception error) {
if (!trySetError(error)) {
throw new IllegalStateException("Cannot set the error on a completed task.");
}
}
}
2 changes: 1 addition & 1 deletion Bolts/src/main/java/bolts/WebViewAppLinkResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public Void call() throws Exception {
@Override
public Task<JSONArray> then(Task<Void> task) throws Exception {
// Load the content in a WebView and use JavaScript to extract the meta tags.
final Task<JSONArray>.TaskCompletionSource tcs = Task.create();
final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>();
final WebView webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.setNetworkAvailable(false);
Expand Down
Loading