45
45
public final class ApiFutures {
46
46
private ApiFutures () {}
47
47
48
- /*
49
- * @deprecated Use {@linkplain #addCallback(ApiFuture, ApiFutureCallback, Executor) the
50
- * overload that requires an executor}. For identical behavior, pass {@link
51
- * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
52
- * another executor would be safer.
48
+ /**
49
+ * Registers a callback to be run when the {@link ApiFuture}'s computation is complete or, if the
50
+ * computation is already complete, immediately.
51
+ *
52
+ * @param future The future attach the callback to
53
+ * @param callback The callback to invoke when future is completed
54
+ * @deprecated Use {@linkplain #addCallback(ApiFuture, ApiFutureCallback, Executor) the overload
55
+ * that requires an executor}. For identical behavior, pass {@link
56
+ * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
57
+ * another executor would be safer.
53
58
*/
54
59
@ Deprecated
55
60
public static <V > void addCallback (
56
61
final ApiFuture <V > future , final ApiFutureCallback <? super V > callback ) {
57
62
addCallback (future , callback , directExecutor ());
58
63
}
59
64
65
+ /**
66
+ * Registers a callback to be run when the {@link ApiFuture}'s computation is complete or, if the
67
+ * computation is already complete, immediately.
68
+ *
69
+ * <p>Note that this method is a delegate of {@link Futures#addCallback(ListenableFuture,
70
+ * FutureCallback, Executor)}.
71
+ *
72
+ * @param future The future attach the callback to
73
+ * @param callback The callback to invoke when future is completed
74
+ * @param executor The executor to run callback when the future completes
75
+ * @see Futures#addCallback(ListenableFuture, FutureCallback, Executor)
76
+ */
60
77
public static <V > void addCallback (
61
78
final ApiFuture <V > future , final ApiFutureCallback <? super V > callback , Executor executor ) {
62
79
Futures .addCallback (
@@ -75,11 +92,20 @@ public void onSuccess(V v) {
75
92
executor );
76
93
}
77
94
78
- /*
79
- * @deprecated Use {@linkplain #catching(ApiFuture, Class, ApiFunction, Executor) the
80
- * overload that requires an executor}. For identical behavior, pass {@link
81
- * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
82
- * another executor would be safer.
95
+ /**
96
+ * Returns an {@link ApiFuture} whose result is taken from the given primary input or, if the
97
+ * primary input fails with the given exceptionType, from the result provided by the callback.
98
+ *
99
+ * @param input The primary input {@code ApiFuture}
100
+ * @param exceptionType The exception type that triggers use of {@code fallback}
101
+ * @param callback The {@link ApiFunction} to be called if input fails with the expected exception
102
+ * type
103
+ * @return A future whose result is taken either from the given {@code input} or by the {@code
104
+ * callback}
105
+ * @deprecated Use {@linkplain #catching(ApiFuture, Class, ApiFunction, Executor) the overload
106
+ * that requires an executor}. For identical behavior, pass {@link
107
+ * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
108
+ * another executor would be safer.
83
109
*/
84
110
@ Deprecated
85
111
public static <V , X extends Throwable > ApiFuture <V > catching (
@@ -89,6 +115,22 @@ public static <V, X extends Throwable> ApiFuture<V> catching(
89
115
return catching (input , exceptionType , callback , directExecutor ());
90
116
}
91
117
118
+ /**
119
+ * Returns an {@link ApiFuture} whose result is taken from the given primary input or, if the
120
+ * primary input fails with the given exceptionType, from the result provided by the callback.
121
+ *
122
+ * <p>Note that this method is a delegate of {@link Futures#catching(ListenableFuture, Class,
123
+ * Function, Executor)}.
124
+ *
125
+ * @param input The primary input {@code ApiFuture}
126
+ * @param exceptionType The exception type that triggers use of {@code fallback}
127
+ * @param callback The {@link ApiFunction} to be called if input fails with the expected exception
128
+ * type
129
+ * @param executor The executor that runs {@code fallback} if {@code input} fails
130
+ * @return A future whose result is taken either from the given {@code input} or by the {@code
131
+ * callback}
132
+ * @see Futures#catching(ListenableFuture, Class, Function, Executor)
133
+ */
92
134
public static <V , X extends Throwable > ApiFuture <V > catching (
93
135
ApiFuture <? extends V > input ,
94
136
Class <X > exceptionType ,
@@ -103,6 +145,22 @@ public static <V, X extends Throwable> ApiFuture<V> catching(
103
145
return new ListenableFutureToApiFuture <V >(catchingFuture );
104
146
}
105
147
148
+ /**
149
+ * Returns a {@link ApiFuture} whose result is taken from the given primary input or, if the
150
+ * primary input fails with the given exceptionType, from the result provided by the callback.
151
+ *
152
+ * <p>Note that this method is a delegate of {@link Futures#catchingAsync(ListenableFuture, Class,
153
+ * AsyncFunction, Executor)}
154
+ *
155
+ * @param input The primary input {@code ApiFuture}
156
+ * @param exceptionType The exception type that triggers use of {@code fallback}.
157
+ * @param callback The {@link ApiAsyncFunction} to be called if {@code input} fails with the
158
+ * expected * exception type.
159
+ * @param executor The executor that runs {@code fallback} if {@code input} fails
160
+ * @return A future whose result is taken either from the given {@code input} or by the {@code
161
+ * callback}
162
+ * @see Futures#catchingAsync(ListenableFuture, Class, AsyncFunction, Executor)
163
+ */
106
164
@ BetaApi
107
165
public static <V , X extends Throwable > ApiFuture <V > catchingAsync (
108
166
ApiFuture <V > input ,
@@ -124,30 +182,78 @@ public ListenableFuture<V> apply(X exception) throws Exception {
124
182
return new ListenableFutureToApiFuture <>(catchingFuture );
125
183
}
126
184
185
+ /**
186
+ * Creates a {@code ApiFuture} which has its value set immediately upon construction.
187
+ *
188
+ * <p>Note that this method is a delegate of {@link Futures#immediateFuture(Object)}.
189
+ *
190
+ * @param value The value set to the {@code ApiFuture} upon construction
191
+ * @return A future that holds {@code value}
192
+ * @see Futures#immediateFuture(Object)
193
+ */
127
194
public static <V > ApiFuture <V > immediateFuture (V value ) {
128
195
return new ListenableFutureToApiFuture <>(Futures .<V >immediateFuture (value ));
129
196
}
130
197
198
+ /**
199
+ * Returns a {@code ApiFuture} which has an exception set immediately upon construction.
200
+ *
201
+ * <p>Note that this method is a delegate of {@link Futures#immediateFailedFuture(Throwable)}.
202
+ *
203
+ * @param throwable The exception set to the {@code ApiFuture} upon construction
204
+ * @return A future that holds an exception
205
+ * @see Futures#immediateFailedFuture(Throwable)
206
+ */
131
207
public static <V > ApiFuture <V > immediateFailedFuture (Throwable throwable ) {
132
208
return new ListenableFutureToApiFuture <V >(Futures .<V >immediateFailedFuture (throwable ));
133
209
}
134
210
211
+ /**
212
+ * Creates a {@code ApiFuture} which is cancelled immediately upon construction, so that {@code
213
+ * isCancelled()} always returns {@code true}.
214
+ *
215
+ * <p>Note that this method is a delegate of {@link Futures#immediateCancelledFuture()}.
216
+ *
217
+ * @return A cancelled future
218
+ * @see Futures#immediateCancelledFuture()
219
+ */
135
220
public static <V > ApiFuture <V > immediateCancelledFuture () {
136
221
return new ListenableFutureToApiFuture <V >(Futures .<V >immediateCancelledFuture ());
137
222
}
138
223
139
- /*
140
- * @deprecated Use {@linkplain #transform(ApiFuture, ApiFunction, Executor) the
141
- * overload that requires an executor}. For identical behavior, pass {@link
142
- * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
143
- * another executor would be safer.
224
+ /**
225
+ * Returns a new {@code ApiFuture} whose result is derived from the result of the given {@code
226
+ * ApiFuture}.
227
+ *
228
+ * @param input The future to transform
229
+ * @param function A Function to transform the results of the provided future to the results of
230
+ * the returned future
231
+ * @return A future that holds result of the transformation
232
+ * @deprecated Use {@linkplain #transform(ApiFuture, ApiFunction, Executor) the overload that
233
+ * requires an executor}. For identical behavior, pass {@link
234
+ * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
235
+ * another executor would be safer.
144
236
*/
145
237
@ Deprecated
146
238
public static <V , X > ApiFuture <X > transform (
147
239
ApiFuture <? extends V > input , final ApiFunction <? super V , ? extends X > function ) {
148
240
return transform (input , function , directExecutor ());
149
241
}
150
242
243
+ /**
244
+ * Returns a new {@code ApiFuture} whose result is derived from the result of the given {@code
245
+ * ApiFuture}.
246
+ *
247
+ * <p>Note that this method is a delegate of {@link Futures#transform(ListenableFuture, Function,
248
+ * Executor)}.
249
+ *
250
+ * @param input The future to transform
251
+ * @param function A Function to transform the results of the provided future to the results of
252
+ * the returned future.
253
+ * @param executor Executor to run the function in.
254
+ * @return A future that holds result of the transformation
255
+ * @see Futures#transform(ListenableFuture, Function, Executor)
256
+ */
151
257
public static <V , X > ApiFuture <X > transform (
152
258
ApiFuture <? extends V > input ,
153
259
final ApiFunction <? super V , ? extends X > function ,
@@ -159,6 +265,18 @@ public static <V, X> ApiFuture<X> transform(
159
265
executor ));
160
266
}
161
267
268
+ /**
269
+ * Creates a new {@code ApiFuture} whose value is a list containing the values of all its input
270
+ * futures, if all succeed.
271
+ *
272
+ * <p>The list of results is in the same order as the input list.
273
+ *
274
+ * <p>Note that this method is a delegate of {@link Futures#allAsList(Iterable)}.
275
+ *
276
+ * @param futures Futures to combine
277
+ * @return A future that provides a list of the results of the component futures
278
+ * @see Futures#allAsList(Iterable)
279
+ */
162
280
public static <V > ApiFuture <List <V >> allAsList (
163
281
Iterable <? extends ApiFuture <? extends V >> futures ) {
164
282
return new ListenableFutureToApiFuture <>(
@@ -172,6 +290,21 @@ public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
172
290
})));
173
291
}
174
292
293
+ /**
294
+ * Creates a new {@code ApiFuture} whose value is a list containing the values of all its
295
+ * successful input futures. The list of results is in the same order as the input list, and if
296
+ * any of the provided futures fails or is canceled, its corresponding position will contain
297
+ * {@code null} (which is indistinguishable from the future having a successful value of {@code
298
+ * null}).
299
+ *
300
+ * <p>The list of results is in the same order as the input list.
301
+ *
302
+ * <p>Note that this method is a delegate of {@link Futures#successfulAsList(Iterable)}.
303
+ *
304
+ * @param futures Futures to combine
305
+ * @return A future that provides a list of the results of the component futures
306
+ * @see Futures#successfulAsList(Iterable)
307
+ */
175
308
@ BetaApi
176
309
public static <V > ApiFuture <List <V >> successfulAsList (
177
310
Iterable <? extends ApiFuture <? extends V >> futures ) {
@@ -186,18 +319,43 @@ public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
186
319
})));
187
320
}
188
321
189
- /*
190
- * @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiFunction, Executor) the
191
- * overload that requires an executor}. For identical behavior, pass {@link
192
- * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
193
- * another executor would be safer.
322
+ /**
323
+ * Returns a new {@code ApiFuture} whose result is asynchronously derived from the result of the
324
+ * given {@code ApiFuture}. If the given {@code Future} fails, the returned {@code ApiFuture}
325
+ * fails with the same exception (and the function is not invoked).
326
+ *
327
+ * @param input The future to transform
328
+ * @param function A function to transform the result of the input future to the result of the
329
+ * output future
330
+ * @return A future that holds result of the function (if the input succeeded) or the original
331
+ * input's failure (if not)
332
+ * @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiAsyncFunction, Executor)}, the
333
+ * overload that requires an executor. For identical behavior, pass {@link
334
+ * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether
335
+ * another executor would be safer.
194
336
*/
195
337
@ Deprecated
196
338
public static <I , O > ApiFuture <O > transformAsync (
197
339
ApiFuture <I > input , final ApiAsyncFunction <I , O > function ) {
198
340
return transformAsync (input , function , directExecutor ());
199
341
}
200
342
343
+ /**
344
+ * Returns a new {@code ApiFuture} whose result is asynchronously derived from the result of the
345
+ * given {@code ApiFuture}. If the given {@code Future} fails, the returned {@code ApiFuture}
346
+ * fails with the same exception (and the function is not invoked).
347
+ *
348
+ * <p>Note that this method is a delegate of {@link Futures#transformAsync(ListenableFuture,
349
+ * AsyncFunction, Executor)}.
350
+ *
351
+ * @param input The future to transform
352
+ * @param function A function to transform the result of the input future to the result of the
353
+ * output future
354
+ * @param executor Executor to run the function in.
355
+ * @return A future that holds result of the function (if the input succeeded) or the original
356
+ * input's failure (if not)
357
+ * @see Futures#transformAsync(ListenableFuture, AsyncFunction, Executor)
358
+ */
201
359
public static <I , O > ApiFuture <O > transformAsync (
202
360
ApiFuture <I > input , final ApiAsyncFunction <I , O > function , Executor executor ) {
203
361
ListenableFuture <I > listenableInput = listenableFutureForApiFuture (input );
0 commit comments