diff options
Diffstat (limited to 'android/guava/src/com/google/common/util/concurrent/ClosingFuture.java')
-rw-r--r-- | android/guava/src/com/google/common/util/concurrent/ClosingFuture.java | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/android/guava/src/com/google/common/util/concurrent/ClosingFuture.java b/android/guava/src/com/google/common/util/concurrent/ClosingFuture.java index 43157c7a4..c43074b3c 100644 --- a/android/guava/src/com/google/common/util/concurrent/ClosingFuture.java +++ b/android/guava/src/com/google/common/util/concurrent/ClosingFuture.java @@ -32,6 +32,7 @@ import static com.google.common.util.concurrent.Futures.getDone; import static com.google.common.util.concurrent.Futures.immediateFuture; import static com.google.common.util.concurrent.Futures.nonCancellationPropagating; import static com.google.common.util.concurrent.MoreExecutors.directExecutor; +import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException; import static java.util.logging.Level.FINER; import static java.util.logging.Level.SEVERE; import static java.util.logging.Level.WARNING; @@ -47,7 +48,6 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.DoNotMock; import com.google.j2objc.annotations.RetainedWith; import java.io.Closeable; -import java.io.IOException; import java.util.IdentityHashMap; import java.util.Map; import java.util.concurrent.Callable; @@ -58,7 +58,6 @@ import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.atomic.AtomicReference; -import java.util.logging.Logger; import javax.annotation.CheckForNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -196,7 +195,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; // TODO(dpb): GWT compatibility. public final class ClosingFuture<V extends @Nullable Object> { - private static final Logger logger = Logger.getLogger(ClosingFuture.class.getName()); + private static final LazyLogger logger = new LazyLogger(ClosingFuture.class); /** * An object that can capture objects to be closed later, when a {@link ClosingFuture} pipeline is @@ -678,7 +677,7 @@ public final class ClosingFuture<V extends @Nullable Object> { * * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on - * this {@code ClosingFuture}. + * the original {@code ClosingFuture} instance. * * @param function transforms the value of this step to the value of the derived step * @param executor executor to run the function in @@ -771,7 +770,7 @@ public final class ClosingFuture<V extends @Nullable Object> { * * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on - * this {@code ClosingFuture}. + * the original {@code ClosingFuture} instance. * * @param function transforms the value of this step to a {@code ClosingFuture} with the value of * the derived step @@ -862,7 +861,7 @@ public final class ClosingFuture<V extends @Nullable Object> { * * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on - * this {@code ClosingFuture}. + * the original {@code ClosingFuture} instance. * * @param exceptionType the exception type that triggers use of {@code fallback}. The exception * type is matched against this step's exception. "This step's exception" means the cause of @@ -955,7 +954,7 @@ public final class ClosingFuture<V extends @Nullable Object> { * * <p>After calling this method, you may not call {@link #finishToFuture()}, {@link * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, or any other derivation method on - * this {@code ClosingFuture}. + * the original {@code ClosingFuture} instance. * * @param exceptionType the exception type that triggers use of {@code fallback}. The exception * type is matched against this step's exception. "This step's exception" means the cause of @@ -1012,13 +1011,13 @@ public final class ClosingFuture<V extends @Nullable Object> { * * <p>After calling this method, you may not call {@link * #finishToValueAndCloser(ValueAndCloserConsumer, Executor)}, this method, or any other - * derivation method on this {@code ClosingFuture}. + * derivation method on the original {@code ClosingFuture} instance. * * @return a {@link Future} that represents the final value or exception of the pipeline */ public FluentFuture<V> finishToFuture() { if (compareAndUpdateState(OPEN, WILL_CLOSE)) { - logger.log(FINER, "will close {0}", this); + logger.get().log(FINER, "will close {0}", this); future.addListener( new Runnable() { @Override @@ -1057,7 +1056,7 @@ public final class ClosingFuture<V extends @Nullable Object> { * receiver can store the {@link ValueAndCloser} outside the receiver for later synchronous use. * * <p>After calling this method, you may not call {@link #finishToFuture()}, this method again, or - * any other derivation method on this {@code ClosingFuture}. + * any other derivation method on the original {@code ClosingFuture} instance. * * @param consumer a callback whose method will be called (using {@code executor}) when this * operation is done @@ -1118,7 +1117,7 @@ public final class ClosingFuture<V extends @Nullable Object> { */ @CanIgnoreReturnValue public boolean cancel(boolean mayInterruptIfRunning) { - logger.log(FINER, "cancelling {0}", this); + logger.get().log(FINER, "cancelling {0}", this); boolean cancelled = future.cancel(mayInterruptIfRunning); if (cancelled) { close(); @@ -1127,7 +1126,7 @@ public final class ClosingFuture<V extends @Nullable Object> { } private void close() { - logger.log(FINER, "closing {0}", this); + logger.get().log(FINER, "closing {0}", this); closeables.close(); } @@ -2131,7 +2130,7 @@ public final class ClosingFuture<V extends @Nullable Object> { @Override protected void finalize() { if (state.get().equals(OPEN)) { - logger.log(SEVERE, "Uh oh! An open ClosingFuture has leaked and will close: {0}", this); + logger.get().log(SEVERE, "Uh oh! An open ClosingFuture has leaked and will close: {0}", this); FluentFuture<V> unused = finishToFuture(); } } @@ -2145,14 +2144,27 @@ public final class ClosingFuture<V extends @Nullable Object> { () -> { try { closeable.close(); - } catch (IOException | RuntimeException e) { - logger.log(WARNING, "thrown by close()", e); + } catch (Exception e) { + /* + * In guava-jre, any kind of Exception may be thrown because `closeable` has type + * `AutoCloseable`. + * + * In guava-android, the only kinds of Exception that may be thrown are + * RuntimeException and IOException because `closeable` has type `Closeable`—except + * that we have to account for sneaky checked exception. + */ + restoreInterruptIfIsInterruptedException(e); + logger.get().log(WARNING, "thrown by close()", e); } }); } catch (RejectedExecutionException e) { - if (logger.isLoggable(WARNING)) { - logger.log( - WARNING, String.format("while submitting close to %s; will close inline", executor), e); + if (logger.get().isLoggable(WARNING)) { + logger + .get() + .log( + WARNING, + String.format("while submitting close to %s; will close inline", executor), + e); } closeQuietly(closeable, directExecutor()); } |