这是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
30 changes: 25 additions & 5 deletions Bolts/src/main/java/bolts/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,21 @@ public class Task<TResult> {
private boolean cancelled;
private TResult result;
private Exception error;
private List<Continuation<TResult, Void>> continuations;
private List<Continuation<TResult, Void>> continuations = new ArrayList<>();

/* package */ Task() {
continuations = new ArrayList<>();
}

private Task(TResult result) {
trySetResult(result);
}

private Task(boolean cancelled) {
if (cancelled) {
trySetCancelled();
} else {
trySetResult(null);
}
}

/**
Expand Down Expand Up @@ -128,6 +139,12 @@ public void waitForCompletion() throws InterruptedException {
*/
@SuppressWarnings("unchecked")
public static <TResult> Task<TResult> forResult(TResult value) {
if (value == null) {
return (Task<TResult>) TASK_NULL;
}
if (value instanceof Boolean) {
return (Task<TResult>) ((Boolean) value ? TASK_TRUE : TASK_FALSE);
}
bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
tcs.setResult(value);
return tcs.getTask();
Expand All @@ -147,9 +164,7 @@ public static <TResult> Task<TResult> forError(Exception error) {
*/
@SuppressWarnings("unchecked")
public static <TResult> Task<TResult> cancelled() {
bolts.TaskCompletionSource<TResult> tcs = new bolts.TaskCompletionSource<>();
tcs.setCancelled();
return tcs.getTask();
return (Task<TResult>) TASK_CANCELLED;
}

/**
Expand Down Expand Up @@ -924,4 +939,9 @@ public class TaskCompletionSource extends bolts.TaskCompletionSource<TResult> {
/* package */ TaskCompletionSource() {
}
}

private static Task<?> TASK_NULL = new Task<>(null);
private static Task<Boolean> TASK_TRUE = new Task<>((Boolean) true);
private static Task<Boolean> TASK_FALSE = new Task<>((Boolean) false);
private static Task<?> TASK_CANCELLED = new Task(true);
}
13 changes: 13 additions & 0 deletions Bolts/src/test/java/bolts/TaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class TaskTest {
Expand All @@ -49,6 +50,18 @@ private void runTaskTest(Callable<Task<?>> callable) {
}
}

@Test
public void testCache() {
assertSame(Task.forResult(null), Task.forResult(null));
Task<Boolean> trueTask = Task.forResult(true);
assertTrue(trueTask.getResult());
assertSame(trueTask, Task.forResult(true));
Task<Boolean> falseTask = Task.forResult(false);
assertFalse(falseTask.getResult());
assertSame(falseTask, Task.forResult(false));
assertSame(Task.cancelled(), Task.cancelled());
}

@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testPrimitives() {
Expand Down