1 /* 2 * Copyright (C) 2015 The Dagger Authors. 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 17 package dagger.functional.producers; 18 19 import com.google.common.util.concurrent.Futures; 20 import com.google.common.util.concurrent.ListenableFuture; 21 import dagger.Lazy; 22 import dagger.producers.Produced; 23 import dagger.producers.Producer; 24 import dagger.producers.ProducerModule; 25 import dagger.producers.Produces; 26 import javax.inject.Provider; 27 import javax.inject.Qualifier; 28 29 @ProducerModule(includes = ResponseModule.class) 30 final class ResponseProducerModule { 31 @Qualifier 32 @interface RequestsProducerAndProduced {} 33 34 @Produces greeting()35 static ListenableFuture<String> greeting() { 36 return Futures.immediateFuture("Hello"); 37 } 38 39 @Produces 40 @RequestsProducerAndProduced intermediateGreeting( @uppressWarnings"unused") String greeting, Producer<String> greetingProducer, @SuppressWarnings("unused") Produced<String> greetingProduced, @SuppressWarnings("unused") Provider<Integer> requestNumberProvider, @SuppressWarnings("unused") Lazy<Integer> requestNumberLazy)41 static ListenableFuture<String> intermediateGreeting( 42 // TODO(beder): Allow Producer and Provider of the same type (which would force the binding 43 // to be a provision binding), and add validation for that. 44 @SuppressWarnings("unused") String greeting, 45 Producer<String> greetingProducer, 46 @SuppressWarnings("unused") Produced<String> greetingProduced, 47 @SuppressWarnings("unused") Provider<Integer> requestNumberProvider, 48 @SuppressWarnings("unused") Lazy<Integer> requestNumberLazy) { 49 return greetingProducer.get(); 50 } 51 52 @Produces response( @equestsProducerAndProduced String greeting, Request request, int requestNumber)53 static Response response( 54 @RequestsProducerAndProduced String greeting, Request request, int requestNumber) { 55 return new Response(String.format("%s, %s #%d!", greeting, request.name(), requestNumber)); 56 } 57 } 58