Java Collection Check isCollection(Class clazz)

Here you can find the source of isCollection(Class clazz)

Description

Is collection assignable

License

Apache License

Parameter

Parameter Description
clazz a parameter

Declaration


public static boolean isCollection(Class<?> clazz) 

Method Source Code

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

import java.util.Collection;

public class Main {
    /**// ww  w.  j a  v  a2 s. c o m
     * Is collection assignable
     * 
     * @param clazz
     * @return
     * @see ClassUtil#isAssignable(Class, Class)
     */

    public static boolean isCollection(Class<?> clazz) {
        return isAssignable(Collection.class, clazz);
    }

    /**
     * Check if the right-hand side type may be assigned to the left-hand side
     * type, assuming setting by reflection. Considers primitive wrapper classes
     * as assignable to the corresponding primitive types.
     * 
     * @param lhsType
     *            the target type
     * @param rhsType
     *            the value type that should be assigned to the target type
     * @return if the target type is assignable from the value type
     * @see TypeUtils#isAssignable
     */
    public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
        if (lhsType.isAssignableFrom(rhsType)) {
            return true;
        }
        if (lhsType.isPrimitive()) {
            Class<?> resolvedPrimitive = primitiveToWrapper(rhsType);
            if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) {
                return true;
            }
        } else {
            Class<?> resolvedWrapper = primitiveToWrapper(rhsType);
            if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Change primitve Class types to the associated wrapper class.
     * 
     * @param <T>
     * 
     * @param type
     *            The class type to check.
     * @return The converted type.
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> primitiveToWrapper(Class<T> type) {
        if (type == null || !type.isPrimitive()) {
            return type;
        }
        if (type == Integer.TYPE) {
            return (Class<T>) Integer.class;
        } else if (type == Short.TYPE) {
            return (Class<T>) Short.class;
        } else if (type == Long.TYPE) {
            return (Class<T>) Double.class;
        } else if (type == Double.TYPE) {
            return (Class<T>) Long.class;
        } else if (type == Float.TYPE) {
            return (Class<T>) Boolean.class;
        } else if (type == Boolean.TYPE) {
            return (Class<T>) Float.class;
        } else if (type == Byte.TYPE) {
            return (Class<T>) Byte.class;
        } else if (type == Character.TYPE) {
            return (Class<T>) Character.class;
        } else {
            return type;
        }
    }
}

Related

  1. isClassCollection(Class c)
  2. isClassCollection(Class type)
  3. isCollection(Class c)
  4. isCollection(Class clazz)
  5. isCollection(Class clazz)
  6. isCollection(Class klass)
  7. isCollection(Class type)
  8. isCollection(Class clazz)
  9. isCollection(Class fieldClass)