aboutsummaryrefslogtreecommitdiff
path: root/java/dagger/producers/internal/AbstractProducesMethodProducer.java
blob: 0cf36ca531577e42e97a2154bf428e055c8be08b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2018 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.producers.internal;

import static dagger.internal.Preconditions.checkNotNull;

import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import dagger.producers.monitoring.ProducerMonitor;
import dagger.producers.monitoring.ProducerToken;
import dagger.producers.monitoring.ProductionComponentMonitor;
import java.util.concurrent.Executor;
import javax.inject.Provider;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
 * An {@link AbstractProducer} for all {@link dagger.producers.Produces} methods.
 *
 * @param <D> the type of asynchronous dependencies. These will be collected in {@link
 *     #collectDependencies()} and then made available to the {@code @Produces method in} {@link
 *     #callProducesMethod(Object)}. If there is only one asynchronous dependency, {@code D} can be
 *     the key for that dependency. If there are multiple, they should be wrapped in a list and
 *     unwrapped in {@link #callProducesMethod(Object)}.
 * @param <T> the produced type
 */
public abstract class AbstractProducesMethodProducer<D, T> extends AbstractProducer<T>
    implements AsyncFunction<D, T>, Executor {
  private final Provider<ProductionComponentMonitor> monitorProvider;
  @NullableDecl private final ProducerToken token;
  private final Provider<Executor> executorProvider;
  private volatile ProducerMonitor monitor = null;

  protected AbstractProducesMethodProducer(
      Provider<ProductionComponentMonitor> monitorProvider,
      @NullableDecl ProducerToken token,
      Provider<Executor> executorProvider) {
    this.monitorProvider = checkNotNull(monitorProvider);
    this.token = token;
    this.executorProvider = checkNotNull(executorProvider);
  }

  @Override
  protected final ListenableFuture<T> compute() {
    monitor = monitorProvider.get().producerMonitorFor(token);
    monitor.requested();
    ListenableFuture<T> result = Futures.transformAsync(collectDependencies(), this, this);
    monitor.addCallbackTo(result);
    return result;
  }

  /**
   * Collects the asynchronous dependencies to be passed to {@link
   * Futures#transformAsync(ListenableFuture, AsyncFunction, Executor)}.
   */
  protected abstract ListenableFuture<D> collectDependencies();

  /** @deprecated this may only be called from the internal {@link #compute()} */
  @Deprecated
  @Override
  public final ListenableFuture<T> apply(D asyncDependencies) throws Exception {
    // NOTE(beder): We don't worry about catching exceptions from the monitor methods themselves
    // because we'll wrap all monitoring in non-throwing monitors before we pass them to the
    // factories.
    monitor.methodStarting();
    try {
      return callProducesMethod(asyncDependencies);
    } finally {
      monitor.methodFinished();
    }
  }

  /**
   * Calls the {@link dagger.producers.Produces} method. This will always be called on the {@link
   * Executor} provided to this producer.
   */
  protected abstract ListenableFuture<T> callProducesMethod(D asyncDependencies) throws Exception;

  /** @deprecated this may only be called from the internal {@link #compute()} */
  @Deprecated
  @Override
  public final void execute(Runnable runnable) {
    monitor.ready();
    executorProvider.get().execute(runnable);
  }
}