• 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 import static dagger.internal.codegen.xprocessing.XTypes.isTypeOf;
21 import static dagger.internal.codegen.xprocessing.XTypes.unwrapType;
22 
23 import androidx.room.compiler.processing.XType;
24 import com.google.auto.value.AutoValue;
25 import com.squareup.javapoet.ClassName;
26 import com.squareup.javapoet.TypeName;
27 import dagger.internal.codegen.javapoet.TypeNames;
28 import dagger.internal.codegen.model.Key;
29 import dagger.internal.codegen.xprocessing.XTypes;
30 
31 /** Information about a {@link java.util.Set} type. */
32 @AutoValue
33 public abstract class SetType {
34   private XType type;
35 
36   /** The set type itself. */
typeName()37   abstract TypeName typeName();
38 
39   /** The set type itself. */
type()40   private XType type() {
41     return type;
42   }
43 
44   /** {@code true} if the set type is the raw {@link java.util.Set} type. */
isRawType()45   public boolean isRawType() {
46     return XTypes.isRawParameterizedType(type());
47   }
48 
49   /** Returns the element type. */
elementType()50   public XType elementType() {
51     return unwrapType(type());
52   }
53 
54   /** Returns {@code true} if {@link #elementType()} is of type {@code className}. */
elementsAreTypeOf(ClassName className)55   public boolean elementsAreTypeOf(ClassName className) {
56     return !isRawType() && isTypeOf(elementType(), className);
57   }
58 
59   /**
60    * {@code T} if {@link #elementType()} is a {@code WrappingClass<T>}.
61    *
62    * @throws IllegalStateException if {@link #elementType()} is not a {@code WrappingClass<T>}
63    */
64   // TODO(b/202033221): Consider using stricter input type, e.g. FrameworkType.
unwrappedElementType(ClassName wrappingClass)65   public XType unwrappedElementType(ClassName wrappingClass) {
66     checkArgument(
67         elementsAreTypeOf(wrappingClass),
68         "expected elements to be %s, but this type is %s",
69         wrappingClass,
70         type());
71     return unwrapType(elementType());
72   }
73 
74   /** {@code true} if {@code type} is a {@link java.util.Set} type. */
isSet(XType type)75   public static boolean isSet(XType type) {
76     return isTypeOf(type, TypeNames.SET);
77   }
78 
79   /** {@code true} if {@code key.type()} is a {@link java.util.Set} type. */
isSet(Key key)80   public static boolean isSet(Key key) {
81     return isSet(key.type().xprocessing());
82   }
83 
84   /**
85    * Returns a {@link SetType} for {@code type}.
86    *
87    * @throws IllegalArgumentException if {@code type} is not a {@link java.util.Set} type
88    */
from(XType type)89   public static SetType from(XType type) {
90     checkArgument(isSet(type), "%s must be a Set", type);
91     SetType setType = new AutoValue_SetType(type.getTypeName());
92     setType.type = type;
93     return setType;
94   }
95 
96   /**
97    * Returns a {@link SetType} for {@code key}'s {@link Key#type() type}.
98    *
99    * @throws IllegalArgumentException if {@code key.type()} is not a {@link java.util.Set} type
100    */
from(Key key)101   public static SetType from(Key key) {
102     return from(key.type().xprocessing());
103   }
104 }
105