1/*
2* Copyright (C) 2015 Google, Inc.
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*/
16package producerstest;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.util.concurrent.Futures;
20import com.google.common.util.concurrent.ListenableFuture;
21import com.google.common.util.concurrent.SettableFuture;
22import dagger.producers.Produced;
23import dagger.producers.Producer;
24import dagger.producers.ProducerModule;
25import dagger.producers.Produces;
26import java.io.IOException;
27import java.util.Set;
28import javax.inject.Provider;
29import javax.inject.Qualifier;
30
31import static dagger.producers.Produces.Type.SET;
32import static dagger.producers.Produces.Type.SET_VALUES;
33
34/**
35 * A module that contains various signatures of produces methods. This is not used in any
36 * components.
37 */
38@ProducerModule
39final class SimpleProducerModule {
40  @Qualifier @interface Qual {
41    int value();
42  }
43
44  // Unique bindings.
45
46  @Produces
47  @Qual(-2)
48  static ListenableFuture<String> throwingProducer() {
49    throw new RuntimeException("monkey");
50  }
51
52  @Produces
53  @Qual(-1)
54  static ListenableFuture<String> settableFutureStr(SettableFuture<String> future) {
55    return future;
56  }
57
58  @Produces
59  @Qual(0)
60  static String str() {
61    return "str";
62  }
63
64  @Produces
65  @Qual(1)
66  static ListenableFuture<String> futureStr() {
67    return Futures.immediateFuture("future str");
68  }
69
70  @Produces
71  @Qual(2)
72  static String strWithArg(int i) {
73    return "str with arg";
74  }
75
76  @Produces
77  @Qual(3)
78  static ListenableFuture<String> futureStrWithArg(int i) {
79    return Futures.immediateFuture("future str with arg");
80  }
81
82  @Produces
83  @Qual(4)
84  static String strThrowingException() throws IOException {
85    return "str throwing exception";
86  }
87
88  @Produces
89  @Qual(5)
90  static ListenableFuture<String> futureStrThrowingException() throws IOException {
91    return Futures.immediateFuture("future str throwing exception");
92  }
93
94  @Produces
95  @Qual(6)
96  static String strWithArgThrowingException(int i) throws IOException {
97    return "str with arg throwing exception";
98  }
99
100  @Produces
101  @Qual(7)
102  static ListenableFuture<String> futureStrWithArgThrowingException(int i) throws IOException {
103    return Futures.immediateFuture("future str with arg throwing exception");
104  }
105
106  @Produces
107  @Qual(8)
108  static String strWithArgs(int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) {
109    return "str with args";
110  }
111
112  @Produces
113  @Qual(9)
114  static String strWithArgsThrowingException(
115      int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) throws IOException {
116    return "str with args throwing exception";
117  }
118
119  @Produces
120  @Qual(10)
121  static ListenableFuture<String> futureStrWithArgs(
122      int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) {
123    return Futures.immediateFuture("future str with args");
124  }
125
126  @Produces
127  @Qual(11)
128  static ListenableFuture<String> futureStrWithArgsThrowingException(
129      int i, Produced<Double> b, Producer<Object> c, Provider<Boolean> d) throws IOException {
130    return Futures.immediateFuture("str with args throwing exception");
131  }
132
133  // Set bindings.
134
135  @Produces(type = SET)
136  static String setOfStrElement() {
137    return "set of str element";
138  }
139
140  @Produces(type = SET)
141  static String setOfStrElementThrowingException() throws IOException {
142    return "set of str element throwing exception";
143  }
144
145  @Produces(type = SET)
146  static ListenableFuture<String> setOfStrFutureElement() {
147    return Futures.immediateFuture("set of str element");
148  }
149
150  @Produces(type = SET)
151  static ListenableFuture<String> setOfStrFutureElementThrowingException() throws IOException {
152    return Futures.immediateFuture("set of str element throwing exception");
153  }
154
155  @Produces(type = SET)
156  static String setOfStrElementWithArg(int i) {
157    return "set of str element with arg";
158  }
159
160  @Produces(type = SET)
161  static String setOfStrElementWithArgThrowingException(int i) throws IOException {
162    return "set of str element with arg throwing exception";
163  }
164
165  @Produces(type = SET)
166  static ListenableFuture<String> setOfStrFutureElementWithArg(int i) {
167    return Futures.immediateFuture("set of str element with arg");
168  }
169
170  @Produces(type = SET)
171  static ListenableFuture<String> setOfStrFutureElementWithArgThrowingException(int i)
172      throws IOException {
173    return Futures.immediateFuture("set of str element with arg throwing exception");
174  }
175
176  @Produces(type = SET_VALUES)
177  static Set<String> setOfStrValues() {
178    return ImmutableSet.of("set of str 1", "set of str 2");
179  }
180
181  @Produces(type = SET_VALUES)
182  static Set<String> setOfStrValuesThrowingException() throws IOException {
183    return ImmutableSet.of("set of str 1", "set of str 2 throwing exception");
184  }
185
186  @Produces(type = SET_VALUES)
187  static ListenableFuture<Set<String>> setOfStrFutureValues() {
188    return Futures.<Set<String>>immediateFuture(ImmutableSet.of("set of str 1", "set of str 2"));
189  }
190
191  @Produces(type = SET_VALUES)
192  static ListenableFuture<Set<String>> setOfStrFutureValuesThrowingException() throws IOException {
193    return Futures.<Set<String>>immediateFuture(
194        ImmutableSet.of("set of str 1", "set of str 2 throwing exception"));
195  }
196
197  @Produces(type = SET_VALUES)
198  static Set<String> setOfStrValuesWithArg(int i) {
199    return ImmutableSet.of("set of str with arg 1", "set of str with arg 2");
200  }
201
202  @Produces(type = SET_VALUES)
203  static Set<String> setOfStrValuesWithArgThrowingException(int i) throws IOException {
204    return ImmutableSet.of("set of str with arg 1", "set of str with arg 2 throwing exception");
205  }
206
207  @Produces(type = SET_VALUES)
208  static ListenableFuture<Set<String>> setOfStrFutureValuesWithArg(int i) {
209    return Futures.<Set<String>>immediateFuture(
210        ImmutableSet.of("set of str with arg 1", "set of str with arg 2"));
211  }
212
213  @Produces(type = SET_VALUES)
214  static ListenableFuture<Set<String>> setOfStrFutureValuesWithArgThrowingException(int i)
215      throws IOException {
216    return Futures.<Set<String>>immediateFuture(
217        ImmutableSet.of("set of str with arg 1", "set of str with arg 2 throwing exception"));
218  }
219}
220