• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.util;
6 
7 import java.util.HashMap;
8 import java.util.Map;
9 
10 @SuppressWarnings("unchecked")
11 public final class Primitives {
12 
13     private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = new HashMap<>();
14     private static final Map<Class<?>, Object> PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES =
15             new HashMap<>();
16 
17     /**
18      * Returns the primitive type of the given class.
19      * <p/>
20      * The passed class can be any class : <code>boolean.class</code>, <code>Integer.class</code>
21      * in witch case this method will return <code>boolean.class</code>, even <code>SomeObject.class</code>
22      * in which case <code>null</code> will be returned.
23      *
24      * @param clazz The class from which primitive type has to be retrieved
25      * @param <T>   The type
26      * @return The primitive type if relevant, otherwise <code>null</code>
27      */
primitiveTypeOf(Class<T> clazz)28     public static <T> Class<T> primitiveTypeOf(Class<T> clazz) {
29         if (clazz.isPrimitive()) {
30             return clazz;
31         }
32         return (Class<T>) PRIMITIVE_TYPES.get(clazz);
33     }
34 
35     /**
36      * Indicates if the given class is primitive type or a primitive wrapper.
37      *
38      * @param type The type to check
39      * @return <code>true</code> if primitive or wrapper, <code>false</code> otherwise.
40      */
isPrimitiveOrWrapper(Class<?> type)41     public static boolean isPrimitiveOrWrapper(Class<?> type) {
42         return PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.containsKey(type);
43     }
44 
isAssignableFromWrapper(Class<?> valueClass, Class<?> referenceType)45     public static boolean isAssignableFromWrapper(Class<?> valueClass, Class<?> referenceType) {
46         if (isPrimitiveOrWrapper(valueClass) && isPrimitiveOrWrapper(referenceType)) {
47             return Primitives.primitiveTypeOf(valueClass)
48                     .isAssignableFrom(Primitives.primitiveTypeOf(referenceType));
49         }
50         return false;
51     }
52 
53     /**
54      * Returns the boxed default value for a primitive or a primitive wrapper.
55      *
56      * @param primitiveOrWrapperType The type to lookup the default value
57      * @return The boxed default values as defined in Java Language Specification,
58      *         <code>null</code> if the type is neither a primitive nor a wrapper
59      */
defaultValue(Class<T> primitiveOrWrapperType)60     public static <T> T defaultValue(Class<T> primitiveOrWrapperType) {
61         return (T) PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.get(primitiveOrWrapperType);
62     }
63 
64     static {
PRIMITIVE_TYPES.put(Boolean.class, Boolean.TYPE)65         PRIMITIVE_TYPES.put(Boolean.class, Boolean.TYPE);
PRIMITIVE_TYPES.put(Character.class, Character.TYPE)66         PRIMITIVE_TYPES.put(Character.class, Character.TYPE);
PRIMITIVE_TYPES.put(Byte.class, Byte.TYPE)67         PRIMITIVE_TYPES.put(Byte.class, Byte.TYPE);
PRIMITIVE_TYPES.put(Short.class, Short.TYPE)68         PRIMITIVE_TYPES.put(Short.class, Short.TYPE);
PRIMITIVE_TYPES.put(Integer.class, Integer.TYPE)69         PRIMITIVE_TYPES.put(Integer.class, Integer.TYPE);
PRIMITIVE_TYPES.put(Long.class, Long.TYPE)70         PRIMITIVE_TYPES.put(Long.class, Long.TYPE);
PRIMITIVE_TYPES.put(Float.class, Float.TYPE)71         PRIMITIVE_TYPES.put(Float.class, Float.TYPE);
PRIMITIVE_TYPES.put(Double.class, Double.TYPE)72         PRIMITIVE_TYPES.put(Double.class, Double.TYPE);
73     }
74 
75     static {
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Boolean.class, false)76         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Boolean.class, false);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Character.class, '\\u0000')77         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Character.class, '\u0000');
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Byte.class, (byte) 0)78         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Byte.class, (byte) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Short.class, (short) 0)79         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Short.class, (short) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Integer.class, 0)80         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Integer.class, 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Long.class, 0L)81         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Long.class, 0L);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Float.class, 0F)82         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Float.class, 0F);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Double.class, 0D)83         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Double.class, 0D);
84 
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(boolean.class, false)85         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(boolean.class, false);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(char.class, '\\u0000')86         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(char.class, '\u0000');
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(byte.class, (byte) 0)87         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(byte.class, (byte) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(short.class, (short) 0)88         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(short.class, (short) 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(int.class, 0)89         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(int.class, 0);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(long.class, 0L)90         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(long.class, 0L);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(float.class, 0F)91         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(float.class, 0F);
PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(double.class, 0D)92         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(double.class, 0D);
93     }
94 
Primitives()95     private Primitives() {}
96 }
97