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