Skip to content

Commit 60ba1ab

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Reduce scope of suppressions in Abstract*Future classes, and make the classes look slightly more similar to one another.
RELNOTES=n/a PiperOrigin-RevId: 729152015
1 parent a5a02ae commit 60ba1ab

File tree

4 files changed

+78
-86
lines changed

4 files changed

+78
-86
lines changed

android/guava/src/com/google/common/util/concurrent/AbstractCatchingFuture.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@
3434

3535
/** Implementations of {@code Futures.catching*}. */
3636
@GwtCompatible
37-
@SuppressWarnings({
38-
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
39-
"ShortCircuitBoolean",
40-
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
41-
})
37+
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
38+
@SuppressWarnings("ShortCircuitBoolean")
4239
abstract class AbstractCatchingFuture<
4340
V extends @Nullable Object, X extends Throwable, F, T extends @Nullable Object>
4441
extends FluentFuture.TrustedFuture<V> implements Runnable {
@@ -47,19 +44,19 @@ abstract class AbstractCatchingFuture<
4744
Class<X> exceptionType,
4845
Function<? super X, ? extends V> fallback,
4946
Executor executor) {
50-
CatchingFuture<V, X> future = new CatchingFuture<>(input, exceptionType, fallback);
51-
input.addListener(future, rejectionPropagatingExecutor(executor, future));
52-
return future;
47+
CatchingFuture<V, X> output = new CatchingFuture<>(input, exceptionType, fallback);
48+
input.addListener(output, rejectionPropagatingExecutor(executor, output));
49+
return output;
5350
}
5451

5552
static <X extends Throwable, V extends @Nullable Object> ListenableFuture<V> createAsync(
5653
ListenableFuture<? extends V> input,
5754
Class<X> exceptionType,
5855
AsyncFunction<? super X, ? extends V> fallback,
5956
Executor executor) {
60-
AsyncCatchingFuture<V, X> future = new AsyncCatchingFuture<>(input, exceptionType, fallback);
61-
input.addListener(future, rejectionPropagatingExecutor(executor, future));
62-
return future;
57+
AsyncCatchingFuture<V, X> output = new AsyncCatchingFuture<>(input, exceptionType, fallback);
58+
input.addListener(output, rejectionPropagatingExecutor(executor, output));
59+
return output;
6360
}
6461

