Home | History | Annotate | Download | only in util
      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 class Primitives {
     12 
     13     private static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES = new HashMap<Class<?>, Class<?>>();
     14     private static final Map<Class<?>, Object> PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES = new HashMap<Class<?>, Object>();
     15 
     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      */
     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      */
     41     public static boolean isPrimitiveOrWrapper(Class<?> type) {
     42         return PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.containsKey(type);
     43     }
     44 
     45     public static boolean isAssignableFromWrapper(Class<?> valueClass, Class<?> referenceType) {
     46         if(isPrimitiveOrWrapper(valueClass) && isPrimitiveOrWrapper(referenceType)) {
     47             return Primitives.primitiveTypeOf(valueClass).isAssignableFrom(Primitives.primitiveTypeOf(referenceType));
     48         }
     49         return false;
     50     }
     51 
     52     /**
     53      * Returns the boxed default value for a primitive or a primitive wrapper.
     54      *
     55      * @param primitiveOrWrapperType The type to lookup the default value
     56      * @return The boxed default values as defined in Java Language Specification,
     57      *         <code>null</code> if the type is neither a primitive nor a wrapper
     58      */
     59     public static <T> T defaultValue(Class<T> primitiveOrWrapperType) {
     60         return (T) PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.get(primitiveOrWrapperType);
     61     }
     62 
     63 
     64     static {
     65         PRIMITIVE_TYPES.put(Boolean.class, Boolean.TYPE);
     66         PRIMITIVE_TYPES.put(Character.class, Character.TYPE);
     67         PRIMITIVE_TYPES.put(Byte.class, Byte.TYPE);
     68         PRIMITIVE_TYPES.put(Short.class, Short.TYPE);
     69         PRIMITIVE_TYPES.put(Integer.class, Integer.TYPE);
     70         PRIMITIVE_TYPES.put(Long.class, Long.TYPE);
     71         PRIMITIVE_TYPES.put(Float.class, Float.TYPE);
     72         PRIMITIVE_TYPES.put(Double.class, Double.TYPE);
     73     }
     74 
     75     static {
     76         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Boolean.class, false);
     77         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Character.class, '\u0000');
     78         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Byte.class, (byte) 0);
     79         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Short.class, (short) 0);
     80         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Integer.class, 0);
     81         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Long.class, 0L);
     82         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Float.class, 0F);
     83         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Double.class, 0D);
     84 
     85         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(boolean.class, false);
     86         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(char.class, '\u0000');
     87         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(byte.class, (byte) 0);
     88         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(short.class, (short) 0);
     89         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(int.class, 0);
     90         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(long.class, 0L);
     91         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(float.class, 0F);
     92         PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(double.class, 0D);
     93     }
     94 }
     95