Java Reflection Primitive calculateDistancePrimitive(Class a, Class b)

Here you can find the source of calculateDistancePrimitive(Class a, Class b)

Description

calculate Distance Primitive

License

LGPL

Declaration

static float calculateDistancePrimitive(Class<?> a, Class<?> b) 

Method Source Code

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

import java.util.Arrays;

import java.util.List;

public class Main {
    /**/*from  w  ww . j av a 2s . c  om*/
     * Array that contains the primitive wrappers in the order they can be
     * converted without a type-cast.
     */
    private static final List<Class<?>> PRIMITIVE_WIDENING = Arrays.asList(Byte.class, Short.class, Character.class,
            Integer.class, Long.class, Float.class, Double.class);

    static float calculateDistancePrimitive(Class<?> a, Class<?> b) {

        // Nothing can be converted to a char or a boolean.
        if (b == Character.class || b == Boolean.class)
            return -1F;
        // char acts like a short.
        if (a == Character.class)
            a = Short.class;

        int indexA = PRIMITIVE_WIDENING.indexOf(a);
        int indexB = PRIMITIVE_WIDENING.indexOf(b);

        if (indexA < 0 || indexB < 0)
            return -1F;
        return (indexB - indexA) / 10F;
    }
}

Related

  1. addPrimitiveToWrapperRelation(Class primitiveClass, Class wrapperClass)
  2. canCoerceLiteralNumberExpr(Class type)
  3. castToPrimitive(Object start, Class prim)
  4. convertPrimitiveToObjectClass(Class clazz)
  5. convertPrimitiveToWrapper(Class clazz)