View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.util;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  public final class AutoBoxing {
14      private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = new HashMap<>();
15      private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE = new HashMap<>();
16  
17      static {
18          WRAPPER_TO_PRIMITIVE.put(Boolean.class, boolean.class);
19          WRAPPER_TO_PRIMITIVE.put(Character.class, char.class);
20          WRAPPER_TO_PRIMITIVE.put(Byte.class, byte.class);
21          WRAPPER_TO_PRIMITIVE.put(Short.class, short.class);
22          WRAPPER_TO_PRIMITIVE.put(Integer.class, int.class);
23          WRAPPER_TO_PRIMITIVE.put(Float.class, float.class);
24          WRAPPER_TO_PRIMITIVE.put(Long.class, long.class);
25          WRAPPER_TO_PRIMITIVE.put(Double.class, double.class);
26  
27          PRIMITIVE_TO_WRAPPER.put(boolean.class, Boolean.class);
28          PRIMITIVE_TO_WRAPPER.put(char.class, Character.class);
29          PRIMITIVE_TO_WRAPPER.put(byte.class, Byte.class);
30          PRIMITIVE_TO_WRAPPER.put(short.class, Short.class);
31          PRIMITIVE_TO_WRAPPER.put(int.class, Integer.class);
32          PRIMITIVE_TO_WRAPPER.put(float.class, Float.class);
33          PRIMITIVE_TO_WRAPPER.put(long.class, Long.class);
34          PRIMITIVE_TO_WRAPPER.put(double.class, Double.class);
35      }
36  
37      private AutoBoxing() {
38      }
39  
40      public static boolean isWrapperOfPrimitiveType(@NonNull Class<?> type) {
41          return WRAPPER_TO_PRIMITIVE.containsKey(type);
42      }
43  
44      @Nullable
45      public static Class<?> getPrimitiveType(@NonNull Class<?> wrapperType) {
46          return WRAPPER_TO_PRIMITIVE.get(wrapperType);
47      }
48  
49      @Nullable
50      public static Class<?> getWrapperType(@NonNull Class<?> primitiveType) {
51          return PRIMITIVE_TO_WRAPPER.get(primitiveType);
52      }
53  }