Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
package bolts;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Represents the result of an asynchronous operation.
*
* @param
* The type of the result of the task.
*/
public class Task {
/**
* An {@link java.util.concurrent.Executor} that executes tasks in parallel.
*/
public static final ExecutorService BACKGROUND_EXECUTOR = BoltsExecutors.background();
/**
* An {@link java.util.concurrent.Executor} that executes tasks in the current thread unless
* the stack runs too deep, at which point it will delegate to {@link Task#BACKGROUND_EXECUTOR} in
* order to trim the stack.
*/
private static final Executor IMMEDIATE_EXECUTOR = BoltsExecutors.immediate();
/**
* An {@link java.util.concurrent.Executor} that executes tasks on the UI thread.
*/
public static final Executor UI_THREAD_EXECUTOR = AndroidExecutors.uiThread();
private final Object lock = new Object();
private boolean complete;
private boolean cancelled;
private TResult result;
private Exception error;
private List> continuations;
private 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.
*/
public static Task.TaskCompletionSource create() {
Task task = new Task();
return task.new TaskCompletionSource();
}
/**
* @return {@code true} if the task completed (has a result, an error, or was cancelled.
* {@code false} otherwise.
*/
public boolean isCompleted() {
synchronized (lock) {
return complete;
}
}
/**
* @return {@code true} if the task was cancelled, {@code false} otherwise.
*/
public boolean isCancelled() {
synchronized (lock) {
return cancelled;
}
}
/**
* @return {@code true} if the task has an error, {@code false} otherwise.
*/
public boolean isFaulted() {
synchronized (lock) {
return error != null;
}
}
/**
* @return The result of the task, if set. {@code null} otherwise.
*/
public TResult getResult() {
synchronized (lock) {
return result;
}
}
/**
* @return The error for the task, if set. {@code null} otherwise.
*/
public Exception getError() {
synchronized (lock) {
return error;
}
}
/**
* Blocks until the task is complete.
*/
public void waitForCompletion() throws InterruptedException {
synchronized (lock) {
if (!isCompleted()) {
lock.wait();
}
}
}
/**
* Creates a completed task with the given value.
*/
public static Task forResult(TResult value) {
Task.TaskCompletionSource tcs = Task.create();
tcs.setResult(value);
return tcs.getTask();
}
/**
* Creates a faulted task with the given error.
*/
public static Task forError(Exception error) {
Task.TaskCompletionSource tcs = Task.create();
tcs.setError(error);
return tcs.getTask();
}
/**
* Creates a cancelled task.
*/
public static Task cancelled() {
Task.TaskCompletionSource tcs = Task.create();
tcs.setCancelled();
return tcs.getTask();
}
/**
* Makes a fluent cast of a Task's result possible, avoiding an extra continuation just to cast
* the type of the result.
*/
public Task cast() {
@SuppressWarnings("unchecked")
Task task = (Task) this;
return task;
}
/**
* Turns a Task into a Task, dropping any result.
*/
public Task makeVoid() {
return this.continueWithTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
if (task.isCancelled()) {
return Task.cancelled();
}
if (task.isFaulted()) {
return Task.forError(task.getError());
}
return Task.forResult(null);
}
});
}
/**
* Invokes the callable on a background thread, returning a Task to represent the operation.
*/
public static Task callInBackground(Callable callable) {
return call(callable, BACKGROUND_EXECUTOR);
}
/**
* Invokes the callable using the given executor, returning a Task to represent the operation.
*/
public static Task call(final Callable callable, Executor executor) {
final Task.TaskCompletionSource tcs = Task.create();
executor.execute(new Runnable() {
@Override
public void run() {
try {
tcs.setResult(callable.call());
} catch (Exception e) {
tcs.setError(e);
}
}
});
return tcs.getTask();
}
/**
* Invokes the callable on the current thread, producing a Task.
*/
public static Task call(final Callable callable) {
return call(callable, IMMEDIATE_EXECUTOR);
}
/**
* Creates a task that completes when all of the provided tasks are complete.
*/
public static Task whenAll(Collection extends Task>> tasks) {
if (tasks.size() == 0) {
return Task.forResult(null);
}
final Task.TaskCompletionSource allFinished = Task.create();
final ArrayList errors = new ArrayList();
final Object errorLock = new Object();
final AtomicInteger count = new AtomicInteger(tasks.size());
final AtomicBoolean isCancelled = new AtomicBoolean(false);
for (Task> task : tasks) {
@SuppressWarnings("unchecked")
Task