View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or 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    *       https://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 org.mybatis.generator.api.dom.java;
17  
18  public class PrimitiveTypeWrapper extends FullyQualifiedJavaType {
19      private static PrimitiveTypeWrapper booleanInstance;
20      private static PrimitiveTypeWrapper byteInstance;
21      private static PrimitiveTypeWrapper characterInstance;
22      private static PrimitiveTypeWrapper doubleInstance;
23      private static PrimitiveTypeWrapper floatInstance;
24      private static PrimitiveTypeWrapper integerInstance;
25      private static PrimitiveTypeWrapper longInstance;
26      private static PrimitiveTypeWrapper shortInstance;
27  
28      private final String toPrimitiveMethod;
29  
30      /**
31       * Use the static getXXXInstance methods to gain access to one of the type
32       * wrappers.
33       *
34       * @param fullyQualifiedName
35       *            fully qualified name of the wrapper type
36       * @param toPrimitiveMethod
37       *            the method that returns the wrapped primitive
38       */
39      private PrimitiveTypeWrapper(String fullyQualifiedName,
40              String toPrimitiveMethod) {
41          super(fullyQualifiedName);
42          this.toPrimitiveMethod = toPrimitiveMethod;
43      }
44  
45      public String getToPrimitiveMethod() {
46          return toPrimitiveMethod;
47      }
48  
49      @Override
50      public boolean equals(Object obj) {
51          if (this == obj) {
52              return true;
53          }
54  
55          if (!(obj instanceof PrimitiveTypeWrapper)) {
56              return false;
57          }
58  
59          PrimitiveTypeWrapper other = (PrimitiveTypeWrapper) obj;
60  
61          return getFullyQualifiedName().equals(other.getFullyQualifiedName());
62      }
63  
64      @Override
65      public int hashCode() {
66          return getFullyQualifiedName().hashCode();
67      }
68  
69      public static PrimitiveTypeWrapper getBooleanInstance() {
70          if (booleanInstance == null) {
71              booleanInstance = new PrimitiveTypeWrapper("java.lang.Boolean", //$NON-NLS-1$
72                      "booleanValue()"); //$NON-NLS-1$
73          }
74  
75          return booleanInstance;
76      }
77  
78      public static PrimitiveTypeWrapper getByteInstance() {
79          if (byteInstance == null) {
80              byteInstance = new PrimitiveTypeWrapper("java.lang.Byte", //$NON-NLS-1$
81                      "byteValue()"); //$NON-NLS-1$
82          }
83  
84          return byteInstance;
85      }
86  
87      public static PrimitiveTypeWrapper getCharacterInstance() {
88          if (characterInstance == null) {
89              characterInstance = new PrimitiveTypeWrapper("java.lang.Character", //$NON-NLS-1$
90                      "charValue()"); //$NON-NLS-1$
91          }
92  
93          return characterInstance;
94      }
95  
96      public static PrimitiveTypeWrapper getDoubleInstance() {
97          if (doubleInstance == null) {
98              doubleInstance = new PrimitiveTypeWrapper("java.lang.Double", //$NON-NLS-1$
99                      "doubleValue()"); //$NON-NLS-1$
100         }
101 
102         return doubleInstance;
103     }
104 
105     public static PrimitiveTypeWrapper getFloatInstance() {
106         if (floatInstance == null) {
107             floatInstance = new PrimitiveTypeWrapper("java.lang.Float", //$NON-NLS-1$
108                     "floatValue()"); //$NON-NLS-1$
109         }
110 
111         return floatInstance;
112     }
113 
114     public static PrimitiveTypeWrapper getIntegerInstance() {
115         if (integerInstance == null) {
116             integerInstance = new PrimitiveTypeWrapper("java.lang.Integer", //$NON-NLS-1$
117                     "intValue()"); //$NON-NLS-1$
118         }
119 
120         return integerInstance;
121     }
122 
123     public static PrimitiveTypeWrapper getLongInstance() {
124         if (longInstance == null) {
125             longInstance = new PrimitiveTypeWrapper("java.lang.Long", //$NON-NLS-1$
126                     "longValue()"); //$NON-NLS-1$
127         }
128 
129         return longInstance;
130     }
131 
132     public static PrimitiveTypeWrapper getShortInstance() {
133         if (shortInstance == null) {
134             shortInstance = new PrimitiveTypeWrapper("java.lang.Short", //$NON-NLS-1$
135                     "shortValue()"); //$NON-NLS-1$
136         }
137 
138         return shortInstance;
139     }
140 }