aboutsummaryrefslogtreecommitdiff
path: root/guava/src/com/google/common/util/concurrent/Futures.java
blob: 6e8d20193eb5ce482ad6fcad5a601fae58b37059 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
/*
 * Copyright (C) 2006 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.base.Preconditions.checkState;
import static com.google.common.util.concurrent.Internal.toNanosSaturated;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.CollectionFuture.ListFuture;
import com.google.common.util.concurrent.ImmediateFuture.ImmediateCancelledFuture;
import com.google.common.util.concurrent.ImmediateFuture.ImmediateFailedFuture;
import com.google.common.util.concurrent.internal.InternalFutureFailureAccess;
import com.google.common.util.concurrent.internal.InternalFutures;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Static utility methods pertaining to the {@link Future} interface.
 *
 * <p>Many of these methods use the {@link ListenableFuture} API; consult the Guava User Guide
 * article on <a href="https://github.com/google/guava/wiki/ListenableFutureExplained">{@code
 * ListenableFuture}</a>.
 *
 * <p>The main purpose of {@code ListenableFuture} is to help you chain together a graph of
 * asynchronous operations. You can chain them together manually with calls to methods like {@link
 * Futures#transform(ListenableFuture, Function, Executor) Futures.transform}, but you will often
 * find it easier to use a framework. Frameworks automate the process, often adding features like
 * monitoring, debugging, and cancellation. Examples of frameworks include:
 *
 * <ul>
 *   <li><a href="https://dagger.dev/producers.html">Dagger Producers</a>
 * </ul>
 *
 * <p>If you do chain your operations manually, you may want to use {@link FluentFuture}.
 *
 * @author Kevin Bourrillion
 * @author Nishant Thakkar
 * @author Sven Mawson
 * @since 1.0
 */
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public final class Futures extends GwtFuturesCatchingSpecialization {

  // A note on memory visibility.
  // Many of the utilities in this class (transform, withFallback, withTimeout, asList, combine)
  // have two requirements that significantly complicate their design.
  // 1. Cancellation should propagate from the returned future to the input future(s).
  // 2. The returned futures shouldn't unnecessarily 'pin' their inputs after completion.
  //
  // A consequence of these requirements is that the delegate futures cannot be stored in
  // final fields.
  //
  // For simplicity the rest of this description will discuss Futures.catching since it is the
  // simplest instance, though very similar descriptions apply to many other classes in this file.
  //
  // In the constructor of AbstractCatchingFuture, the delegate future is assigned to a field
  // 'inputFuture'. That field is non-final and non-volatile. There are 2 places where the
  // 'inputFuture' field is read and where we will have to consider visibility of the write
  // operation in the constructor.
  //
  // 1. In the listener that performs the callback. In this case it is fine since inputFuture is
  //    assigned prior to calling addListener, and addListener happens-before any invocation of the
  //    listener. Notably, this means that 'volatile' is unnecessary to make 'inputFuture' visible
  //    to the listener.
  //
  // 2. In done() where we may propagate cancellation to the input. In this case it is _not_ fine.
  //    There is currently nothing that enforces that the write to inputFuture in the constructor is
  //    visible to done(). This is because there is no happens before edge between the write and a
  //    (hypothetical) unsafe read by our caller. Note: adding 'volatile' does not fix this issue,
  //    it would just add an edge such that if done() observed non-null, then it would also
  //    definitely observe all earlier writes, but we still have no guarantee that done() would see
  //    the initial write (just stronger guarantees if it does).
  //
  // See: http://cs.oswego.edu/pipermail/concurrency-interest/2015-January/013800.html
  // For a (long) discussion about this specific issue and the general futility of life.
  //
  // For the time being we are OK with the problem discussed above since it requires a caller to
  // introduce a very specific kind of data-race. And given the other operations performed by these
  // methods that involve volatile read/write operations, in practice there is no issue. Also, the
  // way in such a visibility issue would surface is most likely as a failure of cancel() to
  // propagate to the input. Cancellation propagation is fundamentally racy so this is fine.
  //
  // Future versions of the JMM may revise safe construction semantics in such a way that we can
  // safely publish these objects and we won't need this whole discussion.
  // TODO(user,lukes): consider adding volatile to all these fields since in current known JVMs
  // that should resolve the issue. This comes at the cost of adding more write barriers to the
  // implementations.

  private Futures() {}

  /**
   * Creates a {@code ListenableFuture} which has its value set immediately upon construction. The
   * getters just return the value. This {@code Future} can't be canceled or timed out and its
   * {@code isDone()} method always returns {@code true}.
   */
  public static <V extends @Nullable Object> ListenableFuture<V> immediateFuture(
      @ParametricNullness V value) {
    if (value == null) {
      // This cast is safe because null is assignable to V for all V (i.e. it is bivariant)
      @SuppressWarnings("unchecked")
      ListenableFuture<V> typedNull = (ListenableFuture<V>) ImmediateFuture.NULL;
      return typedNull;
    }
    return new ImmediateFuture<>(value);
  }

  /**
   * Returns a successful {@code ListenableFuture<Void>}. This method is equivalent to {@code
   * immediateFuture(null)} except that it is restricted to produce futures of type {@code Void}.
   *
   * @since 29.0
   */
  @SuppressWarnings("unchecked")
  public static ListenableFuture<@Nullable Void> immediateVoidFuture() {
    return (ListenableFuture<@Nullable Void>) ImmediateFuture.NULL;
  }

  /**
   * Returns a {@code ListenableFuture} which has an exception set immediately upon construction.
   *
   * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()} method always
   * returns {@code true}. Calling {@code get()} will immediately throw the provided {@code
   * Throwable} wrapped in an {@code ExecutionException}.
   */
  public static <V extends @Nullable Object> ListenableFuture<V> immediateFailedFuture(
      Throwable throwable) {
    checkNotNull(throwable);
    return new ImmediateFailedFuture<V>(throwable);
  }

  /**
   * Creates a {@code ListenableFuture} which is cancelled immediately upon construction, so that
   * {@code isCancelled()} always returns {@code true}.
   *
   * @since 14.0
   */
  public static <V extends @Nullable Object> ListenableFuture<V> immediateCancelledFuture() {
    ListenableFuture<Object> instance = ImmediateCancelledFuture.INSTANCE;
    if (instance != null) {
      return (ListenableFuture<V>) instance;
    }
    return new ImmediateCancelledFuture<>();
  }

  /**
   * Executes {@code callable} on the specified {@code executor}, returning a {@code Future}.
   *
   * @throws RejectedExecutionException if the task cannot be scheduled for execution
   * @since 28.2
   */
  public static <O extends @Nullable Object> ListenableFuture<O> submit(
      Callable<O> callable, Executor executor) {
    TrustedListenableFutureTask<O> task = TrustedListenableFutureTask.create(callable);
    executor.execute(task);
    return task;
  }

  /**
   * Executes {@code runnable} on the specified {@code executor}, returning a {@code Future} that
   * will complete after execution.
   *
   * @throws RejectedExecutionException if the task cannot be scheduled for execution
   * @since 28.2
   */
  public static ListenableFuture<@Nullable Void> submit(Runnable runnable, Executor executor) {
    TrustedListenableFutureTask<@Nullable Void> task =
        TrustedListenableFutureTask.create(runnable, null);
    executor.execute(task);
    return task;
  }

  /**
   * Executes {@code callable} on the specified {@code executor}, returning a {@code Future}.
   *
   * @throws RejectedExecutionException if the task cannot be scheduled for execution
   * @since 23.0
   */
  public static <O extends @Nullable Object> ListenableFuture<O> submitAsync(
      AsyncCallable<O> callable, Executor executor) {
    TrustedListenableFutureTask<O> task = TrustedListenableFutureTask.create(callable);
    executor.execute(task);
    return task;
  }

  /**
   * Schedules {@code callable} on the specified {@code executor}, returning a {@code Future}.
   *
   * @throws RejectedExecutionException if the task cannot be scheduled for execution
   * @since 28.0
   */
  @J2ktIncompatible
  @GwtIncompatible // java.util.concurrent.ScheduledExecutorService
  // TODO(cpovirk): Return ListenableScheduledFuture?
  public static <O extends @Nullable Object> ListenableFuture<O> scheduleAsync(
      AsyncCallable<O> callable, Duration delay, ScheduledExecutorService executorService) {
    return scheduleAsync(callable, toNanosSaturated(delay), TimeUnit.NANOSECONDS, executorService);
  }

  /**
   * Schedules {@code callable} on the specified {@code executor}, returning a {@code Future}.
   *
   * @throws RejectedExecutionException if the task cannot be scheduled for execution
   * @since 23.0
   */
  @J2ktIncompatible
  @GwtIncompatible // java.util.concurrent.ScheduledExecutorService
  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
  // TODO(cpovirk): Return ListenableScheduledFuture?
  public static <O extends @Nullable Object> ListenableFuture<O> scheduleAsync(
      AsyncCallable<O> callable,
      long delay,
      TimeUnit timeUnit,
      ScheduledExecutorService executorService) {
    TrustedListenableFutureTask<O> task = TrustedListenableFutureTask.create(callable);
    Future<?> scheduled = executorService.schedule(task, delay, timeUnit);
    /*
     * Even when the user interrupts the task, we pass `false` to `cancel` so that we don't
     * interrupt a second time after the interruption performed by TrustedListenableFutureTask.
     */
    task.addListener(() -> scheduled.cancel(false), directExecutor());
    return task;
  }

  /**
   * Returns a {@code Future} whose result is taken from the given primary {@code input} or, if the
   * primary input fails with the given {@code exceptionType}, from the result provided by the
   * {@code fallback}. {@link Function#apply} is not invoked until the primary input has failed, so
   * if the primary input succeeds, it is never invoked. If, during the invocation of {@code
   * fallback}, an exception is thrown, this exception is used as the result of the output {@code
   * Future}.
   *
   * <p>Usage example:
   *
   * <pre>{@code
   * ListenableFuture<Integer> fetchCounterFuture = ...;
   *
   * // Falling back to a zero counter in case an exception happens when
   * // processing the RPC to fetch counters.
   * ListenableFuture<Integer> faultTolerantFuture = Futures.catching(
   *     fetchCounterFuture, FetchException.class, x -> 0, directExecutor());
   * }</pre>
   *
   * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See
   * the warnings the {@link MoreExecutors#directExecutor} documentation.
   *
   * @param input the primary input {@code Future}
   * @param exceptionType the exception type that triggers use of {@code fallback}. The exception
   *     type is matched against the input's exception. "The input's exception" means the cause of
   *     the {@link ExecutionException} thrown by {@code input.get()} or, if {@code get()} throws a
   *     different kind of exception, that exception itself. To avoid hiding bugs and other
   *     unrecoverable errors, callers should prefer more specific types, avoiding {@code
   *     Throwable.class} in particular.
   * @param fallback the {@link Function} to be called if {@code input} fails with the expected
   *     exception type. The function's argument is the input's exception. "The input's exception"
   *     means the cause of the {@link ExecutionException} thrown by {@code input.get()} or, if
   *     {@code get()} throws a different kind of exception, that exception itself.
   * @param executor the executor that runs {@code fallback} if {@code input} fails
   * @since 19.0
   */
  @J2ktIncompatible
  @Partially.GwtIncompatible("AVAILABLE but requires exceptionType to be Throwable.class")
  public static <V extends @Nullable Object, X extends Throwable> ListenableFuture<V> catching(
      ListenableFuture<? extends V> input,
      Class<X> exceptionType,
      Function<? super X, ? extends V> fallback,
      Executor executor) {
    return AbstractCatchingFuture.create(input, exceptionType, fallback, executor);
  }

  /**
   * Returns a {@code Future} whose result is taken from the given primary {@code input} or, if the
   * primary input fails with the given {@code exceptionType}, from the result provided by the
   * {@code fallback}. {@link AsyncFunction#apply} is not invoked until the primary input has
   * failed, so if the primary input succeeds, it is never invoked. If, during the invocation of
   * {@code fallback}, an exception is thrown, this exception is used as the result of the output
   * {@code Future}.
   *
   * <p>Usage examples:
   *
   * <pre>{@code
   * ListenableFuture<Integer> fetchCounterFuture = ...;
   *
   * // Falling back to a zero counter in case an exception happens when
   * // processing the RPC to fetch counters.
   * ListenableFuture<Integer> faultTolerantFuture = Futures.catchingAsync(
   *     fetchCounterFuture, FetchException.class, x -> immediateFuture(0), directExecutor());
   * }</pre>
   *
   * <p>The fallback can also choose to propagate the original exception when desired:
   *
   * <pre>{@code
   * ListenableFuture<Integer> fetchCounterFuture = ...;
   *
   * // Falling back to a zero counter only in case the exception was a
   * // TimeoutException.
   * ListenableFuture<Integer> faultTolerantFuture = Futures.catchingAsync(
   *     fetchCounterFuture,
   *     FetchException.class,
   *     e -> {
   *       if (omitDataOnFetchFailure) {
   *         return immediateFuture(0);
   *       }
   *       throw e;
   *     },
   *     directExecutor());
   * }</pre>
   *
   * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See
   * the warnings the {@link MoreExecutors#directExecutor} documentation.
   *
   * @param input the primary input {@code Future}
   * @param exceptionType the exception type that triggers use of {@code fallback}. The exception
   *     type is matched against the input's exception. "The input's exception" means the cause of
   *     the {@link ExecutionException} thrown by {@code input.get()} or, if {@code get()} throws a
   *     different kind of exception, that exception itself. To avoid hiding bugs and other
   *     unrecoverable errors, callers should prefer more specific types, avoiding {@code
   *     Throwable.class} in particular.
   * @param fallback the {@link AsyncFunction} to be called if {@code input} fails with the expected
   *     exception type. The function's argument is the input's exception. "The input's exception"
   *     means the cause of the {@link ExecutionException} thrown by {@code input.get()} or, if
   *     {@code get()} throws a different kind of exception, that exception itself.
   * @param executor the executor that runs {@code fallback} if {@code input} fails
   * @since 19.0 (similar functionality in 14.0 as {@code withFallback})
   */
  @J2ktIncompatible
  @Partially.GwtIncompatible("AVAILABLE but requires exceptionType to be Throwable.class")
  public static <V extends @Nullable Object, X extends Throwable> ListenableFuture<V> catchingAsync(
      ListenableFuture<? extends V> input,
      Class<X> exceptionType,
      AsyncFunction<? super X, ? extends V> fallback,
      Executor executor) {
    return AbstractCatchingFuture.create(input, exceptionType, fallback, executor);
  }

  /**
   * Returns a future that delegates to another but will finish early (via a {@link
   * TimeoutException} wrapped in an {@link ExecutionException}) if the specified duration expires.
   *
   * <p>The delegate future is interrupted and cancelled if it times out.
   *
   * @param delegate The future to delegate to.
   * @param time when to time out the future
   * @param scheduledExecutor The executor service to enforce the timeout.
   * @since 28.0
   */
  @J2ktIncompatible
  @GwtIncompatible // java.util.concurrent.ScheduledExecutorService
  public static <V extends @Nullable Object> ListenableFuture<V> withTimeout(
      ListenableFuture<V> delegate, Duration time, ScheduledExecutorService scheduledExecutor) {
    return withTimeout(delegate, toNanosSaturated(time), TimeUnit.NANOSECONDS, scheduledExecutor);
  }

  /**
   * Returns a future that delegates to another but will finish early (via a {@link
   * TimeoutException} wrapped in an {@link ExecutionException}) if the specified duration expires.
   *
   * <p>The delegate future is interrupted and cancelled if it times out.
   *
   * @param delegate The future to delegate to.
   * @param time when to time out the future
   * @param unit the time unit of the time parameter
   * @param scheduledExecutor The executor service to enforce the timeout.
   * @since 19.0
   */
  @J2ktIncompatible
  @GwtIncompatible // java.util.concurrent.ScheduledExecutorService
  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
  public static <V extends @Nullable Object> ListenableFuture<V> withTimeout(
      ListenableFuture<V> delegate,
      long time,
      TimeUnit unit,
      ScheduledExecutorService scheduledExecutor) {
    if (delegate.isDone()) {
      return delegate;
    }
    return TimeoutFuture.create(delegate, time, unit, scheduledExecutor);
  }

  /**
   * Returns a new {@code Future} whose result is asynchronously derived from the result of the
   * given {@code Future}. If the given {@code Future} fails, the returned {@code Future} fails with
   * the same exception (and the function is not invoked).
   *
   * <p>More precisely, the returned {@code Future} takes its result from a {@code Future} produced
   * by applying the given {@code AsyncFunction} to the result of the original {@code Future}.
   * Example usage:
   *
   * <pre>{@code
   * ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
   * ListenableFuture<QueryResult> queryFuture =
   *     transformAsync(rowKeyFuture, dataService::readFuture, executor);
   * }</pre>
   *
   * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See
   * the warnings the {@link MoreExecutors#directExecutor} documentation.
   *
   * <p>The returned {@code Future} attempts to keep its cancellation state in sync with that of the
   * input future and that of the future returned by the chain function. That is, if the returned
   * {@code Future} is cancelled, it will attempt to cancel the other two, and if either of the
   * other two is cancelled, the returned {@code Future} will receive a callback in which it will
   * attempt to cancel itself.
   *
   * @param input The future to transform
   * @param function A function to transform the result of the input future to the result of the
   *     output future
   * @param executor Executor to run the function in.
   * @return A future that holds result of the function (if the input succeeded) or the original
   *     input's failure (if not)
   * @since 19.0 (in 11.0 as {@code transform})
   */
  public static <I extends @Nullable Object, O extends @Nullable Object>
      ListenableFuture<O> transformAsync(
          ListenableFuture<I> input,
          AsyncFunction<? super I, ? extends O> function,
          Executor executor) {
    return AbstractTransformFuture.create(input, function, executor);
  }

  /**
   * Returns a new {@code Future} whose result is derived from the result of the given {@code
   * Future}. If {@code input} fails, the returned {@code Future} fails with the same exception (and
   * the function is not invoked). Example usage:
   *
   * <pre>{@code
   * ListenableFuture<QueryResult> queryFuture = ...;
   * ListenableFuture<List<Row>> rowsFuture =
   *     transform(queryFuture, QueryResult::getRows, executor);
   * }</pre>
   *
   * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See
   * the warnings the {@link MoreExecutors#directExecutor} documentation.
   *
   * <p>The returned {@code Future} attempts to keep its cancellation state in sync with that of the
   * input future. That is, if the returned {@code Future} is cancelled, it will attempt to cancel
   * the input, and if the input is cancelled, the returned {@code Future} will receive a callback
   * in which it will attempt to cancel itself.
   *
   * <p>An example use of this method is to convert a serializable object returned from an RPC into
   * a POJO.
   *
   * @param input The future to transform
   * @param function A Function to transform the results of the provided future to the results of
   *     the returned future.
   * @param executor Executor to run the function in.
   * @return A future that holds result of the transformation.
   * @since 9.0 (in 2.0 as {@code compose})
   */
  public static <I extends @Nullable Object, O extends @Nullable Object>
      ListenableFuture<O> transform(
          ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) {
    return AbstractTransformFuture.create(input, function, executor);
  }

  /**
   * Like {@link #transform(ListenableFuture, Function, Executor)} except that the transformation
   * {@code function} is invoked on each call to {@link Future#get() get()} on the returned future.
   *
   * <p>The returned {@code Future} reflects the input's cancellation state directly, and any
   * attempt to cancel the returned Future is likewise passed through to the input Future.
   *
   * <p>Note that calls to {@linkplain Future#get(long, TimeUnit) timed get} only apply the timeout
   * to the execution of the underlying {@code Future}, <em>not</em> to the execution of the
   * transformation function.
   *
   * <p>The primary audience of this method is callers of {@code transform} who don't have a {@code
   * ListenableFuture} available and do not mind repeated, lazy function evaluation.
   *
   * @param input The future to transform
   * @param function A Function to transform the results of the provided future to the results of
   *     the returned future.
   * @return A future that returns the result of the transformation.
   * @since 10.0
   */
  @J2ktIncompatible
  @GwtIncompatible // TODO
  public static <I extends @Nullable Object, O extends @Nullable Object> Future<O> lazyTransform(
      final Future<I> input, final Function<? super I, ? extends O> function) {
    checkNotNull(input);
    checkNotNull(function);
    return new Future<O>() {

      @Override
      public boolean cancel(boolean mayInterruptIfRunning) {
        return input.cancel(mayInterruptIfRunning);
      }

      @Override
      public boolean isCancelled() {
        return input.isCancelled();
      }

      @Override
      public boolean isDone() {
        return input.isDone();
      }

      @Override
      public O get() throws InterruptedException, ExecutionException {
        return applyTransformation(input.get());
      }

      @Override
      public O get(long timeout, TimeUnit unit)
          throws InterruptedException, ExecutionException, TimeoutException {
        return applyTransformation(input.get(timeout, unit));
      }

      private O applyTransformation(I input) throws ExecutionException {
        try {
          return function.apply(input);
        } catch (Throwable t) {
          // Any Exception is either a RuntimeException or sneaky checked exception.
          throw new ExecutionException(t);
        }
      }
    };
  }

  /**
   * Creates a new {@code ListenableFuture} whose value is a list containing the values of all its
   * input futures, if all succeed.
   *
   * <p>The list of results is in the same order as the input list.
   *
   * <p>This differs from {@link #successfulAsList(ListenableFuture[])} in that it will return a
   * failed future if any of the items fails.
   *
   * <p>Canceling this future will attempt to cancel all the component futures, and if any of the
   * provided futures fails or is canceled, this one is, too.
   *
   * @param futures futures to combine
   * @return a future that provides a list of the results of the component futures
   * @since 10.0
   */
  @SafeVarargs
  public static <V extends @Nullable Object> ListenableFuture<List<V>> allAsList(
      ListenableFuture<? extends V>... futures) {
    ListenableFuture<List<@Nullable V>> nullable =
        new ListFuture<V>(ImmutableList.copyOf(futures), true);
    // allAsList ensures that it fills the output list with V instances.
    @SuppressWarnings("nullness")
    ListenableFuture<List<V>> nonNull = nullable;
    return nonNull;
  }

  /**
   * Creates a new {@code ListenableFuture} whose value is a list containing the values of all its
   * input futures, if all succeed.
   *
   * <p>The list of results is in the same order as the input list.
   *
   * <p>This differs from {@link #successfulAsList(Iterable)} in that it will return a failed future
   * if any of the items fails.
   *
   * <p>Canceling this future will attempt to cancel all the component futures, and if any of the
   * provided futures fails or is canceled, this one is, too.
   *
   * @param futures futures to combine
   * @return a future that provides a list of the results of the component futures
   * @since 10.0
   */
  public static <V extends @Nullable Object> ListenableFuture<List<V>> allAsList(
      Iterable<? extends ListenableFuture<? extends V>> futures) {
    ListenableFuture<List<@Nullable V>> nullable =
        new ListFuture<V>(ImmutableList.copyOf(futures), true);
    // allAsList ensures that it fills the output list with V instances.
    @SuppressWarnings("nullness")
    ListenableFuture<List<V>> nonNull = nullable;
    return nonNull;
  }

  /**
   * Creates a {@link FutureCombiner} that processes the completed futures whether or not they're
   * successful.
   *
   * <p>Any failures from the input futures will not be propagated to the returned future.
   *
   * @since 20.0
   */
  @SafeVarargs
  public static <V extends @Nullable Object> FutureCombiner<V> whenAllComplete(
      ListenableFuture<? extends V>... futures) {
    return new FutureCombiner<V>(false, ImmutableList.copyOf(futures));
  }

  /**
   * Creates a {@link FutureCombiner} that processes the completed futures whether or not they're
   * successful.
   *
   * <p>Any failures from the input futures will not be propagated to the returned future.
   *
   * @since 20.0
   */
  public static <V extends @Nullable Object> FutureCombiner<V> whenAllComplete(
      Iterable<? extends ListenableFuture<? extends V>> futures) {
    return new FutureCombiner<V>(false, ImmutableList.copyOf(futures));
  }

  /**
   * Creates a {@link FutureCombiner} requiring that all passed in futures are successful.
   *
   * <p>If any input fails, the returned future fails immediately.
   *
   * @since 20.0
   */
  @SafeVarargs
  public static <V extends @Nullable Object> FutureCombiner<V> whenAllSucceed(
      ListenableFuture<? extends V>... futures) {
    return new FutureCombiner<V>(true, ImmutableList.copyOf(futures));
  }

  /**
   * Creates a {@link FutureCombiner} requiring that all passed in futures are successful.
   *
   * <p>If any input fails, the returned future fails immediately.
   *
   * @since 20.0
   */
  public static <V extends @Nullable Object> FutureCombiner<V> whenAllSucceed(
      Iterable<? extends ListenableFuture<? extends V>> futures) {
    return new FutureCombiner<V>(true, ImmutableList.copyOf(futures));
  }

  /**
   * A helper to create a new {@code ListenableFuture} whose result is generated from a combination
   * of input futures.
   *
   * <p>See {@link #whenAllComplete} and {@link #whenAllSucceed} for how to instantiate this class.
   *
   * <p>Example:
   *
   * <pre>{@code
   * final ListenableFuture<Instant> loginDateFuture =
   *     loginService.findLastLoginDate(username);
   * final ListenableFuture<List<String>> recentCommandsFuture =
   *     recentCommandsService.findRecentCommands(username);
   * ListenableFuture<UsageHistory> usageFuture =
   *     Futures.whenAllSucceed(loginDateFuture, recentCommandsFuture)
   *         .call(
   *             () ->
   *                 new UsageHistory(
   *                     username,
   *                     Futures.getDone(loginDateFuture),
   *                     Futures.getDone(recentCommandsFuture)),
   *             executor);
   * }</pre>
   *
   * @since 20.0
   */
  @GwtCompatible
  public static final class FutureCombiner<V extends @Nullable Object> {
    private final boolean allMustSucceed;
    private final ImmutableList<ListenableFuture<? extends V>> futures;

    private FutureCombiner(
        boolean allMustSucceed, ImmutableList<ListenableFuture<? extends V>> futures) {
      this.allMustSucceed = allMustSucceed;
      this.futures = futures;
    }

    /**
     * Creates the {@link ListenableFuture} which will return the result of calling {@link
     * AsyncCallable#call} in {@code combiner} when all futures complete, using the specified {@code
     * executor}.
     *
     * <p>If the combiner throws a {@code CancellationException}, the returned future will be
     * cancelled.
     *
     * <p>If the combiner throws an {@code ExecutionException}, the cause of the thrown {@code
     * ExecutionException} will be extracted and returned as the cause of the new {@code
     * ExecutionException} that gets thrown by the returned combined future.
     *
     * <p>Canceling this future will attempt to cancel all the component futures.
     *
     * @return a future whose result is based on {@code combiner} (or based on the input futures
     *     passed to {@code whenAllSucceed}, if that is the method you used to create this {@code
     *     FutureCombiner}). Even if you don't care about the value of the future, you should
     *     typically check whether it failed: See <a
     *     href="https://errorprone.info/bugpattern/FutureReturnValueIgnored">https://errorprone.info/bugpattern/FutureReturnValueIgnored</a>.
     */
    public <C extends @Nullable Object> ListenableFuture<C> callAsync(
        AsyncCallable<C> combiner, Executor executor) {
      return new CombinedFuture<C>(futures, allMustSucceed, executor, combiner);
    }

    /**
     * Creates the {@link ListenableFuture} which will return the result of calling {@link
     * Callable#call} in {@code combiner} when all futures complete, using the specified {@code
     * executor}.
     *
     * <p>If the combiner throws a {@code CancellationException}, the returned future will be
     * cancelled.
     *
     * <p>If the combiner throws an {@code ExecutionException}, the cause of the thrown {@code
     * ExecutionException} will be extracted and returned as the cause of the new {@code
     * ExecutionException} that gets thrown by the returned combined future.
     *
     * <p>Canceling this future will attempt to cancel all the component futures.
     *
     * @return a future whose result is based on {@code combiner} (or based on the input futures
     *     passed to {@code whenAllSucceed}, if that is the method you used to create this {@code
     *     FutureCombiner}). Even if you don't care about the value of the future, you should
     *     typically check whether it failed: See <a
     *     href="https://errorprone.info/bugpattern/FutureReturnValueIgnored">https://errorprone.info/bugpattern/FutureReturnValueIgnored</a>.
     */
    public <C extends @Nullable Object> ListenableFuture<C> call(
        Callable<C> combiner, Executor executor) {
      return new CombinedFuture<C>(futures, allMustSucceed, executor, combiner);
    }

    /**
     * Creates the {@link ListenableFuture} which will return the result of running {@code combiner}
     * when all Futures complete. {@code combiner} will run using {@code executor}.
     *
     * <p>If the combiner throws a {@code CancellationException}, the returned future will be
     * cancelled.
     *
     * <p>Canceling this Future will attempt to cancel all the component futures.
     *
     * @since 23.6
     * @return a future whose result is based on {@code combiner} (or based on the input futures
     *     passed to {@code whenAllSucceed}, if that is the method you used to create this {@code
     *     FutureCombiner}). Even though the future never produces a value other than {@code null},
     *     you should typically check whether it failed: See <a
     *     href="https://errorprone.info/bugpattern/FutureReturnValueIgnored">https://errorprone.info/bugpattern/FutureReturnValueIgnored</a>.
     */
    public ListenableFuture<?> run(final Runnable combiner, Executor executor) {
      return call(
          new Callable<@Nullable Void>() {
            @Override
            @CheckForNull
            public Void call() throws Exception {
              combiner.run();
              return null;
            }
          },
          executor);
    }
  }

  /**
   * Returns a {@code ListenableFuture} whose result is set from the supplied future when it
   * completes. Cancelling the supplied future will also cancel the returned future, but cancelling
   * the returned future will have no effect on the supplied future.
   *
   * @since 15.0
   */
  public static <V extends @Nullable Object> ListenableFuture<V> nonCancellationPropagating(
      ListenableFuture<V> future) {
    if (future.isDone()) {
      return future;
    }
    NonCancellationPropagatingFuture<V> output = new NonCancellationPropagatingFuture<>(future);
    future.addListener(output, directExecutor());
    return output;
  }

  /** A wrapped future that does not propagate cancellation to its delegate. */
  private static final class NonCancellationPropagatingFuture<V extends @Nullable Object>
      extends AbstractFuture.TrustedFuture<V> implements Runnable {
    @CheckForNull private ListenableFuture<V> delegate;

    NonCancellationPropagatingFuture(final ListenableFuture<V> delegate) {
      this.delegate = delegate;
    }

    @Override
    public void run() {
      // This prevents cancellation from propagating because we don't call setFuture(delegate) until
      // delegate is already done, so calling cancel() on this future won't affect it.
      ListenableFuture<V> localDelegate = delegate;
      if (localDelegate != null) {
        setFuture(localDelegate);
      }
    }

    @Override
    @CheckForNull
    protected String pendingToString() {
      ListenableFuture<V> localDelegate = delegate;
      if (localDelegate != null) {
        return "delegate=[" + localDelegate + "]";
      }
      return null;
    }

    @Override
    protected void afterDone() {
      delegate = null;
    }
  }

  /**
   * Creates a new {@code ListenableFuture} whose value is a list containing the values of all its
   * successful input futures. The list of results is in the same order as the input list, and if
   * any of the provided futures fails or is canceled, its corresponding position will contain
   * {@code null} (which is indistinguishable from the future having a successful value of {@code
   * null}).
   *
   * <p>The list of results is in the same order as the input list.
   *
   * <p>This differs from {@link #allAsList(ListenableFuture[])} in that it's tolerant of failed
   * futures for any of the items, representing them as {@code null} in the result list.
   *
   * <p>Canceling this future will attempt to cancel all the component futures.
   *
   * @param futures futures to combine
   * @return a future that provides a list of the results of the component futures
   * @since 10.0
   */
  @SafeVarargs
  public static <V extends @Nullable Object> ListenableFuture<List<@Nullable V>> successfulAsList(
      ListenableFuture<? extends V>... futures) {
    /*
     * Another way to express this signature would be to bound <V> by @NonNull and accept
     * LF<? extends @Nullable V>. That might be better: There's currently no difference between the
     * outputs users get when calling this with <Foo> and calling it with <@Nullable Foo>. The only
     * difference is that calling it with <Foo> won't work when an input Future has a @Nullable
     * type. So why even make that error possible by giving callers the choice?
     *
     * On the other hand, the current signature is consistent with the similar allAsList method. And
     * eventually this method may go away entirely in favor of an API like
     * whenAllComplete().collectSuccesses(). That API would have a signature more like the current
     * one.
     */
    return new ListFuture<V>(ImmutableList.copyOf(futures), false);
  }

  /**
   * Creates a new {@code ListenableFuture} whose value is a list containing the values of all its
   * successful input futures. The list of results is in the same order as the input list, and if
   * any of the provided futures fails or is canceled, its corresponding position will contain
   * {@code null} (which is indistinguishable from the future having a successful value of {@code
   * null}).
   *
   * <p>The list of results is in the same order as the input list.
   *
   * <p>This differs from {@link #allAsList(Iterable)} in that it's tolerant of failed futures for
   * any of the items, representing them as {@code null} in the result list.
   *
   * <p>Canceling this future will attempt to cancel all the component futures.
   *
   * @param futures futures to combine
   * @return a future that provides a list of the results of the component futures
   * @since 10.0
   */
  public static <V extends @Nullable Object> ListenableFuture<List<@Nullable V>> successfulAsList(
      Iterable<? extends ListenableFuture<? extends V>> futures) {
    return new ListFuture<V>(ImmutableList.copyOf(futures), false);
  }

  /**
   * Returns a list of delegate futures that correspond to the futures received in the order that
   * they complete. Delegate futures return the same value or throw the same exception as the
   * corresponding input future returns/throws.
   *
   * <p>"In the order that they complete" means, for practical purposes, about what you would
   * expect, but there are some subtleties. First, we do guarantee that, if the output future at
   * index n is done, the output future at index n-1 is also done. (But as usual with futures, some
   * listeners for future n may complete before some for future n-1.) However, it is possible, if
   * one input completes with result X and another later with result Y, for Y to come before X in
   * the output future list. (Such races are impossible to solve without global synchronization of
   * all future completions. And they should have little practical impact.)
   *
   * <p>Cancelling a delegate future propagates to input futures once all the delegates complete,
   * either from cancellation or because an input future has completed. If N futures are passed in,
   * and M delegates are cancelled, the remaining M input futures will be cancelled once N - M of
   * the input futures complete. If all the delegates are cancelled, all the input futures will be
   * too.
   *
   * @since 17.0
   */
  public static <T extends @Nullable Object> ImmutableList<ListenableFuture<T>> inCompletionOrder(
      Iterable<? extends ListenableFuture<? extends T>> futures) {
    ListenableFuture<? extends T>[] copy = gwtCompatibleToArray(futures);
    final InCompletionOrderState<T> state = new InCompletionOrderState<>(copy);
    ImmutableList.Builder<AbstractFuture<T>> delegatesBuilder =
        ImmutableList.builderWithExpectedSize(copy.length);
    for (int i = 0; i < copy.length; i++) {
      delegatesBuilder.add(new InCompletionOrderFuture<T>(state));
    }

    final ImmutableList<AbstractFuture<T>> delegates = delegatesBuilder.build();
    for (int i = 0; i < copy.length; i++) {
      final int localI = i;
      copy[i].addListener(() -> state.recordInputCompletion(delegates, localI), directExecutor());
    }

    @SuppressWarnings("unchecked")
    ImmutableList<ListenableFuture<T>> delegatesCast = (ImmutableList) delegates;
    return delegatesCast;
  }

  /** Can't use Iterables.toArray because it's not gwt compatible */
  @SuppressWarnings("unchecked")
  private static <T extends @Nullable Object> ListenableFuture<? extends T>[] gwtCompatibleToArray(
      Iterable<? extends ListenableFuture<? extends T>> futures) {
    final Collection<ListenableFuture<? extends T>> collection;
    if (futures instanceof Collection) {
      collection = (Collection<ListenableFuture<? extends T>>) futures;
    } else {
      collection = ImmutableList.copyOf(futures);
    }
    return (ListenableFuture<? extends T>[]) collection.toArray(new ListenableFuture<?>[0]);
  }

  // This can't be a TrustedFuture, because TrustedFuture has clever optimizations that
  // mean cancel won't be called if this Future is passed into setFuture, and then
  // cancelled.
  private static final class InCompletionOrderFuture<T extends @Nullable Object>
      extends AbstractFuture<T> {
    @CheckForNull private InCompletionOrderState<T> state;

    private InCompletionOrderFuture(InCompletionOrderState<T> state) {
      this.state = state;
    }

    @Override
    public boolean cancel(boolean interruptIfRunning) {
      InCompletionOrderState<T> localState = state;
      if (super.cancel(interruptIfRunning)) {
        /*
         * requireNonNull is generally safe: If cancel succeeded, then this Future was still
         * pending, so its `state` field hasn't been nulled out yet.
         *
         * OK, it's technically possible for this to fail in the presence of unsafe publishing, as
         * discussed in the comments in TimeoutFuture. TODO(cpovirk): Maybe check for null before
         * calling recordOutputCancellation?
         */
        requireNonNull(localState).recordOutputCancellation(interruptIfRunning);
        return true;
      }
      return false;
    }

    @Override
    protected void afterDone() {
      state = null;
    }

    @Override
    @CheckForNull
    protected String pendingToString() {
      InCompletionOrderState<T> localState = state;
      if (localState != null) {
        // Don't print the actual array! We don't want inCompletionOrder(list).toString() to have
        // quadratic output.
        return "inputCount=["
            + localState.inputFutures.length
            + "], remaining=["
            + localState.incompleteOutputCount.get()
            + "]";
      }
      return null;
    }
  }

  private static final class InCompletionOrderState<T extends @Nullable Object> {
    // A happens-before edge between the writes of these fields and their reads exists, because
    // in order to read these fields, the corresponding write to incompleteOutputCount must have
    // been read.
    private boolean wasCancelled = false;
    private boolean shouldInterrupt = true;
    private final AtomicInteger incompleteOutputCount;
    // We set the elements of the array to null as they complete.
    private final @Nullable ListenableFuture<? extends T>[] inputFutures;
    private volatile int delegateIndex = 0;

    private InCompletionOrderState(ListenableFuture<? extends T>[] inputFutures) {
      this.inputFutures = inputFutures;
      incompleteOutputCount = new AtomicInteger(inputFutures.length);
    }

    private void recordOutputCancellation(boolean interruptIfRunning) {
      wasCancelled = true;
      // If all the futures were cancelled with interruption, cancel the input futures
      // with interruption; otherwise cancel without
      if (!interruptIfRunning) {
        shouldInterrupt = false;
      }
      recordCompletion();
    }

    private void recordInputCompletion(
        ImmutableList<AbstractFuture<T>> delegates, int inputFutureIndex) {
      /*
       * requireNonNull is safe because we accepted an Iterable of non-null Future instances, and we
       * don't overwrite an element in the array until after reading it.
       */
      ListenableFuture<? extends T> inputFuture = requireNonNull(inputFutures[inputFutureIndex]);
      // Null out our reference to this future, so it can be GCed
      inputFutures[inputFutureIndex] = null;
      for (int i = delegateIndex; i < delegates.size(); i++) {
        if (delegates.get(i).setFuture(inputFuture)) {
          recordCompletion();
          // this is technically unnecessary, but should speed up later accesses
          delegateIndex = i + 1;
          return;
        }
      }
      // If all the delegates were complete, no reason for the next listener to have to
      // go through the whole list. Avoids O(n^2) behavior when the entire output list is
      // cancelled.
      delegateIndex = delegates.size();
    }

    private void recordCompletion() {
      if (incompleteOutputCount.decrementAndGet() == 0 && wasCancelled) {
        for (ListenableFuture<? extends T> toCancel : inputFutures) {
          if (toCancel != null) {
            toCancel.cancel(shouldInterrupt);
          }
        }
      }
    }
  }

  /**
   * Registers separate success and failure callbacks to be run when the {@code Future}'s
   * computation is {@linkplain java.util.concurrent.Future#isDone() complete} or, if the
   * computation is already complete, immediately.
   *
   * <p>The callback is run on {@code executor}. There is no guaranteed ordering of execution of
   * callbacks, but any callback added through this method is guaranteed to be called once the
   * computation is complete.
   *
   * <p>Exceptions thrown by a {@code callback} will be propagated up to the executor. Any exception
   * thrown during {@code Executor.execute} (e.g., a {@code RejectedExecutionException} or an
   * exception thrown by {@linkplain MoreExecutors#directExecutor direct execution}) will be caught
   * and logged.
   *
   * <p>Example:
   *
   * <pre>{@code
   * ListenableFuture<QueryResult> future = ...;
   * Executor e = ...
   * addCallback(future,
   *     new FutureCallback<QueryResult>() {
   *       public void onSuccess(QueryResult result) {
   *         storeInCache(result);
   *       }
   *       public void onFailure(Throwable t) {
   *         reportError(t);
   *       }
   *     }, e);
   * }</pre>
   *
   * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See
   * the warnings the {@link MoreExecutors#directExecutor} documentation.
   *
   * <p>For a more general interface to attach a completion listener to a {@code Future}, see {@link
   * ListenableFuture#addListener addListener}.
   *
   * @param future The future attach the callback to.
   * @param callback The callback to invoke when {@code future} is completed.
   * @param executor The executor to run {@code callback} when the future completes.
   * @since 10.0
   */
  public static <V extends @Nullable Object> void addCallback(
      final ListenableFuture<V> future,
      final FutureCallback<? super V> callback,
      Executor executor) {
    Preconditions.checkNotNull(callback);
    future.addListener(new CallbackListener<V>(future, callback), executor);
  }

  /** See {@link #addCallback(ListenableFuture, FutureCallback, Executor)} for behavioral notes. */
  private static final class CallbackListener<V extends @Nullable Object> implements Runnable {
    final Future<V> future;
    final FutureCallback<? super V> callback;

    CallbackListener(Future<V> future, FutureCallback<? super V> callback) {
      this.future = future;
      this.callback = callback;
    }

    @Override
    public void run() {
      if (future instanceof InternalFutureFailureAccess) {
        Throwable failure =
            InternalFutures.tryInternalFastPathGetFailure((InternalFutureFailureAccess) future);
        if (failure != null) {
          callback.onFailure(failure);
          return;
        }
      }
      final V value;
      try {
        value = getDone(future);
      } catch (ExecutionException e) {
        callback.onFailure(e.getCause());
        return;
      } catch (Throwable e) {
        // Any Exception is either a RuntimeException or sneaky checked exception.
        callback.onFailure(e);
        return;
      }
      callback.onSuccess(value);
    }

    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).addValue(callback).toString();
    }
  }

  /**
   * Returns the result of the input {@code Future}, which must have already completed.
   *
   * <p>The benefits of this method are twofold. First, the name "getDone" suggests to readers that
   * the {@code Future} is already done. Second, if buggy code calls {@code getDone} on a {@code
   * Future} that is still pending, the program will throw instead of block. This can be important
   * for APIs like {@link #whenAllComplete whenAllComplete(...)}{@code .}{@link
   * FutureCombiner#call(Callable, Executor) call(...)}, where it is easy to use a new input from
   * the {@code call} implementation but forget to add it to the arguments of {@code
   * whenAllComplete}.
   *
   * <p>If you are looking for a method to determine whether a given {@code Future} is done, use the
   * instance method {@link Future#isDone()}.
   *
   * @throws ExecutionException if the {@code Future} failed with an exception
   * @throws CancellationException if the {@code Future} was cancelled
   * @throws IllegalStateException if the {@code Future} is not done
   * @since 20.0
   */
  @CanIgnoreReturnValue
  // TODO(cpovirk): Consider calling getDone() in our own code.
  @ParametricNullness
  public static <V extends @Nullable Object> V getDone(Future<V> future) throws ExecutionException {
    /*
     * We throw IllegalStateException, since the call could succeed later. Perhaps we "should" throw
     * IllegalArgumentException, since the call could succeed with a different argument. Those
     * exceptions' docs suggest that either is acceptable. Google's Java Practices page recommends
     * IllegalArgumentException here, in part to keep its recommendation simple: Static methods
     * should throw IllegalStateException only when they use static state.
     *
     * Why do we deviate here? The answer: We want for fluentFuture.getDone() to throw the same
     * exception as Futures.getDone(fluentFuture).
     */
    checkState(future.isDone(), "Future was expected to be done: %s", future);
    return getUninterruptibly(future);
  }

  /**
   * Returns the result of {@link Future#get()}, converting most exceptions to a new instance of the
   * given checked exception type. This reduces boilerplate for a common use of {@code Future} in
   * which it is unnecessary to programmatically distinguish between exception types or to extract
   * other information from the exception instance.
   *
   * <p>Exceptions from {@code Future.get} are treated as follows:
   *
   * <ul>
   *   <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an {@code X} if the cause
   *       is a checked exception, an {@link UncheckedExecutionException} if the cause is a {@code
   *       RuntimeException}, or an {@link ExecutionError} if the cause is an {@code Error}.
   *   <li>Any {@link InterruptedException} is wrapped in an {@code X} (after restoring the
   *       interrupt).
   *   <li>Any {@link CancellationException} is propagated untouched, as is any other {@link
   *       RuntimeException} (though {@code get} implementations are discouraged from throwing such
   *       exceptions).
   * </ul>
   *
   * <p>The overall principle is to continue to treat every checked exception as a checked
   * exception, every unchecked exception as an unchecked exception, and every error as an error. In
   * addition, the cause of any {@code ExecutionException} is wrapped in order to ensure that the
   * new stack trace matches that of the current thread.
   *
   * <p>Instances of {@code exceptionClass} are created by choosing an arbitrary public constructor
   * that accepts zero or more arguments, all of type {@code String} or {@code Throwable}
   * (preferring constructors with at least one {@code String}, then preferring constructors with at
   * least one {@code Throwable}) and calling the constructor via reflection. If the exception did
   * not already have a cause, one is set by calling {@link Throwable#initCause(Throwable)} on it.
   * If no such constructor exists, an {@code IllegalArgumentException} is thrown.
   *
   * @throws X if {@code get} throws any checked exception except for an {@code ExecutionException}
   *     whose cause is not itself a checked exception
   * @throws UncheckedExecutionException if {@code get} throws an {@code ExecutionException} with a
   *     {@code RuntimeException} as its cause
   * @throws ExecutionError if {@code get} throws an {@code ExecutionException} with an {@code
   *     Error} as its cause
   * @throws CancellationException if {@code get} throws a {@code CancellationException}
   * @throws IllegalArgumentException if {@code exceptionClass} extends {@code RuntimeException} or
   *     does not have a suitable constructor
   * @since 19.0 (in 10.0 as {@code get})
   */
  @CanIgnoreReturnValue
  @J2ktIncompatible
  @GwtIncompatible // reflection
  @ParametricNullness
  public static <V extends @Nullable Object, X extends Exception> V getChecked(
      Future<V> future, Class<X> exceptionClass) throws X {
    return FuturesGetChecked.getChecked(future, exceptionClass);
  }

  /**
   * Returns the result of {@link Future#get(long, TimeUnit)}, converting most exceptions to a new
   * instance of the given checked exception type. This reduces boilerplate for a common use of
   * {@code Future} in which it is unnecessary to programmatically distinguish between exception
   * types or to extract other information from the exception instance.
   *
   * <p>Exceptions from {@code Future.get} are treated as follows:
   *
   * <ul>
   *   <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an {@code X} if the cause
   *       is a checked exception, an {@link UncheckedExecutionException} if the cause is a {@code
   *       RuntimeException}, or an {@link ExecutionError} if the cause is an {@code Error}.
   *   <li>Any {@link InterruptedException} is wrapped in an {@code X} (after restoring the
   *       interrupt).
   *   <li>Any {@link TimeoutException} is wrapped in an {@code X}.
   *   <li>Any {@link CancellationException} is propagated untouched, as is any other {@link
   *       RuntimeException} (though {@code get} implementations are discouraged from throwing such
   *       exceptions).
   * </ul>
   *
   * <p>The overall principle is to continue to treat every checked exception as a checked
   * exception, every unchecked exception as an unchecked exception, and every error as an error. In
   * addition, the cause of any {@code ExecutionException} is wrapped in order to ensure that the
   * new stack trace matches that of the current thread.
   *
   * <p>Instances of {@code exceptionClass} are created by choosing an arbitrary public constructor
   * that accepts zero or more arguments, all of type {@code String} or {@code Throwable}
   * (preferring constructors with at least one {@code String}, then preferring constructors with at
   * least one {@code Throwable}) and calling the constructor via reflection. If the exception did
   * not already have a cause, one is set by calling {@link Throwable#initCause(Throwable)} on it.
   * If no such constructor exists, an {@code IllegalArgumentException} is thrown.
   *
   * @throws X if {@code get} throws any checked exception except for an {@code ExecutionException}
   *     whose cause is not itself a checked exception
   * @throws UncheckedExecutionException if {@code get} throws an {@code ExecutionException} with a
   *     {@code RuntimeException} as its cause
   * @throws ExecutionError if {@code get} throws an {@code ExecutionException} with an {@code
   *     Error} as its cause
   * @throws CancellationException if {@code get} throws a {@code CancellationException}
   * @throws IllegalArgumentException if {@code exceptionClass} extends {@code RuntimeException} or
   *     does not have a suitable constructor
   * @since 28.0
   */
  @CanIgnoreReturnValue
  @J2ktIncompatible
  @GwtIncompatible // reflection
  @ParametricNullness
  public static <V extends @Nullable Object, X extends Exception> V getChecked(
      Future<V> future, Class<X> exceptionClass, Duration timeout) throws X {
    return getChecked(future, exceptionClass, toNanosSaturated(timeout), TimeUnit.NANOSECONDS);
  }

  /**
   * Returns the result of {@link Future#get(long, TimeUnit)}, converting most exceptions to a new
   * instance of the given checked exception type. This reduces boilerplate for a common use of
   * {@code Future} in which it is unnecessary to programmatically distinguish between exception
   * types or to extract other information from the exception instance.
   *
   * <p>Exceptions from {@code Future.get} are treated as follows:
   *
   * <ul>
   *   <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an {@code X} if the cause
   *       is a checked exception, an {@link UncheckedExecutionException} if the cause is a {@code
   *       RuntimeException}, or an {@link ExecutionError} if the cause is an {@code Error}.
   *   <li>Any {@link InterruptedException} is wrapped in an {@code X} (after restoring the
   *       interrupt).
   *   <li>Any {@link TimeoutException} is wrapped in an {@code X}.
   *   <li>Any {@link CancellationException} is propagated untouched, as is any other {@link
   *       RuntimeException} (though {@code get} implementations are discouraged from throwing such
   *       exceptions).
   * </ul>
   *
   * <p>The overall principle is to continue to treat every checked exception as a checked
   * exception, every unchecked exception as an unchecked exception, and every error as an error. In
   * addition, the cause of any {@code ExecutionException} is wrapped in order to ensure that the
   * new stack trace matches that of the current thread.
   *
   * <p>Instances of {@code exceptionClass} are created by choosing an arbitrary public constructor
   * that accepts zero or more arguments, all of type {@code String} or {@code Throwable}
   * (preferring constructors with at least one {@code String}) and calling the constructor via
   * reflection. If the exception did not already have a cause, one is set by calling {@link
   * Throwable#initCause(Throwable)} on it. If no such constructor exists, an {@code
   * IllegalArgumentException} is thrown.
   *
   * @throws X if {@code get} throws any checked exception except for an {@code ExecutionException}
   *     whose cause is not itself a checked exception
   * @throws UncheckedExecutionException if {@code get} throws an {@code ExecutionException} with a
   *     {@code RuntimeException} as its cause
   * @throws ExecutionError if {@code get} throws an {@code ExecutionException} with an {@code
   *     Error} as its cause
   * @throws CancellationException if {@code get} throws a {@code CancellationException}
   * @throws IllegalArgumentException if {@code exceptionClass} extends {@code RuntimeException} or
   *     does not have a suitable constructor
   * @since 19.0 (in 10.0 as {@code get} and with different parameter order)
   */
  @CanIgnoreReturnValue
  @J2ktIncompatible
  @GwtIncompatible // reflection
  @SuppressWarnings("GoodTime") // should accept a java.time.Duration
  @ParametricNullness
  public static <V extends @Nullable Object, X extends Exception> V getChecked(
      Future<V> future, Class<X> exceptionClass, long timeout, TimeUnit unit) throws X {
    return FuturesGetChecked.getChecked(future, exceptionClass, timeout, unit);
  }

  /**
   * Returns the result of calling {@link Future#get()} uninterruptibly on a task known not to throw
   * a checked exception. This makes {@code Future} more suitable for lightweight, fast-running
   * tasks that, barring bugs in the code, will not fail. This gives it exception-handling behavior
   * similar to that of {@code ForkJoinTask.join}.
   *
   * <p>Exceptions from {@code Future.get} are treated as follows:
   *
   * <ul>
   *   <li>Any {@link ExecutionException} has its <i>cause</i> wrapped in an {@link
   *       UncheckedExecutionException} (if the cause is an {@code Exception}) or {@link
   *       ExecutionError} (if the cause is an {@code Error}).
   *   <li>Any {@link InterruptedException} causes a retry of the {@code get} call. The interrupt is
   *       restored before {@code getUnchecked} returns.
   *   <li>Any {@link CancellationException} is propagated untouched. So is any other {@link
   *       RuntimeException} ({@code get} implementations are discouraged from throwing such
   *       exceptions).
   * </ul>
   *
   * <p>The overall principle is to eliminate all checked exceptions: to loop to avoid {@code
   * InterruptedException}, to pass through {@code CancellationException}, and to wrap any exception
   * from the underlying computation in an {@code UncheckedExecutionException} or {@code
   * ExecutionError}.
   *
   * <p>For an uninterruptible {@code get} that preserves other exceptions, see {@link
   * Uninterruptibles#getUninterruptibly(Future)}.
   *
   * @throws UncheckedExecutionException if {@code get} throws an {@code ExecutionException} with an
   *     {@code Exception} as its cause
   * @throws ExecutionError if {@code get} throws an {@code ExecutionException} with an {@code
   *     Error} as its cause
   * @throws CancellationException if {@code get} throws a {@code CancellationException}
   * @since 10.0
   */
  @CanIgnoreReturnValue
  @ParametricNullness
  public static <V extends @Nullable Object> V getUnchecked(Future<V> future) {
    checkNotNull(future);
    try {
      return getUninterruptibly(future);
    } catch (ExecutionException e) {
      wrapAndThrowUnchecked(e.getCause());
      throw new AssertionError();
    }
  }

  private static void wrapAndThrowUnchecked(Throwable cause) {
    if (cause instanceof Error) {
      throw new ExecutionError((Error) cause);
    }
    /*
     * It's an Exception. (Or it's a non-Error, non-Exception Throwable. From my survey of such
     * classes, I believe that most users intended to extend Exception, so we'll treat it like an
     * Exception.)
     */
    throw new UncheckedExecutionException(cause);
  }

  /*
   * Arguably we don't need a timed getUnchecked because any operation slow enough to require a
   * timeout is heavyweight enough to throw a checked exception and therefore be inappropriate to
   * use with getUnchecked. Further, it's not clear that converting the checked TimeoutException to
   * a RuntimeException -- especially to an UncheckedExecutionException, since it wasn't thrown by
   * the computation -- makes sense, and if we don't convert it, the user still has to write a
   * try-catch block.
   *
   * If you think you would use this method, let us know. You might also look into the
   * Fork-Join framework: http://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
   */
}