• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package dagger.internal.codegen.writer;
17 
18 import com.google.common.base.Ascii;
19 import com.google.common.collect.ImmutableSet;
20 import java.io.IOException;
21 import java.util.Set;
22 import javax.lang.model.type.PrimitiveType;
23 
24 public enum PrimitiveName implements TypeName {
25   BOOLEAN, BYTE, SHORT, INT, LONG, CHAR, FLOAT, DOUBLE;
26 
27   @Override
referencedClasses()28   public Set<ClassName> referencedClasses() {
29     return ImmutableSet.of();
30   }
31 
32   @Override
toString()33   public String toString() {
34     return Ascii.toLowerCase(name());
35   }
36 
37   @Override
write(Appendable appendable, Context context)38   public Appendable write(Appendable appendable, Context context) throws IOException {
39     return appendable.append(toString());
40   }
41 
forTypeMirror(PrimitiveType mirror)42   static PrimitiveName forTypeMirror(PrimitiveType mirror) {
43     switch (mirror.getKind()) {
44       case BOOLEAN:
45         return BOOLEAN;
46       case BYTE:
47         return BYTE;
48       case SHORT:
49         return SHORT;
50       case INT:
51         return INT;
52       case LONG:
53         return LONG;
54       case CHAR:
55         return CHAR;
56       case FLOAT:
57         return FLOAT;
58       case DOUBLE:
59         return DOUBLE;
60       default:
61         throw new AssertionError();
62     }
63   }
64 
forClass(Class<?> primitiveClass)65   static PrimitiveName forClass(Class<?> primitiveClass) {
66     if (boolean.class.equals(primitiveClass)) {
67       return BOOLEAN;
68     }
69     if (byte.class.equals(primitiveClass)) {
70       return BYTE;
71     }
72     if (short.class.equals(primitiveClass)) {
73       return SHORT;
74     }
75     if (int.class.equals(primitiveClass)) {
76       return INT;
77     }
78     if (long.class.equals(primitiveClass)) {
79       return LONG;
80     }
81     if (char.class.equals(primitiveClass)) {
82       return CHAR;
83     }
84     if (float.class.equals(primitiveClass)) {
85       return FLOAT;
86     }
87     if (double.class.equals(primitiveClass)) {
88       return DOUBLE;
89     }
90     throw new IllegalArgumentException(primitiveClass + " is not a primitive type");
91   }
92 }
93