• 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 static com.google.common.base.Preconditions.checkArgument;
20 
21 import com.google.auto.common.MoreTypes;
22 import com.google.auto.value.AutoValue;
23 import com.google.common.base.Equivalence;
24 import dagger.model.Key;
25 import java.util.Set;
26 import javax.lang.model.type.DeclaredType;
27 import javax.lang.model.type.TypeMirror;
28 
29 /**
30  * Information about a {@link Set} {@link TypeMirror}.
31  */
32 @AutoValue
33 public abstract class SetType {
34   /**
35    * The set type itself, wrapped using {@link MoreTypes#equivalence()}. Use
36    * {@link #declaredSetType()} instead.
37    */
wrappedDeclaredSetType()38   protected abstract Equivalence.Wrapper<DeclaredType> wrappedDeclaredSetType();
39 
40   /**
41    * The set type itself.
42    */
declaredSetType()43   private DeclaredType declaredSetType() {
44     return wrappedDeclaredSetType().get();
45   }
46 
47   /**
48    * {@code true} if the set type is the raw {@link Set} type.
49    */
isRawType()50   public boolean isRawType() {
51     return declaredSetType().getTypeArguments().isEmpty();
52   }
53 
54   /**
55    * The element type.
56    */
elementType()57   public TypeMirror elementType() {
58     return declaredSetType().getTypeArguments().get(0);
59   }
60 
61   /**
62    * {@code true} if {@link #elementType()} is a {@code clazz}.
63    */
elementsAreTypeOf(Class<?> clazz)64   public boolean elementsAreTypeOf(Class<?> clazz) {
65     return MoreTypes.isType(elementType()) && MoreTypes.isTypeOf(clazz, elementType());
66   }
67 
68   /**
69    * {@code T} if {@link #elementType()} is a {@code WrappingClass<T>}.
70    *
71    * @throws IllegalStateException if {@link #elementType()} is not a {@code WrappingClass<T>}
72    * @throws IllegalArgumentException if {@code wrappingClass} does not have exactly one type
73    *     parameter
74    */
unwrappedElementType(Class<?> wrappingClass)75   public TypeMirror unwrappedElementType(Class<?> wrappingClass) {
76     checkArgument(
77         wrappingClass.getTypeParameters().length == 1,
78         "%s must have exactly one type parameter",
79         wrappingClass);
80     checkArgument(
81         elementsAreTypeOf(wrappingClass),
82         "expected elements to be %s, but this type is %s",
83         wrappingClass,
84         declaredSetType());
85     return MoreTypes.asDeclared(elementType()).getTypeArguments().get(0);
86   }
87 
88   /**
89    * {@code true} if {@code type} is a {@link Set} type.
90    */
isSet(TypeMirror type)91   public static boolean isSet(TypeMirror type) {
92     return MoreTypes.isType(type) && MoreTypes.isTypeOf(Set.class, type);
93   }
94 
95   /**
96    * {@code true} if {@code key.type()} is a {@link Set} type.
97    */
isSet(Key key)98   public static boolean isSet(Key key) {
99     return isSet(key.type());
100   }
101 
102   /**
103    * Returns a {@link SetType} for {@code type}.
104    *
105    * @throws IllegalArgumentException if {@code type} is not a {@link Set} type
106    */
from(TypeMirror type)107   public static SetType from(TypeMirror type) {
108     checkArgument(isSet(type), "%s must be a Set", type);
109     return new AutoValue_SetType(MoreTypes.equivalence().wrap(MoreTypes.asDeclared(type)));
110   }
111 
112   /**
113    * Returns a {@link SetType} for {@code key}'s {@link Key#type() type}.
114    *
115    * @throws IllegalArgumentException if {@code key.type()} is not a {@link Set} type
116    */
from(Key key)117   public static SetType from(Key key) {
118     return from (key.type());
119   }
120 }
121