• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.internal.codegen.base;
18 
19 import androidx.room.compiler.processing.XElement;
20 import dagger.internal.codegen.javapoet.TypeNames;
21 
22 /** Whether a binding or declaration is for a unique contribution or a map or set multibinding. */
23 public enum ContributionType {
24   /** Represents map bindings. */
25   MAP,
26   /** Represents set bindings. */
27   SET,
28   /** Represents set values bindings. */
29   SET_VALUES,
30   /** Represents a valid non-collection binding. */
31   UNIQUE,
32   ;
33 
34   /** An object that is associated with a {@link ContributionType}. */
35   public interface HasContributionType {
36 
37     /** The contribution type of this object. */
contributionType()38     ContributionType contributionType();
39   }
40 
41   /** {@code true} if this is for a multibinding. */
isMultibinding()42   public boolean isMultibinding() {
43     return !this.equals(UNIQUE);
44   }
45 
46   /**
47    * The contribution type from a binding element's annotations. Presumes a well-formed binding
48    * element (at most one of @IntoSet, @IntoMap, and @ElementsIntoSet). {@link
49    * dagger.internal.codegen.validation.BindingMethodValidator} and {@link
50    * dagger.internal.codegen.validation.BindsInstanceProcessingStep} validate correctness on their
51    * own.
52    */
fromBindingElement(XElement element)53   public static ContributionType fromBindingElement(XElement element) {
54     if (element.hasAnnotation(TypeNames.INTO_MAP)) {
55       return ContributionType.MAP;
56     } else if (element.hasAnnotation(TypeNames.INTO_SET)) {
57       return ContributionType.SET;
58     } else if (element.hasAnnotation(TypeNames.ELEMENTS_INTO_SET)) {
59       return ContributionType.SET_VALUES;
60     }
61     return ContributionType.UNIQUE;
62   }
63 }
64