/
CombinedFuture.java
210 lines (181 loc) · 6.81 KB
/
CombinedFuture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (C) 2015 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.common.util.concurrent;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.AggregateFuture.ReleaseResourcesReason.OUTPUT_FUTURE_DONE;
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableCollection;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.WeakOuter;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/** Aggregate future that computes its value by calling a callable. */
@GwtCompatible
@ElementTypesAreNonnullByDefault
final class CombinedFuture<V extends @Nullable Object>
extends AggregateFuture<@Nullable Object, V> {
@CheckForNull @LazyInit private CombinedFutureInterruptibleTask<?> task;
CombinedFuture(
ImmutableCollection<? extends ListenableFuture<?>> futures,
boolean allMustSucceed,
Executor listenerExecutor,
AsyncCallable<V> callable) {
super(futures, allMustSucceed, false);
this.task = new AsyncCallableInterruptibleTask(callable, listenerExecutor);
init();
}
CombinedFuture(
ImmutableCollection<? extends ListenableFuture<?>> futures,
boolean allMustSucceed,
Executor listenerExecutor,
Callable<V> callable) {
super(futures, allMustSucceed, false);
this.task = new CallableInterruptibleTask(callable, listenerExecutor);
init();
}
@Override
void collectOneValue(int index, @CheckForNull Object returnValue) {}
@Override
void handleAllCompleted() {
CombinedFutureInterruptibleTask<?> localTask = task;
if (localTask != null) {
localTask.execute();
}
}
@Override
void releaseResources(ReleaseResourcesReason reason) {
super.releaseResources(reason);
/*
* If the output future is done, then it won't need to interrupt the task later, so it can clear
* its reference to it.
*
* If the output future is *not* done, then the task field will be cleared after the task runs
* or after the output future is done, whichever comes first.
*/
if (reason == OUTPUT_FUTURE_DONE) {
this.task = null;
}
}
@Override
protected void interruptTask() {
CombinedFutureInterruptibleTask<?> localTask = task;
if (localTask != null) {
localTask.interruptTask();
}
}
@WeakOuter
private abstract class CombinedFutureInterruptibleTask<T extends @Nullable Object>
extends InterruptibleTask<T> {
private final Executor listenerExecutor;
CombinedFutureInterruptibleTask(Executor listenerExecutor) {
this.listenerExecutor = checkNotNull(listenerExecutor);
}
@Override
final boolean isDone() {
return CombinedFuture.this.isDone();
}
final void execute() {
try {
listenerExecutor.execute(this);
} catch (RejectedExecutionException e) {
CombinedFuture.this.setException(e);
}
}
@Override
final void afterRanInterruptiblySuccess(@ParametricNullness T result) {
/*
* The future no longer needs to interrupt this task, so it no longer needs a reference to it.
*
* TODO(cpovirk): It might be nice for our InterruptibleTask subclasses to null out their
* `callable` fields automatically. That would make it less important for us to null out the
* reference to `task` here (though it's still nice to do so in case our reference to the
* executor keeps it alive). Ideally, nulling out `callable` would be the responsibility of
* InterruptibleTask itself so that its other subclasses also benefit. (Handling `callable` in
* InterruptibleTask itself might also eliminate some of the existing boilerplate for, e.g.,
* pendingToString().)
*/
CombinedFuture.this.task = null;
setValue(result);
}
@Override
final void afterRanInterruptiblyFailure(Throwable error) {
// See afterRanInterruptiblySuccess.
CombinedFuture.this.task = null;
if (error instanceof ExecutionException) {
/*
* Cast to ExecutionException to satisfy our nullness checker, which (unsoundly but
* *usually* safely) assumes that getCause() returns non-null on an ExecutionException.
*/
CombinedFuture.this.setException(((ExecutionException) error).getCause());
} else if (error instanceof CancellationException) {
cancel(false);
} else {
CombinedFuture.this.setException(error);
}
}
abstract void setValue(@ParametricNullness T value);
}
@WeakOuter
private final class AsyncCallableInterruptibleTask
extends CombinedFutureInterruptibleTask<ListenableFuture<V>> {
private final AsyncCallable<V> callable;
AsyncCallableInterruptibleTask(AsyncCallable<V> callable, Executor listenerExecutor) {
super(listenerExecutor);
this.callable = checkNotNull(callable);
}
@Override
ListenableFuture<V> runInterruptibly() throws Exception {
ListenableFuture<V> result = callable.call();
return checkNotNull(
result,
"AsyncCallable.call returned null instead of a Future. "
+ "Did you mean to return immediateFuture(null)? %s",
callable);
}
@Override
void setValue(ListenableFuture<V> value) {
CombinedFuture.this.setFuture(value);
}
@Override
String toPendingString() {
return callable.toString();
}
}
@WeakOuter
private final class CallableInterruptibleTask extends CombinedFutureInterruptibleTask<V> {
private final Callable<V> callable;
CallableInterruptibleTask(Callable<V> callable, Executor listenerExecutor) {
super(listenerExecutor);
this.callable = checkNotNull(callable);
}
@Override
@ParametricNullness
V runInterruptibly() throws Exception {
return callable.call();
}
@Override
void setValue(@ParametricNullness V value) {
CombinedFuture.this.set(value);
}
@Override
String toPendingString() {
return callable.toString();
}
}
}