1 /* 2 * Copyright (C) 2017 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.model; 18 19 import static com.google.common.base.CaseFormat.UPPER_CAMEL; 20 import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE; 21 22 import dagger.Lazy; 23 import dagger.producers.Produced; 24 import dagger.producers.Producer; 25 import javax.inject.Provider; 26 27 /** 28 * Represents the different kinds of {@link javax.lang.model.type.TypeMirror types} that may be 29 * requested as dependencies for the same key. For example, {@code String}, {@code 30 * Provider<String>}, and {@code Lazy<String>} can all be requested if a key exists for {@code 31 * String}; they have the {@link #INSTANCE}, {@link #PROVIDER}, and {@link #LAZY} request kinds, 32 * respectively. 33 */ 34 public enum RequestKind { 35 /** A default request for an instance. E.g.: {@code FooType} */ 36 INSTANCE, 37 38 /** A request for a {@link Provider}. E.g.: {@code Provider<FooType>} */ 39 PROVIDER, 40 41 /** A request for a {@link Lazy}. E.g.: {@code Lazy<FooType>} */ 42 LAZY, 43 44 /** A request for a {@link Provider} of a {@link Lazy}. E.g.: {@code Provider<Lazy<FooType>>} */ 45 PROVIDER_OF_LAZY, 46 47 /** 48 * A request for a members injection. E.g. {@code void injectMembers(FooType);}. Can only be 49 * requested by component interfaces. 50 */ 51 MEMBERS_INJECTION, 52 53 /** A request for a {@link Producer}. E.g.: {@code Producer<FooType>} */ 54 PRODUCER, 55 56 /** A request for a {@link Produced}. E.g.: {@code Produced<FooType>} */ 57 PRODUCED, 58 59 /** 60 * A request for a {@link com.google.common.util.concurrent.ListenableFuture}. E.g.: {@code 61 * ListenableFuture<FooType>}. These can only be requested by component interfaces. 62 */ 63 FUTURE, 64 ; 65 66 /** Returns a string that represents requests of this kind for a key. */ format(Key key)67 public String format(Key key) { 68 switch (this) { 69 case INSTANCE: 70 return key.toString(); 71 72 case PROVIDER_OF_LAZY: 73 return String.format("Provider<Lazy<%s>>", key); 74 75 case MEMBERS_INJECTION: 76 return String.format("injectMembers(%s)", key); 77 78 case FUTURE: 79 return String.format("ListenableFuture<%s>", key); 80 81 default: 82 return String.format("%s<%s>", UPPER_UNDERSCORE.to(UPPER_CAMEL, name()), key); 83 } 84 } 85 } 86