Interface Producer<T>


  • @Beta
    public interface Producer<T>
    An interface that represents the production of a type T. You can also inject Producer<T> instead of T, which will delay the execution of any code that produces the T until get() is called.

    For example, you might inject Producer to lazily choose between several different implementations of some type:

    
       @Produces ListenableFuture<Heater> getHeater(
           HeaterFlag flag,
           @Electric Producer<Heater> electricHeater,
           @Gas Producer<Heater> gasHeater) {
         return flag.useElectricHeater() ? electricHeater.get() : gasHeater.get();
       }
     

    Here is a complete example that demonstrates how calling get() will cause each method to be executed:

    
    
       @ProducerModule
       final class MyModule {
         @Produces ListenableFuture<A> a() {
           System.out.println("a");
           return Futures.immediateFuture(new A());
         }
    
         @Produces ListenableFuture<B> b(A a) {
           System.out.println("b");
           return Futures.immediateFuture(new B(a));
         }
    
         @Produces ListenableFuture<C> c(B b) {
           System.out.println("c");
           return Futures.immediateFuture(new C(b));
         }
    
         @Produces @Delayed ListenableFuture<C> delayedC(A a, Producer<C> c) {
           System.out.println("delayed c");
           return c.get();
         }
       }
    
       @ProductionComponent(modules = MyModule.class)
       interface MyComponent {
         @Delayed ListenableFuture<C> delayedC();
       }
     
    Suppose we instantiate the generated implementation of this component and call delayedC():
    
       MyComponent component = DaggerMyComponent
           .builder()
           .executor(MoreExecutors.directExecutor())
           .build();
       System.out.println("Constructed component");
       ListenableFuture<C> cFuture = component.delayedC();
       System.out.println("Retrieved future");
       C c = cFuture.get();
       System.out.println("Retrieved c");
     
    Here, we're using MoreExecutors.directExecutor in order to illustrate how each call directly causes code to execute. The above code will print:
    
       Constructed component
       a
       delayed c
       b
       c
       Retrieved future
       Retrieved c
     
    Since:
    2.0
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      com.google.common.util.concurrent.ListenableFuture<T> get()
      Returns a future representing a running task that produces a value.
    • Method Detail

      • get

        @CheckReturnValue
        com.google.common.util.concurrent.ListenableFuture<T> get()
        Returns a future representing a running task that produces a value. Calling this method will trigger the submission of this task to the executor, if it has not already been triggered. In order to trigger this task's submission, the transitive dependencies required to produce the T will be submitted to the executor, as their dependencies become available.

        If the key is bound to a Produces method, then calling this method multiple times will return the same future.