001/* 002 * Copyright (C) 2017 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.util.concurrent; 018 019import static com.google.common.base.Functions.constant; 020import static com.google.common.base.MoreObjects.toStringHelper; 021import static com.google.common.base.Preconditions.checkArgument; 022import static com.google.common.base.Preconditions.checkNotNull; 023import static com.google.common.base.Preconditions.checkState; 024import static com.google.common.collect.Lists.asList; 025import static com.google.common.util.concurrent.ClosingFuture.State.CLOSED; 026import static com.google.common.util.concurrent.ClosingFuture.State.CLOSING; 027import static com.google.common.util.concurrent.ClosingFuture.State.OPEN; 028import static com.google.common.util.concurrent.ClosingFuture.State.SUBSUMED; 029import static com.google.common.util.concurrent.ClosingFuture.State.WILL_CLOSE; 030import static com.google.common.util.concurrent.ClosingFuture.State.WILL_CREATE_VALUE_AND_CLOSER; 031import static com.google.common.util.concurrent.Futures.getDone; 032import static com.google.common.util.concurrent.Futures.immediateFuture; 033import static com.google.common.util.concurrent.Futures.nonCancellationPropagating; 034import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 035import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException; 036import static java.util.logging.Level.FINER; 037import static java.util.logging.Level.SEVERE; 038import static java.util.logging.Level.WARNING; 039 040import com.google.common.annotations.J2ktIncompatible; 041import com.google.common.annotations.VisibleForTesting; 042import com.google.common.collect.FluentIterable; 043import com.google.common.collect.ImmutableList; 044import com.google.common.util.concurrent.ClosingFuture.Combiner.AsyncCombiningCallable; 045import com.google.common.util.concurrent.ClosingFuture.Combiner.CombiningCallable; 046import com.google.common.util.concurrent.Futures.FutureCombiner; 047import com.google.errorprone.annotations.CanIgnoreReturnValue; 048import com.google.errorprone.annotations.DoNotMock; 049import com.google.j2objc.annotations.RetainedWith; 050import java.io.Closeable; 051import java.util.IdentityHashMap; 052import java.util.Map; 053import java.util.concurrent.Callable; 054import java.util.concurrent.CancellationException; 055import java.util.concurrent.CountDownLatch; 056import java.util.concurrent.ExecutionException; 057import java.util.concurrent.Executor; 058import java.util.concurrent.Future; 059import java.util.concurrent.RejectedExecutionException; 060import java.util.concurrent.atomic.AtomicReference; 061import org.jspecify.annotations.Nullable; 062 063/** 064 * A step in a pipeline of an asynchronous computation. When the last step in the computation is 065 * complete, some objects captured during the computation are closed. 066 * 067 * <p>A pipeline of {@code ClosingFuture}s is a tree of steps. Each step represents either an 068 * asynchronously-computed intermediate value, or else an exception that indicates the failure or 069 * cancellation of the operation so far. The only way to extract the value or exception from a step 070 * is by declaring that step to be the last step of the pipeline. Nevertheless, we refer to the 071 * "value" of a successful step or the "result" (value or exception) of any step. 072 * 073 * <ol> 074 * <li>A pipeline starts at its leaf step (or steps), which is created from either a callable 075 * block or a {@link ListenableFuture}. 076 * <li>Each other step is derived from one or more input steps. At each step, zero or more objects 077 * can be captured for later closing. 078 * <li>There is one last step (the root of the tree), from which you can extract the final result 079 * of the computation. After that result is available (or the computation fails), all objects 080 * captured by any of the steps in the pipeline are closed. 081 * </ol> 082 * 083 * <h3>Starting a pipeline</h3> 084 * 085 * Start a {@code ClosingFuture} pipeline {@linkplain #submit(ClosingCallable, Executor) from a 086 * callable block} that may capture objects for later closing. To start a pipeline from a {@link 087 * ListenableFuture} that doesn't create resources that should be closed later, you can use {@link 088 * #from(ListenableFuture)} instead. 089 * 090 * <h3>Derived steps</h3> 091 * 092 * A {@code ClosingFuture} step can be derived from one or more input {@code ClosingFuture} steps in 093 * ways similar to {@link FluentFuture}s: 094 * 095 * <ul> 096 * <li>by transforming the value from a successful input step, 097 * <li>by catching the exception from a failed input step, or 098 * <li>by combining the results of several input steps. 099 * </ul> 100 * 101 * Each derivation can capture the next value or any intermediate objects for later closing. 102 * 103 * <p>A step can be the input to at most one derived step. Once you transform its value, catch its 104 * exception, or combine it with others, you cannot do anything else with it, including declare it 105 * to be the last step of the pipeline. 106 * 107 * <h4>Transforming</h4> 108 * 109 * To derive the next step by asynchronously applying a function to an input step's value, call 110 * {@link #transform(ClosingFunction, Executor)} or {@link #transformAsync(AsyncClosingFunction, 111 * Executor)} on the input step. 112 * 113 * <h4>Catching</h4> 114 * 115 * To derive the next step from a failed input step, call {@link #catching(Class, ClosingFunction, 116 * Executor)} or {@link #catchingAsync(Class, AsyncClosingFunction, Executor)} on the input step. 117 * 118 * <h4>Combining</h4> 119 * 120 * To derive a {@code ClosingFuture} from two or more input steps, pass the input steps to {@link 121 * #whenAllComplete(Iterable)} or {@link #whenAllSucceed(Iterable)} or its overloads. 122 * 123 * <h3>Cancelling</h3> 124 * 125 * Any step in a pipeline can be {@linkplain #cancel(boolean) cancelled}, even after another step 126 * has been derived, with the same semantics as cancelling a {@link Future}. In addition, a 127 * successfully cancelled step will immediately start closing all objects captured for later closing 128 * by it and by its input steps. 129 * 130 * <h3>Ending a pipeline</h3> 131 * 132 * Each {@code ClosingFuture} pipeline must be ended. To end a pipeline, decide whether you want to 133 * close the captured objects automatically or manually. 134 * 135 * <h4>Automatically closing</h4> 136 * 137 * You can extract a {@link Future} that represents the result of the last step in the pipeline by 138 * calling {@link #finishToFuture()}. All objects the pipeline has captured for closing will begin 139 * to be closed asynchronously <b>after</b> the returned {@code Future} is done: the future 140 * completes before closing starts, rather than once it has finished. 141 * 142 * <pre>{@code 143 * FluentFuture<UserName> userName = 144 * ClosingFuture.submit( 145 * closer -> closer.eventuallyClose(database.newTransaction(), closingExecutor), 146 * executor) 147 * .transformAsync((closer, transaction) -> transaction.queryClosingFuture("..."), executor) 148 * .transform((closer, result) -> result.get("userName"), directExecutor()) 149 * .catching(DBException.class, e -> "no user", directExecutor()) 150 * .finishToFuture(); 151 * }</pre> 152 * 153 * In this example, when the {@code userName} {@link Future} is done, the transaction and the query 154 * result cursor will both be closed, even if the operation is cancelled or fails. 155 * 156 * <h4>Manually closing</h4> 157 * 158 * If you want to close the captured objects manually, after you've used the final result, call 159 * {@link #finishToValueAndCloser(ValueAndCloserConsumer, Executor)} to get an object that holds the 160 * final result. You then call {@link ValueAndCloser#closeAsync()} to close the captured objects. 161 * 162 * <pre>{@code 163 * ClosingFuture.submit( 164 * closer -> closer.eventuallyClose(database.newTransaction(), closingExecutor), 165 * executor) 166 * .transformAsync((closer, transaction) -> transaction.queryClosingFuture("..."), executor) 167 * .transform((closer, result) -> result.get("userName"), directExecutor()) 168 * .catching(DBException.class, e -> "no user", directExecutor()) 169 * .finishToValueAndCloser( 170 * valueAndCloser -> this.userNameValueAndCloser = valueAndCloser, executor); 171 * 172 * // later 173 * try { // get() will throw if the operation failed or was cancelled. 174 * UserName userName = userNameValueAndCloser.get(); 175 * // do something with userName 176 * } finally { 177 * userNameValueAndCloser.closeAsync(); 178 * } 179 * }</pre> 180 * 181 * In this example, when {@code userNameValueAndCloser.closeAsync()} is called, the transaction and 182 * the query result cursor will both be closed, even if the operation is cancelled or fails. 183 * 184 * <p>Note that if you don't call {@code closeAsync()}, the captured objects will not be closed. The 185 * automatic-closing approach described above is safer. 186 * 187 * @param <V> the type of the value of this step 188 * @since 30.0 189 */ 190// TODO(dpb): Consider reusing one CloseableList for the entire pipeline, modulo combinations. 191@DoNotMock("Use ClosingFuture.from(Futures.immediate*Future)") 192@J2ktIncompatible 193// TODO(dpb): GWT compatibility. 194public final class ClosingFuture<V extends @Nullable Object> { 195 196 private static final LazyLogger logger = new LazyLogger(ClosingFuture.class); 197 198 /** 199 * An object that can capture objects to be closed later, when a {@link ClosingFuture} pipeline is 200 * done. 201 */ 202 public static final class DeferredCloser { 203 @RetainedWith private final CloseableList list; 204 205 DeferredCloser(CloseableList list) { 206 this.list = list; 207 } 208 209 /** 210 * Captures an object to be closed when a {@link ClosingFuture} pipeline is done. 211 * 212 * <p>For users of the {@code -jre} flavor of Guava, the object can be any {@code 213 * AutoCloseable}. For users of the {@code -android} flavor, the object must be a {@code 214 * Closeable}. (For more about the flavors, see <a 215 * href="https://github.com/google/guava#adding-guava-to-your-build">Adding Guava to your 216 * build</a>.) 217 * 218 * <p>Be careful when targeting an older SDK than you are building against (most commonly when 219 * building for Android): Ensure that any object you pass implements the interface not just in 220 * your current SDK version but also at the oldest version you support. For example, <a 221 * href="https://developer.android.com/sdk/api_diff/16/">API Level 16</a> is the first version 222 * in which {@code Cursor} is {@code Closeable}. To support older versions, pass a wrapper 223 * {@code Closeable} with a method reference like {@code cursor::close}. 224 * 225 * <p>Note that this method is still binary-compatible between flavors because the erasure of 226 * its parameter type is {@code Object}, not {@code AutoCloseable} or {@code Closeable}. 227 * 228 * @param closeable the object to be closed (see notes above) 229 * @param closingExecutor the object will be closed on this executor 230 * @return the first argument 231 */ 232 @CanIgnoreReturnValue 233 @ParametricNullness 234 public <C extends @Nullable Object & @Nullable AutoCloseable> C eventuallyClose( 235 @ParametricNullness C closeable, Executor closingExecutor) { 236 checkNotNull(closingExecutor); 237 if (closeable != null) { 238 list.add(closeable, closingExecutor); 239 } 240 return closeable; 241 } 242 } 243 244 /** 245 * An operation that computes a result. 246 * 247 * @param <V> the type of the result 248 */ 249 @FunctionalInterface 250 public interface ClosingCallable<V extends @Nullable Object> { 251 /** 252 * Computes a result, or throws an exception if unable to do so. 253 * 254 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 255 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done (but 256 * not before this method completes), even if this method throws or the pipeline is cancelled. 257 */ 258 @ParametricNullness 259 V call(DeferredCloser closer) throws Exception; 260 } 261 262 /** 263 * An operation that computes a {@link ClosingFuture} of a result. 264 * 265 * @param <V> the type of the result 266 * @since 30.1 267 */ 268 @FunctionalInterface 269 public interface AsyncClosingCallable<V extends @Nullable Object> { 270 /** 271 * Computes a result, or throws an exception if unable to do so. 272 * 273 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 274 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done (but 275 * not before this method completes), even if this method throws or the pipeline is cancelled. 276 */ 277 ClosingFuture<V> call(DeferredCloser closer) throws Exception; 278 } 279 280 /** 281 * A function from an input to a result. 282 * 283 * @param <T> the type of the input to the function 284 * @param <U> the type of the result of the function 285 */ 286 @FunctionalInterface 287 public interface ClosingFunction<T extends @Nullable Object, U extends @Nullable Object> { 288 289 /** 290 * Applies this function to an input, or throws an exception if unable to do so. 291 * 292 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 293 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done (but 294 * not before this method completes), even if this method throws or the pipeline is cancelled. 295 */ 296 @ParametricNullness 297 U apply(DeferredCloser closer, @ParametricNullness T input) throws Exception; 298 } 299 300 /** 301 * A function from an input to a {@link ClosingFuture} of a result. 302 * 303 * @param <T> the type of the input to the function 304 * @param <U> the type of the result of the function 305 */ 306 @FunctionalInterface 307 public interface AsyncClosingFunction<T extends @Nullable Object, U extends @Nullable Object> { 308 /** 309 * Applies this function to an input, or throws an exception if unable to do so. 310 * 311 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 312 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done (but 313 * not before this method completes), even if this method throws or the pipeline is cancelled. 314 */ 315 ClosingFuture<U> apply(DeferredCloser closer, @ParametricNullness T input) throws Exception; 316 } 317 318 /** 319 * An object that holds the final result of an asynchronous {@link ClosingFuture} operation and 320 * allows the user to close all the closeable objects that were captured during it for later 321 * closing. 322 * 323 * <p>The asynchronous operation will have completed before this object is created. 324 * 325 * @param <V> the type of the value of a successful operation 326 * @see ClosingFuture#finishToValueAndCloser(ValueAndCloserConsumer, Executor) 327 */ 328 public static final class ValueAndCloser<V extends @Nullable Object> { 329 330 private final ClosingFuture<? extends V> closingFuture; 331 332 ValueAndCloser(ClosingFuture<? extends V> closingFuture) { 333 this.closingFuture = checkNotNull(closingFuture); 334 } 335 336 /** 337 * Returns the final value of the associated {@link ClosingFuture}, or throws an exception as 338 * {@link Future#get()} would. 339 * 340 * <p>Because the asynchronous operation has already completed, this method is synchronous and 341 * returns immediately. 342 * 343 * @throws CancellationException if the computation was cancelled 344 * @throws ExecutionException if the computation threw an exception 345 */ 346 @ParametricNullness 347 public V get() throws ExecutionException { 348 return getDone(closingFuture.future); 349 } 350 351 /** 352 * Starts closing all closeable objects captured during the {@link ClosingFuture}'s asynchronous 353 * operation on the {@link Executor}s specified by calls to {@link 354 * DeferredCloser#eventuallyClose(Object, Executor)}. 355 * 356 * <p>If any such calls specified {@link MoreExecutors#directExecutor()}, those objects will be 357 * closed synchronously. 358 * 359 * <p>Idempotent: objects will be closed at most once. 360 */ 361 public void closeAsync() { 362 closingFuture.close(); 363 } 364 } 365 366 /** 367 * Represents an operation that accepts a {@link ValueAndCloser} for the last step in a {@link 368 * ClosingFuture} pipeline. 369 * 370 * @param <V> the type of the final value of a successful pipeline 371 * @see ClosingFuture#finishToValueAndCloser(ValueAndCloserConsumer, Executor) 372 */ 373 @FunctionalInterface 374 public interface ValueAndCloserConsumer<V extends @Nullable Object> { 375 376 /** Accepts a {@link ValueAndCloser} for the last step in a {@link ClosingFuture} pipeline. */ 377 void accept(ValueAndCloser<V> valueAndCloser); 378 } 379 380 /** 381 * Starts a {@link ClosingFuture} pipeline by submitting a callable block to an executor. 382 * 383 * @throws java.util.concurrent.RejectedExecutionException if the task cannot be scheduled for 384 * execution 385 */ 386 public static <V extends @Nullable Object> ClosingFuture<V> submit( 387 ClosingCallable<V> callable, Executor executor) { 388 checkNotNull(callable); 389 CloseableList closeables = new CloseableList(); 390 TrustedListenableFutureTask<V> task = 391 TrustedListenableFutureTask.create( 392 new Callable<V>() { 393 @Override 394 @ParametricNullness 395 public V call() throws Exception { 396 return callable.call(closeables.closer); 397 } 398 399 @Override 400 public String toString() { 401 return callable.toString(); 402 } 403 }); 404 executor.execute(task); 405 return new ClosingFuture<>(task, closeables); 406 } 407 408 /** 409 * Starts a {@link ClosingFuture} pipeline by submitting a callable block to an executor. 410 * 411 * @throws java.util.concurrent.RejectedExecutionException if the task cannot be scheduled for 412 * execution 413 * @since 30.1 414 */ 415 public static <V extends @Nullable Object> ClosingFuture<V> submitAsync( 416 AsyncClosingCallable<V> callable, Executor executor) { 417 checkNotNull(callable); 418 CloseableList closeables = new CloseableList(); 419 TrustedListenableFutureTask<V> task = 420 TrustedListenableFutureTask.create( 421 new AsyncCallable<V>() { 422 @Override 423 public ListenableFuture<V> call() throws Exception { 424 CloseableList newCloseables = new CloseableList(); 425 try { 426 ClosingFuture<V> closingFuture = callable.call(newCloseables.closer); 427 closingFuture.becomeSubsumedInto(closeables); 428 return closingFuture.future; 429 } finally { 430 closeables.add(newCloseables, directExecutor()); 431 } 432 } 433 434 @Override 435 public String toString() { 436 return callable.toString(); 437 } 438 }); 439 executor.execute(task); 440 return new ClosingFuture<>(task, closeables); 441 } 442 443 /** 444 * Starts a {@link ClosingFuture} pipeline with a {@link ListenableFuture}. 445 * 446 * <p>{@code future}'s value will not be closed when the pipeline is done even if {@code V} 447 * implements {@link Closeable}. In order to start a pipeline with a value that will be closed 448 * when the pipeline is done, use {@link #submit(ClosingCallable, Executor)} instead. 449 */ 450 public static <V extends @Nullable Object> ClosingFuture<V> from(ListenableFuture<V> future) { 451 return new ClosingFuture<>(future); 452 } 453 454 /** 455 * Starts a {@link ClosingFuture} pipeline with a {@link ListenableFuture}. 456 * 457 * <p>If {@code future} succeeds, its value will be closed (using {@code closingExecutor)}) when 458 * the pipeline is done, even if the pipeline is canceled or fails. 459 * 460 * <p>Cancelling the pipeline will not cancel {@code future}, so that the pipeline can access its 461 * value in order to close it. 462 * 463 * @param future the future to create the {@code ClosingFuture} from. For discussion of the 464 * future's result type {@code C}, see {@link DeferredCloser#eventuallyClose(Object, 465 * Executor)}. 466 * @param closingExecutor the future's result will be closed on this executor 467 * @deprecated Creating {@link Future}s of closeable types is dangerous in general because the 468 * underlying value may never be closed if the {@link Future} is canceled after its operation 469 * begins. Consider replacing code that creates {@link ListenableFuture}s of closeable types, 470 * including those that pass them to this method, with {@link #submit(ClosingCallable, 471 * Executor)} in order to ensure that resources do not leak. Or, to start a pipeline with a 472 * {@link ListenableFuture} that doesn't create values that should be closed, use {@link 473 * ClosingFuture#from}. 474 */ 475 @Deprecated 476 public static <C extends @Nullable Object & @Nullable AutoCloseable> 477 ClosingFuture<C> eventuallyClosing( 478 ListenableFuture<C> future, final Executor closingExecutor) { 479 checkNotNull(closingExecutor); 480 final ClosingFuture<C> closingFuture = new ClosingFuture<>(nonCancellationPropagating(future)); 481 Futures.addCallback( 482 future, 483 new FutureCallback<@Nullable AutoCloseable>() { 484 @Override 485 public void onSuccess(@Nullable AutoCloseable result) { 486 closingFuture.closeables.closer.eventuallyClose(result, closingExecutor); 487 } 488 489 @Override 490 public void onFailure(Throwable t) {} 491 }, 492 directExecutor()); 493 return closingFuture; 494 } 495 496 /** 497 * Starts specifying how to combine {@link ClosingFuture}s into a single pipeline. 498 * 499 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 500 * the {@code futures}, or if any has already been {@linkplain #finishToFuture() finished} 501 */ 502 public static Combiner whenAllComplete(Iterable<? extends ClosingFuture<?>> futures) { 503 return new Combiner(false, futures); 504 } 505 506 /** 507 * Starts specifying how to combine {@link ClosingFuture}s into a single pipeline. 508 * 509 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 510 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 511 */ 512 public static Combiner whenAllComplete( 513 ClosingFuture<?> future1, ClosingFuture<?>... moreFutures) { 514 return whenAllComplete(asList(future1, moreFutures)); 515 } 516 517 /** 518 * Starts specifying how to combine {@link ClosingFuture}s into a single pipeline, assuming they 519 * all succeed. If any fail, the resulting pipeline will fail. 520 * 521 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 522 * the {@code futures}, or if any has already been {@linkplain #finishToFuture() finished} 523 */ 524 public static Combiner whenAllSucceed(Iterable<? extends ClosingFuture<?>> futures) { 525 return new Combiner(true, futures); 526 } 527 528 /** 529 * Starts specifying how to combine two {@link ClosingFuture}s into a single pipeline, assuming 530 * they all succeed. If any fail, the resulting pipeline will fail. 531 * 532 * <p>Calling this method allows you to use lambdas or method references typed with the types of 533 * the input {@link ClosingFuture}s. 534 * 535 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 536 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 537 */ 538 public static <V1 extends @Nullable Object, V2 extends @Nullable Object> 539 Combiner2<V1, V2> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2) { 540 return new Combiner2<>(future1, future2); 541 } 542 543 /** 544 * Starts specifying how to combine three {@link ClosingFuture}s into a single pipeline, assuming 545 * they all succeed. If any fail, the resulting pipeline will fail. 546 * 547 * <p>Calling this method allows you to use lambdas or method references typed with the types of 548 * the input {@link ClosingFuture}s. 549 * 550 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 551 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 552 */ 553 public static < 554 V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object> 555 Combiner3<V1, V2, V3> whenAllSucceed( 556 ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3) { 557 return new Combiner3<>(future1, future2, future3); 558 } 559 560 /** 561 * Starts specifying how to combine four {@link ClosingFuture}s into a single pipeline, assuming 562 * they all succeed. If any fail, the resulting pipeline will fail. 563 * 564 * <p>Calling this method allows you to use lambdas or method references typed with the types of 565 * the input {@link ClosingFuture}s. 566 * 567 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 568 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 569 */ 570 public static < 571 V1 extends @Nullable Object, 572 V2 extends @Nullable Object, 573 V3 extends @Nullable Object, 574 V4 extends @Nullable Object> 575 Combiner4<V1, V2, V3, V4> whenAllSucceed( 576 ClosingFuture<V1> future1, 577 ClosingFuture<V2> future2, 578 ClosingFuture<V3> future3, 579 ClosingFuture<V4> future4) { 580 return new Combiner4<>(future1, future2, future3, future4); 581 } 582 583 /** 584 * Starts specifying how to combine five {@link ClosingFuture}s into a single pipeline, assuming 585 * they all succeed. If any fail, the resulting pipeline will fail. 586 * 587 * <p>Calling this method allows you to use lambdas or method references typed with the types of 588 * the input {@link ClosingFuture}s. 589 * 590 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 591 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 592 */ 593 public static < 594 V1 extends @Nullable Object, 595 V2 extends @Nullable Object, 596 V3 extends @Nullable Object, 597 V4 extends @Nullable Object, 598 V5 extends @Nullable Object> 599 Combiner5<V1, V2, V3, V4, V5> whenAllSucceed( 600 ClosingFuture<V1> future1, 601 ClosingFuture<V2> future2, 602 ClosingFuture<V3> future3, 603 ClosingFuture<V4> future4, 604 ClosingFuture<V5> future5) { 605 return new Combiner5<>(future1, future2, future3, future4, future5); 606 } 607 608 /** 609 * Starts specifying how to combine {@link ClosingFuture}s into a single pipeline, assuming they 610 * all succeed. If any fail, the resulting pipeline will fail. 611 * 612 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from any of 613 * the arguments, or if any has already been {@linkplain #finishToFuture() finished} 614 */ 615 public static Combiner whenAllSucceed( 616 ClosingFuture<?> future1, 617 ClosingFuture<?> future2, 618 ClosingFuture<?> future3, 619 ClosingFuture<?> future4, 620 ClosingFuture<?> future5, 621 ClosingFuture<?> future6, 622 ClosingFuture<?>... moreFutures) { 623 return whenAllSucceed( 624 FluentIterable.of(future1, future2, future3, future4, future5, future6) 625 .append(moreFutures)); 626 } 627 628 private final AtomicReference<State> state = new AtomicReference<>(OPEN); 629 private final CloseableList closeables; 630 private final FluentFuture<V> future; 631 632 private ClosingFuture(ListenableFuture<V> future) { 633 this(future, new CloseableList()); 634 } 635 636 private ClosingFuture(ListenableFuture<V> future, CloseableList closeables) { 637 this.future = FluentFuture.from(future); 638 this.closeables = closeables; 639 } 640 641 /** 642 * Returns a future that finishes when this step does. Calling {@code get()} on the returned 643 * future returns {@code null} if the step is successful or throws the same exception that would 644 * be thrown by calling {@code finishToFuture().get()} if this were the last step. Calling {@code 645 * cancel()} on the returned future has no effect on the {@code ClosingFuture} pipeline. 646 * 647 * <p>{@code statusFuture} differs from most methods on {@code ClosingFuture}: You can make calls 648 * to {@code statusFuture} <i>in addition to</i> the call you make to {@link #finishToFuture()} or 649 * a derivation method <i>on the same instance</i>. This is important because calling {@code 650 * statusFuture} alone does not provide a way to close the pipeline. 651 */ 652 public ListenableFuture<?> statusFuture() { 653 return nonCancellationPropagating(future.transform(constant(null), directExecutor())); 654 } 655 656 /** 657 * Returns a new {@code ClosingFuture} pipeline step derived from this one by applying a function 658 * to its value. The function can use a {@link DeferredCloser} to capture objects to be closed 659 * when the pipeline is done. 660 * 661 * <p>If this {@code ClosingFuture} fails, the function will not be called, and the derived {@code 662 * ClosingFuture} will be equivalent to this one. 663 * 664 * <p>If the function throws an exception, that exception is used as the result of the derived 665 * {@code ClosingFuture}. 666 * 667 * <p>Example usage: 668 * 669 * <pre>{@code 670 * ClosingFuture<List<Row>> rowsFuture = 671 * queryFuture.transform((closer, result) -> result.getRows(), executor); 672 * }</pre> 673 * 674 * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See 675 * the discussion in the {@link ListenableFuture#addListener} documentation. All its warnings 676 * about heavyweight listeners are also applicable to heavyweight functions passed to this method. 677 * 678 * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link 679 * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on 680 * the original {@code ClosingFuture} instance. 681 * 682 * @param function transforms the value of this step to the value of the derived step 683 * @param executor executor to run the function in 684 * @return the derived step 685 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from this 686 * one, or if this {@code ClosingFuture} has already been {@linkplain #finishToFuture() 687 * finished} 688 */ 689 public <U extends @Nullable Object> ClosingFuture<U> transform( 690 final ClosingFunction<? super V, U> function, Executor executor) { 691 checkNotNull(function); 692 AsyncFunction<V, U> applyFunction = 693 new AsyncFunction<V, U>() { 694 @Override 695 public ListenableFuture<U> apply(V input) throws Exception { 696 return closeables.applyClosingFunction(function, input); 697 } 698 699 @Override 700 public String toString() { 701 return function.toString(); 702 } 703 }; 704 // TODO(dpb): Switch to future.transformSync when that exists (passing a throwing function). 705 return derive(future.transformAsync(applyFunction, executor)); 706 } 707 708 /** 709 * Returns a new {@code ClosingFuture} pipeline step derived from this one by applying a function 710 * that returns a {@code ClosingFuture} to its value. The function can use a {@link 711 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 712 * captured by the returned {@link ClosingFuture}). 713 * 714 * <p>If this {@code ClosingFuture} succeeds, the derived one will be equivalent to the one 715 * returned by the function. 716 * 717 * <p>If this {@code ClosingFuture} fails, the function will not be called, and the derived {@code 718 * ClosingFuture} will be equivalent to this one. 719 * 720 * <p>If the function throws an exception, that exception is used as the result of the derived 721 * {@code ClosingFuture}. But if the exception is thrown after the function creates a {@code 722 * ClosingFuture}, then none of the closeable objects in that {@code ClosingFuture} will be 723 * closed. 724 * 725 * <p>Usage guidelines for this method: 726 * 727 * <ul> 728 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 729 * {@code ClosingFuture}. If possible, prefer calling {@link #transform(ClosingFunction, 730 * Executor)} instead, with a function that returns the next value directly. 731 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 732 * for every closeable object this step creates in order to capture it for later closing. 733 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 734 * ClosingFuture} call {@link #from(ListenableFuture)}. 735 * <li>In case this step doesn't create new closeables, you can adapt an API that returns a 736 * {@link ListenableFuture} to return a {@code ClosingFuture} by wrapping it with a call to 737 * {@link #withoutCloser(AsyncFunction)} 738 * </ul> 739 * 740 * <p>Example usage: 741 * 742 * <pre>{@code 743 * // Result.getRowsClosingFuture() returns a ClosingFuture. 744 * ClosingFuture<List<Row>> rowsFuture = 745 * queryFuture.transformAsync((closer, result) -> result.getRowsClosingFuture(), executor); 746 * 747 * // Result.writeRowsToOutputStreamFuture() returns a ListenableFuture that resolves to the 748 * // number of written rows. openOutputFile() returns a FileOutputStream (which implements 749 * // Closeable). 750 * ClosingFuture<Integer> rowsFuture2 = 751 * queryFuture.transformAsync( 752 * (closer, result) -> { 753 * FileOutputStream fos = closer.eventuallyClose(openOutputFile(), closingExecutor); 754 * return ClosingFuture.from(result.writeRowsToOutputStreamFuture(fos)); 755 * }, 756 * executor); 757 * 758 * // Result.getRowsFuture() returns a ListenableFuture (no new closeables are created). 759 * ClosingFuture<List<Row>> rowsFuture3 = 760 * queryFuture.transformAsync(withoutCloser(Result::getRowsFuture), executor); 761 * 762 * }</pre> 763 * 764 * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See 765 * the discussion in the {@link ListenableFuture#addListener} documentation. All its warnings 766 * about heavyweight listeners are also applicable to heavyweight functions passed to this method. 767 * (Specifically, {@code directExecutor} functions should avoid heavyweight operations inside 768 * {@code AsyncClosingFunction.apply}. Any heavyweight operations should occur in other threads 769 * responsible for completing the returned {@code ClosingFuture}.) 770 * 771 * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link 772 * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on 773 * the original {@code ClosingFuture} instance. 774 * 775 * @param function transforms the value of this step to a {@code ClosingFuture} with the value of 776 * the derived step 777 * @param executor executor to run the function in 778 * @return the derived step 779 * @throws IllegalStateException if a {@code ClosingFuture} has already been derived from this 780 * one, or if this {@code ClosingFuture} has already been {@linkplain #finishToFuture() 781 * finished} 782 */ 783 public <U extends @Nullable Object> ClosingFuture<U> transformAsync( 784 final AsyncClosingFunction<? super V, U> function, Executor executor) { 785 checkNotNull(function); 786 AsyncFunction<V, U> applyFunction = 787 new AsyncFunction<V, U>() { 788 @Override 789 public ListenableFuture<U> apply(V input) throws Exception { 790 return closeables.applyAsyncClosingFunction(function, input); 791 } 792 793 @Override 794 public String toString() { 795 return function.toString(); 796 } 797 }; 798 return derive(future.transformAsync(applyFunction, executor)); 799 } 800 801 /** 802 * Returns an {@link AsyncClosingFunction} that applies an {@link AsyncFunction} to an input, 803 * ignoring the DeferredCloser and returning a {@code ClosingFuture} derived from the returned 804 * {@link ListenableFuture}. 805 * 806 * <p>Use this method to pass a transformation to {@link #transformAsync(AsyncClosingFunction, 807 * Executor)} or to {@link #catchingAsync(Class, AsyncClosingFunction, Executor)} as long as it 808 * meets these conditions: 809 * 810 * <ul> 811 * <li>It does not need to capture any {@link Closeable} objects by calling {@link 812 * DeferredCloser#eventuallyClose(Object, Executor)}. 813 * <li>It returns a {@link ListenableFuture}. 814 * </ul> 815 * 816 * <p>Example usage: 817 * 818 * <pre>{@code 819 * // Result.getRowsFuture() returns a ListenableFuture. 820 * ClosingFuture<List<Row>> rowsFuture = 821 * queryFuture.transformAsync(withoutCloser(Result::getRowsFuture), executor); 822 * }</pre> 823 * 824 * @param function transforms the value of a {@code ClosingFuture} step to a {@link 825 * ListenableFuture} with the value of a derived step 826 */ 827 public static <V extends @Nullable Object, U extends @Nullable Object> 828 AsyncClosingFunction<V, U> withoutCloser(final AsyncFunction<V, U> function) { 829 checkNotNull(function); 830 return new AsyncClosingFunction<V, U>() { 831 @Override 832 public ClosingFuture<U> apply(DeferredCloser closer, V input) throws Exception { 833 return ClosingFuture.from(function.apply(input)); 834 } 835 }; 836 } 837 838 /** 839 * Returns a new {@code ClosingFuture} pipeline step derived from this one by applying a function 840 * to its exception if it is an instance of a given exception type. The function can use a {@link 841 * DeferredCloser} to capture objects to be closed when the pipeline is done. 842 * 843 * <p>If this {@code ClosingFuture} succeeds or fails with a different exception type, the 844 * function will not be called, and the derived {@code ClosingFuture} will be equivalent to this 845 * one. 846 * 847 * <p>If the function throws an exception, that exception is used as the result of the derived 848 * {@code ClosingFuture}. 849 * 850 * <p>Example usage: 851 * 852 * <pre>{@code 853 * ClosingFuture<QueryResult> queryFuture = 854 * queryFuture.catching( 855 * QueryException.class, (closer, x) -> Query.emptyQueryResult(), executor); 856 * }</pre> 857 * 858 * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See 859 * the discussion in the {@link ListenableFuture#addListener} documentation. All its warnings 860 * about heavyweight listeners are also applicable to heavyweight functions passed to this method. 861 * 862 * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link 863 * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on 864 * the original {@code ClosingFuture} instance. 865 * 866 * @param exceptionType the exception type that triggers use of {@code fallback}. The exception 867 * type is matched against this step's exception. "This step's exception" means the cause of 868 * the {@link ExecutionException} thrown by {@link Future#get()} on the {@link Future} 869 * underlying this step or, if {@code get()} throws a different kind of exception, that 870 * exception itself. To avoid hiding bugs and other unrecoverable errors, callers should 871 * prefer more specific types, avoiding {@code Throwable.class} in particular. 872 * @param fallback the function to be called if this step fails with the expected exception type. 873 * The function's argument is this step's exception. "This step's exception" means the cause 874 * of the {@link ExecutionException} thrown by {@link Future#get()} on the {@link Future} 875 * underlying this step or, if {@code get()} throws a different kind of exception, that 876 * exception itself. 877 * @param executor the executor that runs {@code fallback} if the input fails 878 */ 879 public <X extends Throwable> ClosingFuture<V> catching( 880 Class<X> exceptionType, ClosingFunction<? super X, ? extends V> fallback, Executor executor) { 881 return catchingMoreGeneric(exceptionType, fallback, executor); 882 } 883 884 // Avoids generic type capture inconsistency problems where |? extends V| is incompatible with V. 885 private <X extends Throwable, W extends V> ClosingFuture<V> catchingMoreGeneric( 886 Class<X> exceptionType, final ClosingFunction<? super X, W> fallback, Executor executor) { 887 checkNotNull(fallback); 888 AsyncFunction<X, W> applyFallback = 889 new AsyncFunction<X, W>() { 890 @Override 891 public ListenableFuture<W> apply(X exception) throws Exception { 892 return closeables.applyClosingFunction(fallback, exception); 893 } 894 895 @Override 896 public String toString() { 897 return fallback.toString(); 898 } 899 }; 900 // TODO(dpb): Switch to future.catchingSync when that exists (passing a throwing function). 901 return derive(future.catchingAsync(exceptionType, applyFallback, executor)); 902 } 903 904 /** 905 * Returns a new {@code ClosingFuture} pipeline step derived from this one by applying a function 906 * that returns a {@code ClosingFuture} to its exception if it is an instance of a given exception 907 * type. The function can use a {@link DeferredCloser} to capture objects to be closed when the 908 * pipeline is done (other than those captured by the returned {@link ClosingFuture}). 909 * 910 * <p>If this {@code ClosingFuture} fails with an exception of the given type, the derived {@code 911 * ClosingFuture} will be equivalent to the one returned by the function. 912 * 913 * <p>If this {@code ClosingFuture} succeeds or fails with a different exception type, the 914 * function will not be called, and the derived {@code ClosingFuture} will be equivalent to this 915 * one. 916 * 917 * <p>If the function throws an exception, that exception is used as the result of the derived 918 * {@code ClosingFuture}. But if the exception is thrown after the function creates a {@code 919 * ClosingFuture}, then none of the closeable objects in that {@code ClosingFuture} will be 920 * closed. 921 * 922 * <p>Usage guidelines for this method: 923 * 924 * <ul> 925 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 926 * {@code ClosingFuture}. If possible, prefer calling {@link #catching(Class, 927 * ClosingFunction, Executor)} instead, with a function that returns the next value 928 * directly. 929 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 930 * for every closeable object this step creates in order to capture it for later closing. 931 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 932 * ClosingFuture} call {@link #from(ListenableFuture)}. 933 * <li>In case this step doesn't create new closeables, you can adapt an API that returns a 934 * {@link ListenableFuture} to return a {@code ClosingFuture} by wrapping it with a call to 935 * {@link #withoutCloser(AsyncFunction)} 936 * </ul> 937 * 938 * <p>Example usage: 939 * 940 * <pre>{@code 941 * // Fall back to a secondary input stream in case of IOException. 942 * ClosingFuture<InputStream> inputFuture = 943 * firstInputFuture.catchingAsync( 944 * IOException.class, (closer, x) -> secondaryInputStreamClosingFuture(), executor); 945 * } 946 * }</pre> 947 * 948 * <p>When selecting an executor, note that {@code directExecutor} is dangerous in some cases. See 949 * the discussion in the {@link ListenableFuture#addListener} documentation. All its warnings 950 * about heavyweight listeners are also applicable to heavyweight functions passed to this method. 951 * (Specifically, {@code directExecutor} functions should avoid heavyweight operations inside 952 * {@code AsyncClosingFunction.apply}. Any heavyweight operations should occur in other threads 953 * responsible for completing the returned {@code ClosingFuture}.) 954 * 955 * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link 956 * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on 957 * the original {@code ClosingFuture} instance. 958 * 959 * @param exceptionType the exception type that triggers use of {@code fallback}. The exception 960 * type is matched against this step's exception. "This step's exception" means the cause of 961 * the {@link ExecutionException} thrown by {@link Future#get()} on the {@link Future} 962 * underlying this step or, if {@code get()} throws a different kind of exception, that 963 * exception itself. To avoid hiding bugs and other unrecoverable errors, callers should 964 * prefer more specific types, avoiding {@code Throwable.class} in particular. 965 * @param fallback the function to be called if this step fails with the expected exception type. 966 * The function's argument is this step's exception. "This step's exception" means the cause 967 * of the {@link ExecutionException} thrown by {@link Future#get()} on the {@link Future} 968 * underlying this step or, if {@code get()} throws a different kind of exception, that 969 * exception itself. 970 * @param executor the executor that runs {@code fallback} if the input fails 971 */ 972 // TODO(dpb): Should this do something special if the function throws CancellationException or 973 // ExecutionException? 974 public <X extends Throwable> ClosingFuture<V> catchingAsync( 975 Class<X> exceptionType, 976 AsyncClosingFunction<? super X, ? extends V> fallback, 977 Executor executor) { 978 return catchingAsyncMoreGeneric(exceptionType, fallback, executor); 979 } 980 981 // Avoids generic type capture inconsistency problems where |? extends V| is incompatible with V. 982 private <X extends Throwable, W extends V> ClosingFuture<V> catchingAsyncMoreGeneric( 983 Class<X> exceptionType, 984 final AsyncClosingFunction<? super X, W> fallback, 985 Executor executor) { 986 checkNotNull(fallback); 987 AsyncFunction<X, W> asyncFunction = 988 new AsyncFunction<X, W>() { 989 @Override 990 public ListenableFuture<W> apply(X exception) throws Exception { 991 return closeables.applyAsyncClosingFunction(fallback, exception); 992 } 993 994 @Override 995 public String toString() { 996 return fallback.toString(); 997 } 998 }; 999 return derive(future.catchingAsync(exceptionType, asyncFunction, executor)); 1000 } 1001 1002 /** 1003 * Marks this step as the last step in the {@code ClosingFuture} pipeline. 1004 * 1005 * <p>The returned {@link Future} is completed when the pipeline's computation completes, or when 1006 * the pipeline is cancelled. 1007 * 1008 * <p>All objects the pipeline has captured for closing will begin to be closed asynchronously 1009 * <b>after</b> the returned {@code Future} is done: the future completes before closing starts, 1010 * rather than once it has finished. 1011 * 1012 * <p>After calling this method, you may not call {@link 1013 * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, this method, or any other 1014 * derivation method on the original {@code ClosingFuture} instance. 1015 * 1016 * @return a {@link Future} that represents the final value or exception of the pipeline 1017 */ 1018 public FluentFuture<V> finishToFuture() { 1019 if (compareAndUpdateState(OPEN, WILL_CLOSE)) { 1020 logger.get().log(FINER, "will close {0}", this); 1021 future.addListener( 1022 new Runnable() { 1023 @Override 1024 public void run() { 1025 checkAndUpdateState(WILL_CLOSE, CLOSING); 1026 close(); 1027 checkAndUpdateState(CLOSING, CLOSED); 1028 } 1029 }, 1030 directExecutor()); 1031 } else { 1032 switch (state.get()) { 1033 case SUBSUMED: 1034 throw new IllegalStateException( 1035 "Cannot call finishToFuture() after deriving another step"); 1036 1037 case WILL_CREATE_VALUE_AND_CLOSER: 1038 throw new IllegalStateException( 1039 "Cannot call finishToFuture() after calling finishToValueAndCloser()"); 1040 1041 case WILL_CLOSE: 1042 case CLOSING: 1043 case CLOSED: 1044 throw new IllegalStateException("Cannot call finishToFuture() twice"); 1045 1046 case OPEN: 1047 throw new AssertionError(); 1048 } 1049 } 1050 return future; 1051 } 1052 1053 /** 1054 * Marks this step as the last step in the {@code ClosingFuture} pipeline. When this step is done, 1055 * {@code receiver} will be called with an object that contains the result of the operation. The 1056 * receiver can store the {@link ValueAndCloser} outside the receiver for later synchronous use. 1057 * 1058 * <p>After calling this method, you may not call {@link #finishToFuture()}, this method again, or 1059 * any other derivation method on the original {@code ClosingFuture} instance. 1060 * 1061 * @param consumer a callback whose method will be called (using {@code executor}) when this 1062 * operation is done 1063 */ 1064 public void finishToValueAndCloser( 1065 final ValueAndCloserConsumer<? super V> consumer, Executor executor) { 1066 checkNotNull(consumer); 1067 if (!compareAndUpdateState(OPEN, WILL_CREATE_VALUE_AND_CLOSER)) { 1068 switch (state.get()) { 1069 case SUBSUMED: 1070 throw new IllegalStateException( 1071 "Cannot call finishToValueAndCloser() after deriving another step"); 1072 1073 case WILL_CLOSE: 1074 case CLOSING: 1075 case CLOSED: 1076 throw new IllegalStateException( 1077 "Cannot call finishToValueAndCloser() after calling finishToFuture()"); 1078 1079 case WILL_CREATE_VALUE_AND_CLOSER: 1080 throw new IllegalStateException("Cannot call finishToValueAndCloser() twice"); 1081 1082 case OPEN: 1083 break; 1084 } 1085 throw new AssertionError(state); 1086 } 1087 future.addListener( 1088 new Runnable() { 1089 @Override 1090 public void run() { 1091 provideValueAndCloser(consumer, ClosingFuture.this); 1092 } 1093 }, 1094 executor); 1095 } 1096 1097 private static <C extends @Nullable Object, V extends C> void provideValueAndCloser( 1098 ValueAndCloserConsumer<C> consumer, ClosingFuture<V> closingFuture) { 1099 consumer.accept(new ValueAndCloser<C>(closingFuture)); 1100 } 1101 1102 /** 1103 * Attempts to cancel execution of this step. This attempt will fail if the step has already 1104 * completed, has already been cancelled, or could not be cancelled for some other reason. If 1105 * successful, and this step has not started when {@code cancel} is called, this step should never 1106 * run. 1107 * 1108 * <p>If successful, causes the objects captured by this step (if already started) and its input 1109 * step(s) for later closing to be closed on their respective {@link Executor}s. If any such calls 1110 * specified {@link MoreExecutors#directExecutor()}, those objects will be closed synchronously. 1111 * 1112 * @param mayInterruptIfRunning {@code true} if the thread executing this task should be 1113 * interrupted; otherwise, in-progress tasks are allowed to complete, but the step will be 1114 * cancelled regardless 1115 * @return {@code false} if the step could not be cancelled, typically because it has already 1116 * completed normally; {@code true} otherwise 1117 */ 1118 @CanIgnoreReturnValue 1119 @SuppressWarnings("Interruption") // We are propagating an interrupt from a caller. 1120 public boolean cancel(boolean mayInterruptIfRunning) { 1121 logger.get().log(FINER, "cancelling {0}", this); 1122 boolean cancelled = future.cancel(mayInterruptIfRunning); 1123 if (cancelled) { 1124 close(); 1125 } 1126 return cancelled; 1127 } 1128 1129 private void close() { 1130 logger.get().log(FINER, "closing {0}", this); 1131 closeables.close(); 1132 } 1133 1134 private <U extends @Nullable Object> ClosingFuture<U> derive(FluentFuture<U> future) { 1135 ClosingFuture<U> derived = new ClosingFuture<>(future); 1136 becomeSubsumedInto(derived.closeables); 1137 return derived; 1138 } 1139 1140 private void becomeSubsumedInto(CloseableList otherCloseables) { 1141 checkAndUpdateState(OPEN, SUBSUMED); 1142 otherCloseables.add(closeables, directExecutor()); 1143 } 1144 1145 /** 1146 * An object that can return the value of the {@link ClosingFuture}s that are passed to {@link 1147 * #whenAllComplete(Iterable)} or {@link #whenAllSucceed(Iterable)}. 1148 * 1149 * <p>Only for use by a {@link CombiningCallable} or {@link AsyncCombiningCallable} object. 1150 */ 1151 public static final class Peeker { 1152 private final ImmutableList<ClosingFuture<?>> futures; 1153 private volatile boolean beingCalled; 1154 1155 private Peeker(ImmutableList<ClosingFuture<?>> futures) { 1156 this.futures = checkNotNull(futures); 1157 } 1158 1159 /** 1160 * Returns the value of {@code closingFuture}. 1161 * 1162 * @throws ExecutionException if {@code closingFuture} is a failed step 1163 * @throws CancellationException if the {@code closingFuture}'s future was cancelled 1164 * @throws IllegalArgumentException if {@code closingFuture} is not one of the futures passed to 1165 * {@link #whenAllComplete(Iterable)} or {@link #whenAllComplete(Iterable)} 1166 * @throws IllegalStateException if called outside of a call to {@link 1167 * CombiningCallable#call(DeferredCloser, Peeker)} or {@link 1168 * AsyncCombiningCallable#call(DeferredCloser, Peeker)} 1169 */ 1170 @ParametricNullness 1171 public final <D extends @Nullable Object> D getDone(ClosingFuture<D> closingFuture) 1172 throws ExecutionException { 1173 checkState(beingCalled); 1174 checkArgument(futures.contains(closingFuture)); 1175 return Futures.getDone(closingFuture.future); 1176 } 1177 1178 @ParametricNullness 1179 private <V extends @Nullable Object> V call( 1180 CombiningCallable<V> combiner, CloseableList closeables) throws Exception { 1181 beingCalled = true; 1182 CloseableList newCloseables = new CloseableList(); 1183 try { 1184 return combiner.call(newCloseables.closer, this); 1185 } finally { 1186 closeables.add(newCloseables, directExecutor()); 1187 beingCalled = false; 1188 } 1189 } 1190 1191 private <V extends @Nullable Object> FluentFuture<V> callAsync( 1192 AsyncCombiningCallable<V> combiner, CloseableList closeables) throws Exception { 1193 beingCalled = true; 1194 CloseableList newCloseables = new CloseableList(); 1195 try { 1196 ClosingFuture<V> closingFuture = combiner.call(newCloseables.closer, this); 1197 closingFuture.becomeSubsumedInto(closeables); 1198 return closingFuture.future; 1199 } finally { 1200 closeables.add(newCloseables, directExecutor()); 1201 beingCalled = false; 1202 } 1203 } 1204 } 1205 1206 /** 1207 * A builder of a {@link ClosingFuture} step that is derived from more than one input step. 1208 * 1209 * <p>See {@link #whenAllComplete(Iterable)} and {@link #whenAllSucceed(Iterable)} for how to 1210 * instantiate this class. 1211 * 1212 * <p>Example: 1213 * 1214 * <pre>{@code 1215 * final ClosingFuture<BufferedReader> file1ReaderFuture = ...; 1216 * final ClosingFuture<BufferedReader> file2ReaderFuture = ...; 1217 * ListenableFuture<Integer> numberOfDifferentLines = 1218 * ClosingFuture.whenAllSucceed(file1ReaderFuture, file2ReaderFuture) 1219 * .call( 1220 * (closer, peeker) -> { 1221 * BufferedReader file1Reader = peeker.getDone(file1ReaderFuture); 1222 * BufferedReader file2Reader = peeker.getDone(file2ReaderFuture); 1223 * return countDifferentLines(file1Reader, file2Reader); 1224 * }, 1225 * executor) 1226 * .closing(executor); 1227 * }</pre> 1228 */ 1229 @DoNotMock("Use ClosingFuture.whenAllSucceed() or .whenAllComplete() instead.") 1230 public static class Combiner { 1231 1232 private final CloseableList closeables = new CloseableList(); 1233 1234 /** 1235 * An operation that returns a result and may throw an exception. 1236 * 1237 * @param <V> the type of the result 1238 */ 1239 @FunctionalInterface 1240 public interface CombiningCallable<V extends @Nullable Object> { 1241 /** 1242 * Computes a result, or throws an exception if unable to do so. 1243 * 1244 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1245 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1246 * (but not before this method completes), even if this method throws or the pipeline is 1247 * cancelled. 1248 * 1249 * @param peeker used to get the value of any of the input futures 1250 */ 1251 @ParametricNullness 1252 V call(DeferredCloser closer, Peeker peeker) throws Exception; 1253 } 1254 1255 /** 1256 * An operation that returns a {@link ClosingFuture} result and may throw an exception. 1257 * 1258 * @param <V> the type of the result 1259 */ 1260 @FunctionalInterface 1261 public interface AsyncCombiningCallable<V extends @Nullable Object> { 1262 /** 1263 * Computes a {@link ClosingFuture} result, or throws an exception if unable to do so. 1264 * 1265 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1266 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1267 * (but not before this method completes), even if this method throws or the pipeline is 1268 * cancelled. 1269 * 1270 * @param peeker used to get the value of any of the input futures 1271 */ 1272 ClosingFuture<V> call(DeferredCloser closer, Peeker peeker) throws Exception; 1273 } 1274 1275 private final boolean allMustSucceed; 1276 protected final ImmutableList<ClosingFuture<?>> inputs; 1277 1278 private Combiner(boolean allMustSucceed, Iterable<? extends ClosingFuture<?>> inputs) { 1279 this.allMustSucceed = allMustSucceed; 1280 this.inputs = ImmutableList.copyOf(inputs); 1281 for (ClosingFuture<?> input : inputs) { 1282 input.becomeSubsumedInto(closeables); 1283 } 1284 } 1285 1286 /** 1287 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1288 * combining function to their values. The function can use a {@link DeferredCloser} to capture 1289 * objects to be closed when the pipeline is done. 1290 * 1291 * <p>If this combiner was returned by a {@link #whenAllSucceed} method and any of the inputs 1292 * fail, so will the returned step. 1293 * 1294 * <p>If the combiningCallable throws a {@code CancellationException}, the pipeline will be 1295 * cancelled. 1296 * 1297 * <p>If the combiningCallable throws an {@code ExecutionException}, the cause of the thrown 1298 * {@code ExecutionException} will be extracted and used as the failure of the derived step. 1299 */ 1300 public <V extends @Nullable Object> ClosingFuture<V> call( 1301 final CombiningCallable<V> combiningCallable, Executor executor) { 1302 Callable<V> callable = 1303 new Callable<V>() { 1304 @Override 1305 @ParametricNullness 1306 public V call() throws Exception { 1307 return new Peeker(inputs).call(combiningCallable, closeables); 1308 } 1309 1310 @Override 1311 public String toString() { 1312 return combiningCallable.toString(); 1313 } 1314 }; 1315 ClosingFuture<V> derived = new ClosingFuture<>(futureCombiner().call(callable, executor)); 1316 derived.closeables.add(closeables, directExecutor()); 1317 return derived; 1318 } 1319 1320 /** 1321 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1322 * {@code ClosingFuture}-returning function to their values. The function can use a {@link 1323 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 1324 * captured by the returned {@link ClosingFuture}). 1325 * 1326 * <p>If this combiner was returned by a {@link #whenAllSucceed} method and any of the inputs 1327 * fail, so will the returned step. 1328 * 1329 * <p>If the combiningCallable throws a {@code CancellationException}, the pipeline will be 1330 * cancelled. 1331 * 1332 * <p>If the combiningCallable throws an {@code ExecutionException}, the cause of the thrown 1333 * {@code ExecutionException} will be extracted and used as the failure of the derived step. 1334 * 1335 * <p>If the combiningCallable throws any other exception, it will be used as the failure of the 1336 * derived step. 1337 * 1338 * <p>If an exception is thrown after the combiningCallable creates a {@code ClosingFuture}, 1339 * then none of the closeable objects in that {@code ClosingFuture} will be closed. 1340 * 1341 * <p>Usage guidelines for this method: 1342 * 1343 * <ul> 1344 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 1345 * {@code ClosingFuture}. If possible, prefer calling {@link #call(CombiningCallable, 1346 * Executor)} instead, with a function that returns the next value directly. 1347 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 1348 * for every closeable object this step creates in order to capture it for later closing. 1349 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 1350 * ClosingFuture} call {@link #from(ListenableFuture)}. 1351 * </ul> 1352 * 1353 * <p>The same warnings about doing heavyweight operations within {@link 1354 * ClosingFuture#transformAsync(AsyncClosingFunction, Executor)} apply here. 1355 */ 1356 public <V extends @Nullable Object> ClosingFuture<V> callAsync( 1357 final AsyncCombiningCallable<V> combiningCallable, Executor executor) { 1358 AsyncCallable<V> asyncCallable = 1359 new AsyncCallable<V>() { 1360 @Override 1361 public ListenableFuture<V> call() throws Exception { 1362 return new Peeker(inputs).callAsync(combiningCallable, closeables); 1363 } 1364 1365 @Override 1366 public String toString() { 1367 return combiningCallable.toString(); 1368 } 1369 }; 1370 ClosingFuture<V> derived = 1371 new ClosingFuture<>(futureCombiner().callAsync(asyncCallable, executor)); 1372 derived.closeables.add(closeables, directExecutor()); 1373 return derived; 1374 } 1375 1376 private FutureCombiner<@Nullable Object> futureCombiner() { 1377 return allMustSucceed 1378 ? Futures.whenAllSucceed(inputFutures()) 1379 : Futures.whenAllComplete(inputFutures()); 1380 } 1381 1382 private ImmutableList<FluentFuture<?>> inputFutures() { 1383 return FluentIterable.from(inputs) 1384 .<FluentFuture<?>>transform(future -> future.future) 1385 .toList(); 1386 } 1387 } 1388 1389 /** 1390 * A generic {@link Combiner} that lets you use a lambda or method reference to combine two {@link 1391 * ClosingFuture}s. Use {@link #whenAllSucceed(ClosingFuture, ClosingFuture)} to start this 1392 * combination. 1393 * 1394 * @param <V1> the type returned by the first future 1395 * @param <V2> the type returned by the second future 1396 */ 1397 public static final class Combiner2<V1 extends @Nullable Object, V2 extends @Nullable Object> 1398 extends Combiner { 1399 1400 /** 1401 * A function that returns a value when applied to the values of the two futures passed to 1402 * {@link #whenAllSucceed(ClosingFuture, ClosingFuture)}. 1403 * 1404 * @param <V1> the type returned by the first future 1405 * @param <V2> the type returned by the second future 1406 * @param <U> the type returned by the function 1407 */ 1408 @FunctionalInterface 1409 public interface ClosingFunction2< 1410 V1 extends @Nullable Object, V2 extends @Nullable Object, U extends @Nullable Object> { 1411 1412 /** 1413 * Applies this function to two inputs, or throws an exception if unable to do so. 1414 * 1415 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1416 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1417 * (but not before this method completes), even if this method throws or the pipeline is 1418 * cancelled. 1419 */ 1420 @ParametricNullness 1421 U apply(DeferredCloser closer, @ParametricNullness V1 value1, @ParametricNullness V2 value2) 1422 throws Exception; 1423 } 1424 1425 /** 1426 * A function that returns a {@link ClosingFuture} when applied to the values of the two futures 1427 * passed to {@link #whenAllSucceed(ClosingFuture, ClosingFuture)}. 1428 * 1429 * @param <V1> the type returned by the first future 1430 * @param <V2> the type returned by the second future 1431 * @param <U> the type returned by the function 1432 */ 1433 @FunctionalInterface 1434 public interface AsyncClosingFunction2< 1435 V1 extends @Nullable Object, V2 extends @Nullable Object, U extends @Nullable Object> { 1436 1437 /** 1438 * Applies this function to two inputs, or throws an exception if unable to do so. 1439 * 1440 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1441 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1442 * (but not before this method completes), even if this method throws or the pipeline is 1443 * cancelled. 1444 */ 1445 ClosingFuture<U> apply( 1446 DeferredCloser closer, @ParametricNullness V1 value1, @ParametricNullness V2 value2) 1447 throws Exception; 1448 } 1449 1450 private final ClosingFuture<V1> future1; 1451 private final ClosingFuture<V2> future2; 1452 1453 private Combiner2(ClosingFuture<V1> future1, ClosingFuture<V2> future2) { 1454 super(true, ImmutableList.of(future1, future2)); 1455 this.future1 = future1; 1456 this.future2 = future2; 1457 } 1458 1459 /** 1460 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1461 * combining function to their values. The function can use a {@link DeferredCloser} to capture 1462 * objects to be closed when the pipeline is done. 1463 * 1464 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture)} and 1465 * any of the inputs fail, so will the returned step. 1466 * 1467 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1468 * 1469 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1470 * ExecutionException} will be extracted and used as the failure of the derived step. 1471 */ 1472 public <U extends @Nullable Object> ClosingFuture<U> call( 1473 final ClosingFunction2<V1, V2, U> function, Executor executor) { 1474 return call( 1475 new CombiningCallable<U>() { 1476 @Override 1477 @ParametricNullness 1478 public U call(DeferredCloser closer, Peeker peeker) throws Exception { 1479 return function.apply(closer, peeker.getDone(future1), peeker.getDone(future2)); 1480 } 1481 1482 @Override 1483 public String toString() { 1484 return function.toString(); 1485 } 1486 }, 1487 executor); 1488 } 1489 1490 /** 1491 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1492 * {@code ClosingFuture}-returning function to their values. The function can use a {@link 1493 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 1494 * captured by the returned {@link ClosingFuture}). 1495 * 1496 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture)} and 1497 * any of the inputs fail, so will the returned step. 1498 * 1499 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1500 * 1501 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1502 * ExecutionException} will be extracted and used as the failure of the derived step. 1503 * 1504 * <p>If the function throws any other exception, it will be used as the failure of the derived 1505 * step. 1506 * 1507 * <p>If an exception is thrown after the function creates a {@code ClosingFuture}, then none of 1508 * the closeable objects in that {@code ClosingFuture} will be closed. 1509 * 1510 * <p>Usage guidelines for this method: 1511 * 1512 * <ul> 1513 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 1514 * {@code ClosingFuture}. If possible, prefer calling {@link #call(CombiningCallable, 1515 * Executor)} instead, with a function that returns the next value directly. 1516 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 1517 * for every closeable object this step creates in order to capture it for later closing. 1518 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 1519 * ClosingFuture} call {@link #from(ListenableFuture)}. 1520 * </ul> 1521 * 1522 * <p>The same warnings about doing heavyweight operations within {@link 1523 * ClosingFuture#transformAsync(AsyncClosingFunction, Executor)} apply here. 1524 */ 1525 public <U extends @Nullable Object> ClosingFuture<U> callAsync( 1526 final AsyncClosingFunction2<V1, V2, U> function, Executor executor) { 1527 return callAsync( 1528 new AsyncCombiningCallable<U>() { 1529 @Override 1530 public ClosingFuture<U> call(DeferredCloser closer, Peeker peeker) throws Exception { 1531 return function.apply(closer, peeker.getDone(future1), peeker.getDone(future2)); 1532 } 1533 1534 @Override 1535 public String toString() { 1536 return function.toString(); 1537 } 1538 }, 1539 executor); 1540 } 1541 } 1542 1543 /** 1544 * A generic {@link Combiner} that lets you use a lambda or method reference to combine three 1545 * {@link ClosingFuture}s. Use {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 1546 * ClosingFuture)} to start this combination. 1547 * 1548 * @param <V1> the type returned by the first future 1549 * @param <V2> the type returned by the second future 1550 * @param <V3> the type returned by the third future 1551 */ 1552 public static final class Combiner3< 1553 V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object> 1554 extends Combiner { 1555 /** 1556 * A function that returns a value when applied to the values of the three futures passed to 1557 * {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture)}. 1558 * 1559 * @param <V1> the type returned by the first future 1560 * @param <V2> the type returned by the second future 1561 * @param <V3> the type returned by the third future 1562 * @param <U> the type returned by the function 1563 */ 1564 @FunctionalInterface 1565 public interface ClosingFunction3< 1566 V1 extends @Nullable Object, 1567 V2 extends @Nullable Object, 1568 V3 extends @Nullable Object, 1569 U extends @Nullable Object> { 1570 /** 1571 * Applies this function to three inputs, or throws an exception if unable to do so. 1572 * 1573 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1574 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1575 * (but not before this method completes), even if this method throws or the pipeline is 1576 * cancelled. 1577 */ 1578 @ParametricNullness 1579 U apply( 1580 DeferredCloser closer, 1581 @ParametricNullness V1 value1, 1582 @ParametricNullness V2 value2, 1583 @ParametricNullness V3 value3) 1584 throws Exception; 1585 } 1586 1587 /** 1588 * A function that returns a {@link ClosingFuture} when applied to the values of the three 1589 * futures passed to {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture)}. 1590 * 1591 * @param <V1> the type returned by the first future 1592 * @param <V2> the type returned by the second future 1593 * @param <V3> the type returned by the third future 1594 * @param <U> the type returned by the function 1595 */ 1596 @FunctionalInterface 1597 public interface AsyncClosingFunction3< 1598 V1 extends @Nullable Object, 1599 V2 extends @Nullable Object, 1600 V3 extends @Nullable Object, 1601 U extends @Nullable Object> { 1602 /** 1603 * Applies this function to three inputs, or throws an exception if unable to do so. 1604 * 1605 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1606 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1607 * (but not before this method completes), even if this method throws or the pipeline is 1608 * cancelled. 1609 */ 1610 ClosingFuture<U> apply( 1611 DeferredCloser closer, 1612 @ParametricNullness V1 value1, 1613 @ParametricNullness V2 value2, 1614 @ParametricNullness V3 value3) 1615 throws Exception; 1616 } 1617 1618 private final ClosingFuture<V1> future1; 1619 private final ClosingFuture<V2> future2; 1620 private final ClosingFuture<V3> future3; 1621 1622 private Combiner3( 1623 ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3) { 1624 super(true, ImmutableList.of(future1, future2, future3)); 1625 this.future1 = future1; 1626 this.future2 = future2; 1627 this.future3 = future3; 1628 } 1629 1630 /** 1631 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1632 * combining function to their values. The function can use a {@link DeferredCloser} to capture 1633 * objects to be closed when the pipeline is done. 1634 * 1635 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 1636 * ClosingFuture)} and any of the inputs fail, so will the returned step. 1637 * 1638 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1639 * 1640 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1641 * ExecutionException} will be extracted and used as the failure of the derived step. 1642 */ 1643 public <U extends @Nullable Object> ClosingFuture<U> call( 1644 final ClosingFunction3<V1, V2, V3, U> function, Executor executor) { 1645 return call( 1646 new CombiningCallable<U>() { 1647 @Override 1648 @ParametricNullness 1649 public U call(DeferredCloser closer, Peeker peeker) throws Exception { 1650 return function.apply( 1651 closer, 1652 peeker.getDone(future1), 1653 peeker.getDone(future2), 1654 peeker.getDone(future3)); 1655 } 1656 1657 @Override 1658 public String toString() { 1659 return function.toString(); 1660 } 1661 }, 1662 executor); 1663 } 1664 1665 /** 1666 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1667 * {@code ClosingFuture}-returning function to their values. The function can use a {@link 1668 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 1669 * captured by the returned {@link ClosingFuture}). 1670 * 1671 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 1672 * ClosingFuture)} and any of the inputs fail, so will the returned step. 1673 * 1674 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1675 * 1676 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1677 * ExecutionException} will be extracted and used as the failure of the derived step. 1678 * 1679 * <p>If the function throws any other exception, it will be used as the failure of the derived 1680 * step. 1681 * 1682 * <p>If an exception is thrown after the function creates a {@code ClosingFuture}, then none of 1683 * the closeable objects in that {@code ClosingFuture} will be closed. 1684 * 1685 * <p>Usage guidelines for this method: 1686 * 1687 * <ul> 1688 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 1689 * {@code ClosingFuture}. If possible, prefer calling {@link #call(CombiningCallable, 1690 * Executor)} instead, with a function that returns the next value directly. 1691 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 1692 * for every closeable object this step creates in order to capture it for later closing. 1693 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 1694 * ClosingFuture} call {@link #from(ListenableFuture)}. 1695 * </ul> 1696 * 1697 * <p>The same warnings about doing heavyweight operations within {@link 1698 * ClosingFuture#transformAsync(AsyncClosingFunction, Executor)} apply here. 1699 */ 1700 public <U extends @Nullable Object> ClosingFuture<U> callAsync( 1701 final AsyncClosingFunction3<V1, V2, V3, U> function, Executor executor) { 1702 return callAsync( 1703 new AsyncCombiningCallable<U>() { 1704 @Override 1705 public ClosingFuture<U> call(DeferredCloser closer, Peeker peeker) throws Exception { 1706 return function.apply( 1707 closer, 1708 peeker.getDone(future1), 1709 peeker.getDone(future2), 1710 peeker.getDone(future3)); 1711 } 1712 1713 @Override 1714 public String toString() { 1715 return function.toString(); 1716 } 1717 }, 1718 executor); 1719 } 1720 } 1721 1722 /** 1723 * A generic {@link Combiner} that lets you use a lambda or method reference to combine four 1724 * {@link ClosingFuture}s. Use {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, 1725 * ClosingFuture)} to start this combination. 1726 * 1727 * @param <V1> the type returned by the first future 1728 * @param <V2> the type returned by the second future 1729 * @param <V3> the type returned by the third future 1730 * @param <V4> the type returned by the fourth future 1731 */ 1732 public static final class Combiner4< 1733 V1 extends @Nullable Object, 1734 V2 extends @Nullable Object, 1735 V3 extends @Nullable Object, 1736 V4 extends @Nullable Object> 1737 extends Combiner { 1738 /** 1739 * A function that returns a value when applied to the values of the four futures passed to 1740 * {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, ClosingFuture)}. 1741 * 1742 * @param <V1> the type returned by the first future 1743 * @param <V2> the type returned by the second future 1744 * @param <V3> the type returned by the third future 1745 * @param <V4> the type returned by the fourth future 1746 * @param <U> the type returned by the function 1747 */ 1748 @FunctionalInterface 1749 public interface ClosingFunction4< 1750 V1 extends @Nullable Object, 1751 V2 extends @Nullable Object, 1752 V3 extends @Nullable Object, 1753 V4 extends @Nullable Object, 1754 U extends @Nullable Object> { 1755 /** 1756 * Applies this function to four inputs, or throws an exception if unable to do so. 1757 * 1758 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1759 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1760 * (but not before this method completes), even if this method throws or the pipeline is 1761 * cancelled. 1762 */ 1763 @ParametricNullness 1764 U apply( 1765 DeferredCloser closer, 1766 @ParametricNullness V1 value1, 1767 @ParametricNullness V2 value2, 1768 @ParametricNullness V3 value3, 1769 @ParametricNullness V4 value4) 1770 throws Exception; 1771 } 1772 1773 /** 1774 * A function that returns a {@link ClosingFuture} when applied to the values of the four 1775 * futures passed to {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, 1776 * ClosingFuture)}. 1777 * 1778 * @param <V1> the type returned by the first future 1779 * @param <V2> the type returned by the second future 1780 * @param <V3> the type returned by the third future 1781 * @param <V4> the type returned by the fourth future 1782 * @param <U> the type returned by the function 1783 */ 1784 @FunctionalInterface 1785 public interface AsyncClosingFunction4< 1786 V1 extends @Nullable Object, 1787 V2 extends @Nullable Object, 1788 V3 extends @Nullable Object, 1789 V4 extends @Nullable Object, 1790 U extends @Nullable Object> { 1791 /** 1792 * Applies this function to four inputs, or throws an exception if unable to do so. 1793 * 1794 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1795 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1796 * (but not before this method completes), even if this method throws or the pipeline is 1797 * cancelled. 1798 */ 1799 ClosingFuture<U> apply( 1800 DeferredCloser closer, 1801 @ParametricNullness V1 value1, 1802 @ParametricNullness V2 value2, 1803 @ParametricNullness V3 value3, 1804 @ParametricNullness V4 value4) 1805 throws Exception; 1806 } 1807 1808 private final ClosingFuture<V1> future1; 1809 private final ClosingFuture<V2> future2; 1810 private final ClosingFuture<V3> future3; 1811 private final ClosingFuture<V4> future4; 1812 1813 private Combiner4( 1814 ClosingFuture<V1> future1, 1815 ClosingFuture<V2> future2, 1816 ClosingFuture<V3> future3, 1817 ClosingFuture<V4> future4) { 1818 super(true, ImmutableList.of(future1, future2, future3, future4)); 1819 this.future1 = future1; 1820 this.future2 = future2; 1821 this.future3 = future3; 1822 this.future4 = future4; 1823 } 1824 1825 /** 1826 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1827 * combining function to their values. The function can use a {@link DeferredCloser} to capture 1828 * objects to be closed when the pipeline is done. 1829 * 1830 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 1831 * ClosingFuture, ClosingFuture)} and any of the inputs fail, so will the returned step. 1832 * 1833 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1834 * 1835 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1836 * ExecutionException} will be extracted and used as the failure of the derived step. 1837 */ 1838 public <U extends @Nullable Object> ClosingFuture<U> call( 1839 final ClosingFunction4<V1, V2, V3, V4, U> function, Executor executor) { 1840 return call( 1841 new CombiningCallable<U>() { 1842 @Override 1843 @ParametricNullness 1844 public U call(DeferredCloser closer, Peeker peeker) throws Exception { 1845 return function.apply( 1846 closer, 1847 peeker.getDone(future1), 1848 peeker.getDone(future2), 1849 peeker.getDone(future3), 1850 peeker.getDone(future4)); 1851 } 1852 1853 @Override 1854 public String toString() { 1855 return function.toString(); 1856 } 1857 }, 1858 executor); 1859 } 1860 1861 /** 1862 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 1863 * {@code ClosingFuture}-returning function to their values. The function can use a {@link 1864 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 1865 * captured by the returned {@link ClosingFuture}). 1866 * 1867 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 1868 * ClosingFuture, ClosingFuture)} and any of the inputs fail, so will the returned step. 1869 * 1870 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 1871 * 1872 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 1873 * ExecutionException} will be extracted and used as the failure of the derived step. 1874 * 1875 * <p>If the function throws any other exception, it will be used as the failure of the derived 1876 * step. 1877 * 1878 * <p>If an exception is thrown after the function creates a {@code ClosingFuture}, then none of 1879 * the closeable objects in that {@code ClosingFuture} will be closed. 1880 * 1881 * <p>Usage guidelines for this method: 1882 * 1883 * <ul> 1884 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 1885 * {@code ClosingFuture}. If possible, prefer calling {@link #call(CombiningCallable, 1886 * Executor)} instead, with a function that returns the next value directly. 1887 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 1888 * for every closeable object this step creates in order to capture it for later closing. 1889 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 1890 * ClosingFuture} call {@link #from(ListenableFuture)}. 1891 * </ul> 1892 * 1893 * <p>The same warnings about doing heavyweight operations within {@link 1894 * ClosingFuture#transformAsync(AsyncClosingFunction, Executor)} apply here. 1895 */ 1896 public <U extends @Nullable Object> ClosingFuture<U> callAsync( 1897 final AsyncClosingFunction4<V1, V2, V3, V4, U> function, Executor executor) { 1898 return callAsync( 1899 new AsyncCombiningCallable<U>() { 1900 @Override 1901 public ClosingFuture<U> call(DeferredCloser closer, Peeker peeker) throws Exception { 1902 return function.apply( 1903 closer, 1904 peeker.getDone(future1), 1905 peeker.getDone(future2), 1906 peeker.getDone(future3), 1907 peeker.getDone(future4)); 1908 } 1909 1910 @Override 1911 public String toString() { 1912 return function.toString(); 1913 } 1914 }, 1915 executor); 1916 } 1917 } 1918 1919 /** 1920 * A generic {@link Combiner} that lets you use a lambda or method reference to combine five 1921 * {@link ClosingFuture}s. Use {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, 1922 * ClosingFuture, ClosingFuture)} to start this combination. 1923 * 1924 * @param <V1> the type returned by the first future 1925 * @param <V2> the type returned by the second future 1926 * @param <V3> the type returned by the third future 1927 * @param <V4> the type returned by the fourth future 1928 * @param <V5> the type returned by the fifth future 1929 */ 1930 public static final class Combiner5< 1931 V1 extends @Nullable Object, 1932 V2 extends @Nullable Object, 1933 V3 extends @Nullable Object, 1934 V4 extends @Nullable Object, 1935 V5 extends @Nullable Object> 1936 extends Combiner { 1937 /** 1938 * A function that returns a value when applied to the values of the five futures passed to 1939 * {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, ClosingFuture, 1940 * ClosingFuture)}. 1941 * 1942 * @param <V1> the type returned by the first future 1943 * @param <V2> the type returned by the second future 1944 * @param <V3> the type returned by the third future 1945 * @param <V4> the type returned by the fourth future 1946 * @param <V5> the type returned by the fifth future 1947 * @param <U> the type returned by the function 1948 */ 1949 @FunctionalInterface 1950 public interface ClosingFunction5< 1951 V1 extends @Nullable Object, 1952 V2 extends @Nullable Object, 1953 V3 extends @Nullable Object, 1954 V4 extends @Nullable Object, 1955 V5 extends @Nullable Object, 1956 U extends @Nullable Object> { 1957 /** 1958 * Applies this function to five inputs, or throws an exception if unable to do so. 1959 * 1960 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 1961 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 1962 * (but not before this method completes), even if this method throws or the pipeline is 1963 * cancelled. 1964 */ 1965 @ParametricNullness 1966 U apply( 1967 DeferredCloser closer, 1968 @ParametricNullness V1 value1, 1969 @ParametricNullness V2 value2, 1970 @ParametricNullness V3 value3, 1971 @ParametricNullness V4 value4, 1972 @ParametricNullness V5 value5) 1973 throws Exception; 1974 } 1975 1976 /** 1977 * A function that returns a {@link ClosingFuture} when applied to the values of the five 1978 * futures passed to {@link #whenAllSucceed(ClosingFuture, ClosingFuture, ClosingFuture, 1979 * ClosingFuture, ClosingFuture)}. 1980 * 1981 * @param <V1> the type returned by the first future 1982 * @param <V2> the type returned by the second future 1983 * @param <V3> the type returned by the third future 1984 * @param <V4> the type returned by the fourth future 1985 * @param <V5> the type returned by the fifth future 1986 * @param <U> the type returned by the function 1987 */ 1988 @FunctionalInterface 1989 public interface AsyncClosingFunction5< 1990 V1 extends @Nullable Object, 1991 V2 extends @Nullable Object, 1992 V3 extends @Nullable Object, 1993 V4 extends @Nullable Object, 1994 V5 extends @Nullable Object, 1995 U extends @Nullable Object> { 1996 /** 1997 * Applies this function to five inputs, or throws an exception if unable to do so. 1998 * 1999 * <p>Any objects that are passed to {@link DeferredCloser#eventuallyClose(Object, Executor) 2000 * closer.eventuallyClose()} will be closed when the {@link ClosingFuture} pipeline is done 2001 * (but not before this method completes), even if this method throws or the pipeline is 2002 * cancelled. 2003 */ 2004 ClosingFuture<U> apply( 2005 DeferredCloser closer, 2006 @ParametricNullness V1 value1, 2007 @ParametricNullness V2 value2, 2008 @ParametricNullness V3 value3, 2009 @ParametricNullness V4 value4, 2010 @ParametricNullness V5 value5) 2011 throws Exception; 2012 } 2013 2014 private final ClosingFuture<V1> future1; 2015 private final ClosingFuture<V2> future2; 2016 private final ClosingFuture<V3> future3; 2017 private final ClosingFuture<V4> future4; 2018 private final ClosingFuture<V5> future5; 2019 2020 private Combiner5( 2021 ClosingFuture<V1> future1, 2022 ClosingFuture<V2> future2, 2023 ClosingFuture<V3> future3, 2024 ClosingFuture<V4> future4, 2025 ClosingFuture<V5> future5) { 2026 super(true, ImmutableList.of(future1, future2, future3, future4, future5)); 2027 this.future1 = future1; 2028 this.future2 = future2; 2029 this.future3 = future3; 2030 this.future4 = future4; 2031 this.future5 = future5; 2032 } 2033 2034 /** 2035 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 2036 * combining function to their values. The function can use a {@link DeferredCloser} to capture 2037 * objects to be closed when the pipeline is done. 2038 * 2039 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 2040 * ClosingFuture, ClosingFuture, ClosingFuture)} and any of the inputs fail, so will the 2041 * returned step. 2042 * 2043 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 2044 * 2045 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 2046 * ExecutionException} will be extracted and used as the failure of the derived step. 2047 */ 2048 public <U extends @Nullable Object> ClosingFuture<U> call( 2049 final ClosingFunction5<V1, V2, V3, V4, V5, U> function, Executor executor) { 2050 return call( 2051 new CombiningCallable<U>() { 2052 @Override 2053 @ParametricNullness 2054 public U call(DeferredCloser closer, Peeker peeker) throws Exception { 2055 return function.apply( 2056 closer, 2057 peeker.getDone(future1), 2058 peeker.getDone(future2), 2059 peeker.getDone(future3), 2060 peeker.getDone(future4), 2061 peeker.getDone(future5)); 2062 } 2063 2064 @Override 2065 public String toString() { 2066 return function.toString(); 2067 } 2068 }, 2069 executor); 2070 } 2071 2072 /** 2073 * Returns a new {@code ClosingFuture} pipeline step derived from the inputs by applying a 2074 * {@code ClosingFuture}-returning function to their values. The function can use a {@link 2075 * DeferredCloser} to capture objects to be closed when the pipeline is done (other than those 2076 * captured by the returned {@link ClosingFuture}). 2077 * 2078 * <p>If this combiner was returned by {@link #whenAllSucceed(ClosingFuture, ClosingFuture, 2079 * ClosingFuture, ClosingFuture, ClosingFuture)} and any of the inputs fail, so will the 2080 * returned step. 2081 * 2082 * <p>If the function throws a {@code CancellationException}, the pipeline will be cancelled. 2083 * 2084 * <p>If the function throws an {@code ExecutionException}, the cause of the thrown {@code 2085 * ExecutionException} will be extracted and used as the failure of the derived step. 2086 * 2087 * <p>If the function throws any other exception, it will be used as the failure of the derived 2088 * step. 2089 * 2090 * <p>If an exception is thrown after the function creates a {@code ClosingFuture}, then none of 2091 * the closeable objects in that {@code ClosingFuture} will be closed. 2092 * 2093 * <p>Usage guidelines for this method: 2094 * 2095 * <ul> 2096 * <li>Use this method only when calling an API that returns a {@link ListenableFuture} or a 2097 * {@code ClosingFuture}. If possible, prefer calling {@link #call(CombiningCallable, 2098 * Executor)} instead, with a function that returns the next value directly. 2099 * <li>Call {@link DeferredCloser#eventuallyClose(Object, Executor) closer.eventuallyClose()} 2100 * for every closeable object this step creates in order to capture it for later closing. 2101 * <li>Return a {@code ClosingFuture}. To turn a {@link ListenableFuture} into a {@code 2102 * ClosingFuture} call {@link #from(ListenableFuture)}. 2103 * </ul> 2104 * 2105 * <p>The same warnings about doing heavyweight operations within {@link 2106 * ClosingFuture#transformAsync(AsyncClosingFunction, Executor)} apply here. 2107 */ 2108 public <U extends @Nullable Object> ClosingFuture<U> callAsync( 2109 final AsyncClosingFunction5<V1, V2, V3, V4, V5, U> function, Executor executor) { 2110 return callAsync( 2111 new AsyncCombiningCallable<U>() { 2112 @Override 2113 public ClosingFuture<U> call(DeferredCloser closer, Peeker peeker) throws Exception { 2114 return function.apply( 2115 closer, 2116 peeker.getDone(future1), 2117 peeker.getDone(future2), 2118 peeker.getDone(future3), 2119 peeker.getDone(future4), 2120 peeker.getDone(future5)); 2121 } 2122 2123 @Override 2124 public String toString() { 2125 return function.toString(); 2126 } 2127 }, 2128 executor); 2129 } 2130 } 2131 2132 @Override 2133 public String toString() { 2134 // TODO(dpb): Better toString, in the style of Futures.transform etc. 2135 return toStringHelper(this).add("state", state.get()).addValue(future).toString(); 2136 } 2137 2138 @SuppressWarnings({"removal", "Finalize"}) // b/260137033 2139 @Override 2140 protected void finalize() { 2141 if (state.get().equals(OPEN)) { 2142 logger.get().log(SEVERE, "Uh oh! An open ClosingFuture has leaked and will close: {0}", this); 2143 FluentFuture<V> unused = finishToFuture(); 2144 } 2145 } 2146 2147 private static void closeQuietly(final @Nullable AutoCloseable closeable, Executor executor) { 2148 if (closeable == null) { 2149 return; 2150 } 2151 try { 2152 executor.execute( 2153 () -> { 2154 try { 2155 closeable.close(); 2156 } catch (Exception e) { 2157 /* 2158 * In guava-jre, any kind of Exception may be thrown because `closeable` has type 2159 * `AutoCloseable`. 2160 * 2161 * In guava-android, the only kinds of Exception that may be thrown are 2162 * RuntimeException and IOException because `closeable` has type `Closeable`—except 2163 * that we have to account for sneaky checked exception. 2164 */ 2165 restoreInterruptIfIsInterruptedException(e); 2166 logger.get().log(WARNING, "thrown by close()", e); 2167 } 2168 }); 2169 } catch (RejectedExecutionException e) { 2170 if (logger.get().isLoggable(WARNING)) { 2171 logger 2172 .get() 2173 .log( 2174 WARNING, 2175 String.format("while submitting close to %s; will close inline", executor), 2176 e); 2177 } 2178 closeQuietly(closeable, directExecutor()); 2179 } 2180 } 2181 2182 private void checkAndUpdateState(State oldState, State newState) { 2183 checkState( 2184 compareAndUpdateState(oldState, newState), 2185 "Expected state to be %s, but it was %s", 2186 oldState, 2187 newState); 2188 } 2189 2190 private boolean compareAndUpdateState(State oldState, State newState) { 2191 return state.compareAndSet(oldState, newState); 2192 } 2193 2194 // TODO(dpb): Should we use a pair of ArrayLists instead of an IdentityHashMap? 2195 private static final class CloseableList extends IdentityHashMap<AutoCloseable, Executor> 2196 implements Closeable { 2197 private final DeferredCloser closer = new DeferredCloser(this); 2198 private volatile boolean closed; 2199 private volatile @Nullable CountDownLatch whenClosed; 2200 2201 <V extends @Nullable Object, U extends @Nullable Object> 2202 ListenableFuture<U> applyClosingFunction( 2203 ClosingFunction<? super V, U> transformation, @ParametricNullness V input) 2204 throws Exception { 2205 // TODO(dpb): Consider ways to defer closing without creating a separate CloseableList. 2206 CloseableList newCloseables = new CloseableList(); 2207 try { 2208 return immediateFuture(transformation.apply(newCloseables.closer, input)); 2209 } finally { 2210 add(newCloseables, directExecutor()); 2211 } 2212 } 2213 2214 <V extends @Nullable Object, U extends @Nullable Object> 2215 FluentFuture<U> applyAsyncClosingFunction( 2216 AsyncClosingFunction<V, U> transformation, @ParametricNullness V input) 2217 throws Exception { 2218 // TODO(dpb): Consider ways to defer closing without creating a separate CloseableList. 2219 CloseableList newCloseables = new CloseableList(); 2220 try { 2221 ClosingFuture<U> closingFuture = transformation.apply(newCloseables.closer, input); 2222 closingFuture.becomeSubsumedInto(newCloseables); 2223 return closingFuture.future; 2224 } finally { 2225 add(newCloseables, directExecutor()); 2226 } 2227 } 2228 2229 @Override 2230 public void close() { 2231 if (closed) { 2232 return; 2233 } 2234 synchronized (this) { 2235 if (closed) { 2236 return; 2237 } 2238 closed = true; 2239 } 2240 for (Map.Entry<AutoCloseable, Executor> entry : entrySet()) { 2241 closeQuietly(entry.getKey(), entry.getValue()); 2242 } 2243 clear(); 2244 if (whenClosed != null) { 2245 whenClosed.countDown(); 2246 } 2247 } 2248 2249 void add(@Nullable AutoCloseable closeable, Executor executor) { 2250 checkNotNull(executor); 2251 if (closeable == null) { 2252 return; 2253 } 2254 synchronized (this) { 2255 if (!closed) { 2256 put(closeable, executor); 2257 return; 2258 } 2259 } 2260 closeQuietly(closeable, executor); 2261 } 2262 2263 /** 2264 * Returns a latch that reaches zero when this objects' deferred closeables have been closed. 2265 */ 2266 CountDownLatch whenClosedCountDown() { 2267 if (closed) { 2268 return new CountDownLatch(0); 2269 } 2270 synchronized (this) { 2271 if (closed) { 2272 return new CountDownLatch(0); 2273 } 2274 checkState(whenClosed == null); 2275 return whenClosed = new CountDownLatch(1); 2276 } 2277 } 2278 } 2279 2280 /** 2281 * Returns an object that can be used to wait until this objects' deferred closeables have all had 2282 * {@link Runnable}s that close them submitted to each one's closing {@link Executor}. 2283 */ 2284 @VisibleForTesting 2285 CountDownLatch whenClosedCountDown() { 2286 return closeables.whenClosedCountDown(); 2287 } 2288 2289 /** The state of a {@link CloseableList}. */ 2290 enum State { 2291 /** The {@link CloseableList} has not been subsumed or closed. */ 2292 OPEN, 2293 2294 /** 2295 * The {@link CloseableList} has been subsumed into another. It may not be closed or subsumed 2296 * into any other. 2297 */ 2298 SUBSUMED, 2299 2300 /** 2301 * Some {@link ListenableFuture} has a callback attached that will close the {@link 2302 * CloseableList}, but it has not yet run. The {@link CloseableList} may not be subsumed. 2303 */ 2304 WILL_CLOSE, 2305 2306 /** 2307 * The callback that closes the {@link CloseableList} is running, but it has not completed. The 2308 * {@link CloseableList} may not be subsumed. 2309 */ 2310 CLOSING, 2311 2312 /** The {@link CloseableList} has been closed. It may not be further subsumed. */ 2313 CLOSED, 2314 2315 /** 2316 * {@link ClosingFuture#finishToValueAndCloser(ValueAndCloserConsumer, Executor)} has been 2317 * called. The step may not be further subsumed, nor may {@link #finishToFuture()} be called. 2318 */ 2319 WILL_CREATE_VALUE_AND_CLOSER, 2320 } 2321}