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  */
16 
17 package com.google.inject.multibindings;
18 
19 import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
20 import static java.lang.annotation.RetentionPolicy.RUNTIME;
21 
22 import java.lang.annotation.Documented;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.Target;
25 
26 /**
27  * Allows users define customized key type annotations for map bindings by annotating an annotation
28  * of a {@code Map}'s key type. The custom key annotation can be applied to methods also annotated
29  * with {@literal @}{@link ProvidesIntoMap}.
30  *
31  * <p>A {@link StringMapKey} and {@link ClassMapKey} are provided for convenience with maps whose
32  * keys are strings or classes. For maps with enums or primitive types as keys, you must provide
33  * your own MapKey annotation, such as this one for an enum:
34  *
35  * <pre>
36  * {@literal @}MapKey(unwrapValue = true)
37  * {@literal @}Retention(RUNTIME)
38  * public {@literal @}interface MyCustomEnumKey {
39  *   MyCustomEnum value();
40  * }
41  * </pre>
42  *
43  * You can also use the whole annotation as the key, if {@code unwrapValue=false}. When unwrapValue
44  * is false, the annotation type will be the key type for the injected map and the annotation
45  * instances will be the key values. If {@code unwrapValue=true}, the value() type will be the key
46  * type for injected map and the value() instances will be the keys values.
47  *
48  * @since 4.0
49  */
50 @Documented
51 @Target(ANNOTATION_TYPE)
52 @Retention(RUNTIME)
53 public @interface MapKey {
54   /**
55    * if {@code unwrapValue} is false, then the whole annotation will be the type and annotation
56    * instances will be the keys. If {@code unwrapValue} is true, the value() type of key type
57    * annotation will be the key type for injected map and the value instances will be the keys.
58    */
unwrapValue()59   boolean unwrapValue() default true;
60 }
61