Skip to content

Commit e50e8d1

Browse files
committed
Reduce static maps in PrimitiveType
1 parent 7cf01f3 commit e50e8d1

File tree

1 file changed

+4
-32
lines changed

1 file changed

+4
-32
lines changed

src/main/java/ch/jalu/typeresolver/primitives/PrimitiveType.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public enum PrimitiveType {
3535
/** Double type pair. */
3636
DOUBLE(double.class, Double.class, 0d);
3737

38-
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_REFERENCE_TYPES = initPrimitiveToReferenceTypesMap();
39-
private static final Map<Class<?>, Class<?>> REFERENCE_TO_PRIMITIVE_TYPES = initReferenceToPrimitiveTypesMap();
4038
private static final Map<Class<?>, PrimitiveType> PRIMITIVES_BY_CLASS = initClassToPrimitiveTypes();
4139

4240
private final Class<?> primitiveType;
@@ -104,8 +102,8 @@ public static PrimitiveType from(Class<?> clazz) {
104102
*/
105103
@SuppressWarnings("unchecked")
106104
public static <T> Class<T> toPrimitiveType(Class<T> clazz) {
107-
Class<?> primitiveType = REFERENCE_TO_PRIMITIVE_TYPES.get(clazz);
108-
return primitiveType == null ? clazz : (Class<T>) primitiveType;
105+
PrimitiveType primitiveTypePair = PRIMITIVES_BY_CLASS.get(clazz);
106+
return primitiveTypePair == null ? clazz : (Class<T>) primitiveTypePair.getPrimitiveType();
109107
}
110108

111109
/**
@@ -120,8 +118,8 @@ public static <T> Class<T> toPrimitiveType(Class<T> clazz) {
120118
*/
121119
@SuppressWarnings("unchecked")
122120
public static <T> Class<T> toReferenceType(Class<T> clazz) {
123-
Class<?> referenceType = PRIMITIVE_TO_REFERENCE_TYPES.get(clazz);
124-
return referenceType == null ? clazz : (Class<T>) referenceType;
121+
PrimitiveType primitiveTypePair = PRIMITIVES_BY_CLASS.get(clazz);
122+
return primitiveTypePair == null ? clazz : (Class<T>) primitiveTypePair.getReferenceType();
125123
}
126124

127125
/**
@@ -139,32 +137,6 @@ public static boolean isRealPrimitive(@Nullable Class<?> clazz) {
139137
// Initialization methods
140138
// -----------
141139

142-
private static Map<Class<?>, Class<?>> initPrimitiveToReferenceTypesMap() {
143-
Map<Class<?>, Class<?>> primToRefTypes = new HashMap<>();
144-
primToRefTypes.put(boolean.class, Boolean.class);
145-
primToRefTypes.put(byte.class, Byte.class);
146-
primToRefTypes.put(char.class, Character.class);
147-
primToRefTypes.put(short.class, Short.class);
148-
primToRefTypes.put(int.class, Integer.class);
149-
primToRefTypes.put(long.class, Long.class);
150-
primToRefTypes.put(float.class, Float.class);
151-
primToRefTypes.put(double.class, Double.class);
152-
return Collections.unmodifiableMap(primToRefTypes);
153-
}
154-
155-
private static Map<Class<?>, Class<?>> initReferenceToPrimitiveTypesMap() {
156-
Map<Class<?>, Class<?>> refToPrimTypes = new HashMap<>();
157-
refToPrimTypes.put(Boolean.class, boolean.class);
158-
refToPrimTypes.put(Byte.class, byte.class);
159-
refToPrimTypes.put(Character.class, char.class);
160-
refToPrimTypes.put(Short.class, short.class);
161-
refToPrimTypes.put(Integer.class, int.class);
162-
refToPrimTypes.put(Long.class, long.class);
163-
refToPrimTypes.put(Float.class, float.class);
164-
refToPrimTypes.put(Double.class, double.class);
165-
return Collections.unmodifiableMap(refToPrimTypes);
166-
}
167-
168140
private static Map<Class<?>, PrimitiveType> initClassToPrimitiveTypes() {
169141
Map<Class<?>, PrimitiveType> classToPrimRefType = new HashMap<>();
170142
for (PrimitiveType type : values()) {

0 commit comments

Comments
 (0)