• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.tracing;
17 
18 import com.google.common.base.Function;
19 import com.google.common.util.concurrent.AsyncFunction;
20 import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import org.checkerframework.checker.nullness.qual.Nullable;
28 
29 /**
30  * Similar to {@link com.google.common.util.concurrent.FluentFuture}, but with trace propagation.
31  * Note that the {@link ListenableFuture#addListener(Runnable, Executor)} method <b>does not</b>
32  * propagate traces.
33  */
34 public final class PropagatedFluentFuture<V extends @Nullable Object>
35     extends SimpleForwardingListenableFuture<V> {
36 
PropagatedFluentFuture(ListenableFuture<V> delegate)37   private PropagatedFluentFuture(ListenableFuture<V> delegate) {
38     super(delegate);
39   }
40 
41   /** See {@link com.google.common.util.concurrent.FluentFuture#from(ListenableFuture)}. */
from( ListenableFuture<V> future)42   public static <V extends @Nullable Object> PropagatedFluentFuture<V> from(
43       ListenableFuture<V> future) {
44     return future instanceof PropagatedFluentFuture
45         ? (PropagatedFluentFuture<V>) future
46         : new PropagatedFluentFuture<>(future);
47   }
48 
49   /**
50    * See {@link com.google.common.util.concurrent.FluentFuture#catching(Class, Function, Executor)}.
51    */
catching( Class<X> exceptionType, Function<? super X, ? extends V> fallback, Executor executor)52   public final <X extends Throwable> PropagatedFluentFuture<V> catching(
53       Class<X> exceptionType, Function<? super X, ? extends V> fallback, Executor executor) {
54     return new PropagatedFluentFuture<>(
55         PropagatedFutures.catching(delegate(), exceptionType, fallback, executor));
56   }
57 
58   /**
59    * See {@link com.google.common.util.concurrent.FluentFuture#catchingAsync(Class, AsyncFunction,
60    * Executor)}.
61    */
catchingAsync( Class<X> exceptionType, AsyncFunction<? super X, ? extends V> fallback, Executor executor)62   public final <X extends Throwable> PropagatedFluentFuture<V> catchingAsync(
63       Class<X> exceptionType, AsyncFunction<? super X, ? extends V> fallback, Executor executor) {
64     return new PropagatedFluentFuture<>(
65         PropagatedFutures.catchingAsync(delegate(), exceptionType, fallback, executor));
66   }
67 
68   /**
69    * See {@link com.google.common.util.concurrent.FluentFuture#withTimeout(long, TimeUnit,
70    * ScheduledExecutorService)}.
71    */
withTimeout( long timeout, TimeUnit unit, ScheduledExecutorService scheduledExecutor)72   public final PropagatedFluentFuture<V> withTimeout(
73       long timeout, TimeUnit unit, ScheduledExecutorService scheduledExecutor) {
74     return new PropagatedFluentFuture<>(
75         Futures.withTimeout(delegate(), timeout, unit, scheduledExecutor));
76   }
77 
78   /**
79    * See {@link com.google.common.util.concurrent.FluentFuture#transformAsync(AsyncFunction,
80    * Executor)}.
81    */
transformAsync( AsyncFunction<? super V, T> function, Executor executor)82   public final <T extends @Nullable Object> PropagatedFluentFuture<T> transformAsync(
83       AsyncFunction<? super V, T> function, Executor executor) {
84     return new PropagatedFluentFuture<>(
85         PropagatedFutures.transformAsync(delegate(), function, executor));
86   }
87 
88   /** See {@link com.google.common.util.concurrent.FluentFuture#transform(Function, Executor)}. */
transform( Function<? super V, T> function, Executor executor)89   public final <T extends @Nullable Object> PropagatedFluentFuture<T> transform(
90       Function<? super V, T> function, Executor executor) {
91     return new PropagatedFluentFuture<>(
92         PropagatedFutures.transform(delegate(), function, executor));
93   }
94 
95   /**
96    * See {@link com.google.common.util.concurrent.FluentFuture#addCallback(FutureCallback,
97    * Executor)}.
98    */
addCallback(FutureCallback<? super V> callback, Executor executor)99   public final void addCallback(FutureCallback<? super V> callback, Executor executor) {
100     PropagatedFutures.addCallback(delegate(), callback, executor);
101   }
102 }
103