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

org.xpertss.proximo.util.Primitives Maven / Gradle / Ivy

Go to download

Proximo provides a simple declarative means for creating proxies and stubbing method behavior overrides.

The newest version!
/**
 * Copyright 2015 XpertSoftware
 * 

* Created By: cfloersch * Date: 6/7/2015 */ package org.xpertss.proximo.util; import java.util.HashMap; import java.util.Map; @SuppressWarnings("unchecked") public class Primitives { private static final Map, Class> WRAPPER_TYPES = new HashMap, Class>(); private static final Map, Class> PRIMITIVE_TYPES = new HashMap, Class>(); private static final Map, Object> PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES = new HashMap, Object>(); public static Class wrapperTypeFor(Class clazz) { if (!clazz.isPrimitive()) { return clazz; } return (Class) WRAPPER_TYPES.get(clazz); } /** * 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) PRIMITIVE_TYPES.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 PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.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) PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.get(primitiveOrWrapperType); } static { WRAPPER_TYPES.put(Boolean.TYPE, Boolean.class); WRAPPER_TYPES.put(Character.TYPE, Character.class); WRAPPER_TYPES.put(Byte.TYPE, Byte.class); WRAPPER_TYPES.put(Short.TYPE, Short.class); WRAPPER_TYPES.put(Integer.TYPE, Integer.class); WRAPPER_TYPES.put(Long.TYPE, Long.class); WRAPPER_TYPES.put(Float.TYPE, Float.class); WRAPPER_TYPES.put(Double.TYPE, Double.class); WRAPPER_TYPES.put(Void.TYPE, void.class); } static { PRIMITIVE_TYPES.put(Boolean.class, Boolean.TYPE); PRIMITIVE_TYPES.put(Character.class, Character.TYPE); PRIMITIVE_TYPES.put(Byte.class, Byte.TYPE); PRIMITIVE_TYPES.put(Short.class, Short.TYPE); PRIMITIVE_TYPES.put(Integer.class, Integer.TYPE); PRIMITIVE_TYPES.put(Long.class, Long.TYPE); PRIMITIVE_TYPES.put(Float.class, Float.TYPE); PRIMITIVE_TYPES.put(Double.class, Double.TYPE); PRIMITIVE_TYPES.put(void.class, Void.TYPE); } static { PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Boolean.class, false); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Character.class, '\u0000'); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Byte.class, (byte) 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Short.class, (short) 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Integer.class, 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Long.class, 0L); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Float.class, 0F); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(Double.class, 0D); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(boolean.class, false); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(char.class, '\u0000'); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(byte.class, (byte) 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(short.class, (short) 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(int.class, 0); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(long.class, 0L); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(float.class, 0F); PRIMITIVE_OR_WRAPPER_DEFAULT_VALUES.put(double.class, 0D); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy