Java Class to Primitive primitiveToWrapper(Class in)

Here you can find the source of primitiveToWrapper(Class in)

Description

convert the input class to a wrapper class returns in if the argument is not primitive

License

Apache License

Parameter

Parameter Description
in non-null class

Return

non-null class guaranteed not to be primitive

Declaration

public static Class primitiveToWrapper(Class in) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*from w  ww  .  j  av  a2s  . c o  m*/
     * { field
     *
     * @name gWrapperClasses
     * @function list of java wrapper classes corresponding to primative types
     * }
     */
    protected static final Class gWrapperClasses[] = { Integer.class, Float.class, Boolean.class, Double.class,
            Byte.class, Character.class, Integer.class, Long.class };
    /**
     * { field
     *
     * @name gPrimitiveClasses
     * @function list of java wrapper classes corresponding to primative types
     * }
     */
    protected static final Class gPrimitiveClasses[] = { Integer.TYPE, Float.TYPE, Boolean.TYPE, Double.TYPE,
            Byte.TYPE, Character.TYPE, Integer.TYPE, Long.TYPE };
    protected static Map gPrimitiveClassToWrapper;
    protected static Map gWrapperToPrimitiveClass;

    /**
     * convert the input class to a wrapper class
     * returns in if the argument is not primitive
     *
     * @param in non-null class
     * @return non-null class guaranteed not to be primitive
     */
    public static Class primitiveToWrapper(Class in) {
        if (!in.isPrimitive())
            return (in);
        if (gPrimitiveClassToWrapper == null)
            buildWrapperMapping();
        return ((Class) gPrimitiveClassToWrapper.get(in));
    }

    /**
     * build mappings primitive to Wrapper
     */
    protected static synchronized void buildWrapperMapping() {
        if (gPrimitiveClassToWrapper != null)
            return; // already done
        Map TheMap = new HashMap();
        Map TheMap2 = new HashMap();
        for (int i = 0; i < gPrimitiveClasses.length; i++) {
            TheMap.put(gPrimitiveClasses[i], gWrapperClasses[i]);
            TheMap2.put(gWrapperClasses[i], gPrimitiveClasses[i]);
        }
        gWrapperToPrimitiveClass = TheMap2;
        gPrimitiveClassToWrapper = TheMap;
    }
}

Related

  1. getPrimitiveTypeByWrapper(Class clazz)
  2. getPrimitiveWrapper(Class c)
  3. getWrapper(String primitiveClassName)
  4. getWrapperTypeByPrimitive(Class clazz)
  5. primitiveToWrapper(Class cls)
  6. primitiveToWrapper(final Class p)
  7. toClass(String primitiveName)