Java Class Type Check isNumber(Class type)

Here you can find the source of isNumber(Class type)

Description

is Number

License

Open Source License

Parameter

Parameter Description
type a parameter

Return

whether the input type is a number class or a number primitive.

Declaration

public static boolean isNumber(Class<?> type) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    private static Map<String, Class<?>> primitives = initializePrimitives();
    private static Map<Class<?>, Class<?>> primToNonPrim = initializePrimToNonPrim();

    /**/*from  www .  j av a2  s .  com*/
     * @param type
     * @return whether the input type is a number class or a number primitive.
     */
    public static boolean isNumber(Class<?> type) {
        if (type == null) {
            return false;
        }
        Class<?> forPrim = classForPrimitive(type);
        if (forPrim != null) {
            type = forPrim;
        }
        return (Number.class.isAssignableFrom(type));
    }

    public static Class<?> classForPrimitive(Class<?> primClass) {
        return getPrimToNonPrim().get(primClass);
    }

    public static Class<?> classForPrimitive(String primClass) {
        return getPrimitives().get(primClass);
    }

    public static Map<Class<?>, Class<?>> getPrimToNonPrim() {
        if (primToNonPrim == null) {
            initializePrimToNonPrim();
        }
        return primToNonPrim;
    }

    public static Map<String, Class<?>> getPrimitives() {
        if (primitives == null) {
            initializePrimitives();
        }
        return primitives;
    }

    private static Map<Class<?>, Class<?>> initializePrimToNonPrim() {
        primToNonPrim = new HashMap<Class<?>, Class<?>>();
        primToNonPrim.put(boolean.class, Boolean.class);
        primToNonPrim.put(byte.class, Byte.class);
        primToNonPrim.put(char.class, Character.class);
        primToNonPrim.put(short.class, Short.class);
        primToNonPrim.put(int.class, Integer.class);
        primToNonPrim.put(long.class, Long.class);
        primToNonPrim.put(float.class, Float.class);
        primToNonPrim.put(double.class, Double.class);
        primToNonPrim.put(void.class, Void.class);
        return primToNonPrim;
    }

    private static Map<String, Class<?>> initializePrimitives() {
        primitives = new TreeMap<String, Class<?>>();
        for (Class<?> c : getPrimToNonPrim().keySet()) {
            primitives.put(c.getSimpleName(), c);
        }
        // primitives.put( "boolean", boolean.class );
        // primitives.put( "byte", byte.class );
        // primitives.put( "char", char.class );
        // primitives.put( "short", short.class );
        // primitives.put( "int", int.class );
        // primitives.put( "long", long.class );
        // primitives.put( "float", float.class );
        // primitives.put( "double", double.class );
        // primitives.put( "void", void.class );
        return primitives;
    }
}

Related

  1. isJavaImmutable(Class cls)
  2. isLogicalPrimitive(Class c)
  3. isMap(Class o)
  4. isMapType(final Class type)
  5. isMethodReturnTypeValid(Class cls)
  6. isNumberType(Class type)
  7. isNumeralType(final Class c)
  8. isPrimitive(Class c)
  9. isPrimitiveType(Class clazz)