6562
/*
@@ -78,6 +75,7 @@ abstract class AbstractCatchingFuture<
7875
}
7976

8077
@Override
78+
@SuppressWarnings("nullness") // TODO(b/147136275): Remove once our checker understands & and |.
8179
public final void run() {
8280
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
8381
@RetainedLocalRef Class<X> localExceptionType = exceptionType;
@@ -148,6 +146,24 @@ public final void run() {
148146
setResult(fallbackResult);
149147
}
150148

149+
/** Template method for subtypes to actually run the fallback. */
150+
@ForOverride
151+
@ParametricNullness
152+
abstract T doFallback(F fallback, X throwable) throws Exception;
153+
154+
/** Template method for subtypes to actually set the result. */
155+
@ForOverride
156+
abstract void setResult(@ParametricNullness T result);
157+
158+
@Override
159+
protected final void afterDone() {
160+
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
161+
maybePropagateCancellationTo(localInputFuture);
162+
this.inputFuture = null;
163+
this.exceptionType = null;
164+
this.fallback = null;
165+
}
166+
151167
@Override
152168
protected @Nullable String pendingToString() {
153169
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
@@ -171,24 +187,6 @@ public final void run() {
171187
return null;
172188
}
173189

174-
/** Template method for subtypes to actually run the fallback. */
175-
@ForOverride
176-
@ParametricNullness
177-
abstract T doFallback(F fallback, X throwable) throws Exception;
178-
179-
/** Template method for subtypes to actually set the result. */
180-
@ForOverride
181-
abstract void setResult(@ParametricNullness T result);
182-
183-
@Override
184-
protected final void afterDone() {
185-
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
186-
maybePropagateCancellationTo(localInputFuture);
187-
this.inputFuture = null;
188-
this.exceptionType = null;
189-
this.fallback = null;
190-
}
191-
192190
/**
193191
* An {@link AbstractCatchingFuture} that delegates to an {@link AsyncFunction} and {@link
194192
* #setFuture(ListenableFuture)}.
@@ -206,13 +204,13 @@ private static final class AsyncCatchingFuture<V extends @Nullable Object, X ext
206204
@Override
207205
ListenableFuture<? extends V> doFallback(
208206
AsyncFunction<? super X, ? extends V> fallback, X cause) throws Exception {
209-
ListenableFuture<? extends V> replacement = fallback.apply(cause);
207+
ListenableFuture<? extends V> output = fallback.apply(cause);
210208
checkNotNull(
211-
replacement,
209+
output,
212210
"AsyncFunction.apply returned null instead of a Future. "
213211
+ "Did you mean to return immediateFuture(null)? %s",
214212
fallback);
215-
return replacement;
213+
return output;
216214
}
217215

218216
@Override

android/guava/src/com/google/common/util/concurrent/AbstractTransformFuture.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,22 @@
3131

3232
/** Implementations of {@code Futures.transform*}. */
3333
@GwtCompatible
34-
@SuppressWarnings({
35-
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
36-
"ShortCircuitBoolean",
37-
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
38-
})
34+
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
35+
@SuppressWarnings("ShortCircuitBoolean")
3936
abstract class AbstractTransformFuture<
4037
I extends @Nullable Object, O extends @Nullable Object, F, T extends @Nullable Object>
4138
extends FluentFuture.TrustedFuture<O> implements Runnable {
4239
static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O> createAsync(
4340
ListenableFuture<I> input,
4441
AsyncFunction<? super I, ? extends O> function,
4542
Executor executor) {
46-
checkNotNull(executor);
4743
AsyncTransformFuture<I, O> output = new AsyncTransformFuture<>(input, function);
4844
input.addListener(output, rejectionPropagatingExecutor(executor, output));
4945
return output;
5046
}
5147

5248
static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O> create(
5349
ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) {
54-
checkNotNull(function);
5550
TransformFuture<I, O> output = new TransformFuture<>(input, function);
5651
input.addListener(output, rejectionPropagatingExecutor(executor, output));
5752
return output;
@@ -70,7 +65,10 @@ abstract class AbstractTransformFuture<
7065
}
7166

7267
@Override
73-
@SuppressWarnings("CatchingUnchecked") // sneaky checked exception
68+
@SuppressWarnings({
69+
"CatchingUnchecked", // sneaky checked exception
70+
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
71+
})
7472
public final void run() {
7573
@RetainedLocalRef ListenableFuture<? extends I> localInputFuture = inputFuture;
7674
@RetainedLocalRef F localFunction = function;
@@ -225,13 +223,13 @@ private static final class AsyncTransformFuture<
225223
ListenableFuture<? extends O> doTransform(
226224
AsyncFunction<? super I, ? extends O> function, @ParametricNullness I input)
227225
throws Exception {
228-
ListenableFuture<? extends O> outputFuture = function.apply(input);
226+
ListenableFuture<? extends O> output = function.apply(input);
229227
checkNotNull(
230-
outputFuture,
228+
output,
231229
"AsyncFunction.apply returned null instead of a Future. "
232230
+ "Did you mean to return immediateFuture(null)? %s",
233231
function);
234-
return outputFuture;
232+
return output;
235233
}
236234

237235
@Override

guava/src/com/google/common/util/concurrent/AbstractCatchingFuture.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@
3434

3535
/** Implementations of {@code Futures.catching*}. */
3636
@GwtCompatible
37-
@SuppressWarnings({
38-
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
39-
"ShortCircuitBoolean",
40-
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
41-
})
37+
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
38+
@SuppressWarnings("ShortCircuitBoolean")
4239
abstract class AbstractCatchingFuture<
4340
V extends @Nullable Object, X extends Throwable, F, T extends @Nullable Object>
4441
extends FluentFuture.TrustedFuture<V> implements Runnable {
@@ -47,19 +44,19 @@ abstract class AbstractCatchingFuture<
4744
Class<X> exceptionType,
4845
Function<? super X, ? extends V> fallback,
4946
Executor executor) {
50-
CatchingFuture<V, X> future = new CatchingFuture<>(input, exceptionType, fallback);
51-
input.addListener(future, rejectionPropagatingExecutor(executor, future));
52-
return future;
47+
CatchingFuture<V, X> output = new CatchingFuture<>(input, exceptionType, fallback);
48+
input.addListener(output, rejectionPropagatingExecutor(executor, output));
49+
return output;
5350
}
5451

5552
static <X extends Throwable, V extends @Nullable Object> ListenableFuture<V> createAsync(
5653
ListenableFuture<? extends V> input,
5754
Class<X> exceptionType,
5855
AsyncFunction<? super X, ? extends V> fallback,
5956
Executor executor) {
60-
AsyncCatchingFuture<V, X> future = new AsyncCatchingFuture<>(input, exceptionType, fallback);
61-
input.addListener(future, rejectionPropagatingExecutor(executor, future));
62-
return future;
57+
AsyncCatchingFuture<V, X> output = new AsyncCatchingFuture<>(input, exceptionType, fallback);
58+
input.addListener(output, rejectionPropagatingExecutor(executor, output));
59+
return output;
6360
}
6461

6562
/*
@@ -78,6 +75,7 @@ abstract class AbstractCatchingFuture<
7875
}
7976

8077
@Override
78+
@SuppressWarnings("nullness") // TODO(b/147136275): Remove once our checker understands & and |.
8179
public final void run() {
8280
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
8381
@RetainedLocalRef Class<X> localExceptionType = exceptionType;
@@ -148,6 +146,24 @@ public final void run() {
148146
setResult(fallbackResult);
149147
}
150148

149+
/** Template method for subtypes to actually run the fallback. */
150+
@ForOverride
151+
@ParametricNullness
152+
abstract T doFallback(F fallback, X throwable) throws Exception;
153+
154+
/** Template method for subtypes to actually set the result. */
155+
@ForOverride
156+
abstract void setResult(@ParametricNullness T result);
157+
158+
@Override
159+
protected final void afterDone() {
160+
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
161+
maybePropagateCancellationTo(localInputFuture);
162+
this.inputFuture = null;
163+
this.exceptionType = null;
164+
this.fallback = null;
165+
}
166+
151167
@Override
152168
protected @Nullable String pendingToString() {
153169
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
@@ -171,24 +187,6 @@ public final void run() {
171187
return null;
172188
}
173189

174-
/** Template method for subtypes to actually run the fallback. */
175-
@ForOverride
176-
@ParametricNullness
177-
abstract T doFallback(F fallback, X throwable) throws Exception;
178-
179-
/** Template method for subtypes to actually set the result. */
180-
@ForOverride
181-
abstract void setResult(@ParametricNullness T result);
182-
183-
@Override
184-
protected final void afterDone() {
185-
@RetainedLocalRef ListenableFuture<? extends V> localInputFuture = inputFuture;
186-
maybePropagateCancellationTo(localInputFuture);
187-
this.inputFuture = null;
188-
this.exceptionType = null;
189-
this.fallback = null;
190-
}
191-
192190
/**
193191
* An {@link AbstractCatchingFuture} that delegates to an {@link AsyncFunction} and {@link
194192
* #setFuture(ListenableFuture)}.
@@ -206,13 +204,13 @@ private static final class AsyncCatchingFuture<V extends @Nullable Object, X ext
206204
@Override
207205
ListenableFuture<? extends V> doFallback(
208206
AsyncFunction<? super X, ? extends V> fallback, X cause) throws Exception {
209-
ListenableFuture<? extends V> replacement = fallback.apply(cause);
207+
ListenableFuture<? extends V> output = fallback.apply(cause);
210208
checkNotNull(
211-
replacement,
209+
output,
212210
"AsyncFunction.apply returned null instead of a Future. "
213211
+ "Did you mean to return immediateFuture(null)? %s",
214212
fallback);
215-
return replacement;
213+
return output;
216214
}
217215

218216
@Override

guava/src/com/google/common/util/concurrent/AbstractTransformFuture.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,22 @@
3131

3232
/** Implementations of {@code Futures.transform*}. */
3333
@GwtCompatible
34-
@SuppressWarnings({
35-
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
36-
"ShortCircuitBoolean",
37-
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
38-
})
34+
// Whenever both tests are cheap and functional, it's faster to use &, | instead of &&, ||
35+
@SuppressWarnings("ShortCircuitBoolean")
3936
abstract class AbstractTransformFuture<
4037
I extends @Nullable Object, O extends @Nullable Object, F, T extends @Nullable Object>
4138
extends FluentFuture.TrustedFuture<O> implements Runnable {
4239
static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O> createAsync(
4340
ListenableFuture<I> input,
4441
AsyncFunction<? super I, ? extends O> function,
4542
Executor executor) {
46-
checkNotNull(executor);
4743
AsyncTransformFuture<I, O> output = new AsyncTransformFuture<>(input, function);
4844
input.addListener(output, rejectionPropagatingExecutor(executor, output));
4945
return output;
5046
}
5147

5248
static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O> create(
5349
ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) {
54-
checkNotNull(function);
5550
TransformFuture<I, O> output = new TransformFuture<>(input, function);
5651
input.addListener(output, rejectionPropagatingExecutor(executor, output));
5752
return output;
@@ -70,7 +65,10 @@ abstract class AbstractTransformFuture<
7065
}
7166

7267
@Override
73-
@SuppressWarnings("CatchingUnchecked") // sneaky checked exception
68+
@SuppressWarnings({
69+
"CatchingUnchecked", // sneaky checked exception
70+
"nullness", // TODO(b/147136275): Remove once our checker understands & and |.
71+
})
7472
public final void run() {
7573
@RetainedLocalRef ListenableFuture<? extends I> localInputFuture = inputFuture;
7674
@RetainedLocalRef F localFunction = function;
@@ -225,13 +223,13 @@ private static final class AsyncTransformFuture<
225223
ListenableFuture<? extends O> doTransform(
226224
AsyncFunction<? super I, ? extends O> function, @ParametricNullness I input)
227225
throws Exception {
228-
ListenableFuture<? extends O> outputFuture = function.apply(input);
226+
ListenableFuture<? extends O> output = function.apply(input);
229227
checkNotNull(
230-
outputFuture,
228+
output,
231229
"AsyncFunction.apply returned null instead of a Future. "
232230
+ "Did you mean to return immediateFuture(null)? %s",
233231
function);
234-
return outputFuture;
232+
return output;
235233
}
236234

237235
@Override

0 commit comments

Comments
 (0)