Java Reflection Primitive getPrimitivePromotionCost(final Class srcClass, final Class destClass)

Here you can find the source of getPrimitivePromotionCost(final Class srcClass, final Class destClass)

Description

get Primitive Promotion Cost

License

Apache License

Declaration

private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) 

Method Source Code

//package com.java2s;
/*/*from www  .jav a2 s.  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<Class<?>, Class<?>>();
    private static final Class<?>[] ORDERED_PRIMITIVE_TYPES = { Byte.TYPE, Short.TYPE, Character.TYPE, Integer.TYPE,
            Long.TYPE, Float.TYPE, Double.TYPE };

    private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) {
        float cost = 0.0f;
        Class<?> cls = srcClass;
        if (!cls.isPrimitive()) {
            // slight unwrapping penalty
            cost += 0.1f;
            cls = wrapperToPrimitive(cls);
        }
        for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) {
            if (cls == ORDERED_PRIMITIVE_TYPES[i]) {
                cost += 0.1f;
                if (i < ORDERED_PRIMITIVE_TYPES.length - 1) {
                    cls = ORDERED_PRIMITIVE_TYPES[i + 1];
                }
            }
        }
        return cost;
    }

    public static Class<?> wrapperToPrimitive(final Class<?> cls) {
        return wrapperPrimitiveMap.get(cls);
    }
}

Related

  1. getPrimitiveClass(String name)
  2. getPrimitiveClass(String primitiveClassName)
  3. getPrimitiveClass(String primitiveName)
  4. getPrimitiveClassIfWrapper(Class clazz)
  5. getPrimitiveDefault(Class primitiveClazz)
  6. getPrimitiveSize(Class type)
  7. getPrimitiveType(Class clazz)
  8. getPrimitiveType(String javaClassName)
  9. getPrimitiveWrapper(Class primitiveType)