blob: 2fc3374d23432486367a0dd0ab18514925c34fa9 [file] [log] [blame]
sameb0b334612015-02-20 10:44:10 -08001/**
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
17package com.google.inject.multibindings;
18
19import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
20import static java.lang.annotation.RetentionPolicy.RUNTIME;
21
22import java.lang.annotation.Documented;
23import java.lang.annotation.Retention;
24import 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}.
44 * When unwrapValue is false, the annotation type will be the key type for the injected map and
45 * the annotation instances will be the key values. If {@code unwrapValue=true}, the value() type
46 * will be the key type for injected map and the value() instances will be the keys values.
47 */
48@Documented
49@Target(ANNOTATION_TYPE)
50@Retention(RUNTIME)
51public @interface MapKey {
52 /**
53 * if {@code unwrapValue} is false, then the whole annotation will be the type and annotation
54 * instances will be the keys. If {@code unwrapValue} is true, the value() type of key type
55 * annotation will be the key type for injected map and the value instances will be the keys.
56 */
sameb6c858432015-03-31 13:42:40 -070057 boolean unwrapValue() default true;
sameb0b334612015-02-20 10:44:10 -080058}