All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.util.Primitives Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/*
 * 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 {
    
    public static boolean isPrimitiveWrapper(Class type) {
        return wrapperReturnValues.containsKey(type);
    }
    
    public static  T primitiveWrapperOf(Class type) {
        return (T) wrapperReturnValues.get(type);
    }
    
    public static  Class primitiveTypeOf(Class clazz) {
        return (Class) primitiveTypes.get(clazz);
    }
    
    public static  T primitiveValueOrNullFor(Class primitiveType) {
        return (T) primitiveValues.get(primitiveType);
    }
    
    private static Map, Object> wrapperReturnValues = new HashMap, Object>();
    private static Map, Class> primitiveTypes = new HashMap, Class>();
    private static Map, Object> primitiveValues = new HashMap, Object>();
    
    static {
        wrapperReturnValues.put(Boolean.class, Boolean.FALSE);
        wrapperReturnValues.put(Character.class, new Character((char) 0));
        wrapperReturnValues.put(Byte.class, new Byte((byte) 0));
        wrapperReturnValues.put(Short.class, new Short((short) 0));
        wrapperReturnValues.put(Integer.class, new Integer(0));
        wrapperReturnValues.put(Long.class, new Long(0));
        wrapperReturnValues.put(Float.class, new Float(0));
        wrapperReturnValues.put(Double.class, new Double(0));
    }
    
    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 {
        primitiveValues.put(boolean.class, false);
        primitiveValues.put(char.class, 0);
        primitiveValues.put(byte.class, 0);
        primitiveValues.put(short.class, 0);
        primitiveValues.put(int.class, 0);
        primitiveValues.put(long.class, 0);
        primitiveValues.put(float.class, 0);
        primitiveValues.put(double.class, 0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy