/* * Copyright (c) 2007 Mockito contributors * This program is made available under the terms of the MIT License. */ package org.mockito.internal.util; import java.util.HashMap; import java.util.Map; @SuppressWarnings("unchecked") public class Primitives { private static Map, Class> primitiveTypes = new HashMap, Class>(); private static Map, Object> primitiveOrWrapperDefaultValues = new HashMap, Object>(); /** * Returns the primitive type of the given class. *

* The passed class can be any class : boolean.class, Integer.class * in witch case this method will return boolean.class, even SomeObject.class * in which case null will be returned. * * @param clazz The class from which primitive type has to be retrieved * @param The type * @return The primitive type if relevant, otherwise null */ public static Class primitiveTypeOf(Class clazz) { if (clazz.isPrimitive()) { return clazz; } return (Class) primitiveTypes.get(clazz); } /** * Indicates if the given class is primitive type or a primitive wrapper. * * @param type The type to check * @return true if primitive or wrapper, false otherwise. */ public static boolean isPrimitiveOrWrapper(Class type) { return primitiveOrWrapperDefaultValues.containsKey(type); } /** * Returns the boxed default value for a primitive or a primitive wrapper. * * @param primitiveOrWrapperType The type to lookup the default value * @return The boxed default values as defined in Java Language Specification, * null if the type is neither a primitive nor a wrapper */ public static T defaultValueForPrimitiveOrWrapper(Class primitiveOrWrapperType) { return (T) primitiveOrWrapperDefaultValues.get(primitiveOrWrapperType); } static { primitiveTypes.put(Boolean.class, Boolean.TYPE); primitiveTypes.put(Character.class, Character.TYPE); primitiveTypes.put(Byte.class, Byte.TYPE); primitiveTypes.put(Short.class, Short.TYPE); primitiveTypes.put(Integer.class, Integer.TYPE); primitiveTypes.put(Long.class, Long.TYPE); primitiveTypes.put(Float.class, Float.TYPE); primitiveTypes.put(Double.class, Double.TYPE); } static { primitiveOrWrapperDefaultValues.put(Boolean.class, false); primitiveOrWrapperDefaultValues.put(Character.class, '\u0000'); primitiveOrWrapperDefaultValues.put(Byte.class, (byte) 0); primitiveOrWrapperDefaultValues.put(Short.class, (short) 0); primitiveOrWrapperDefaultValues.put(Integer.class, 0); primitiveOrWrapperDefaultValues.put(Long.class, 0L); primitiveOrWrapperDefaultValues.put(Float.class, 0F); primitiveOrWrapperDefaultValues.put(Double.class, 0D); primitiveOrWrapperDefaultValues.put(boolean.class, false); primitiveOrWrapperDefaultValues.put(char.class, '\u0000'); primitiveOrWrapperDefaultValues.put(byte.class, (byte) 0); primitiveOrWrapperDefaultValues.put(short.class, (short) 0); primitiveOrWrapperDefaultValues.put(int.class, 0); primitiveOrWrapperDefaultValues.put(long.class, 0L); primitiveOrWrapperDefaultValues.put(float.class, 0F); primitiveOrWrapperDefaultValues.put(double.class, 0D); } }