Java Class Type Check isUniformCollection(Collection c, Class e)

Here you can find the source of isUniformCollection(Collection c, Class e)

Description

Check if the given collection is a uniform collection of the given type.

License

Open Source License

Declaration

public static boolean isUniformCollection(Collection<?> c, Class<?> e) 

Method Source Code

//package com.java2s;
/*//www.j  a  v a  2 s .c om
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.*;

public class Main {
    private static final Map<String, Class<?>> primitiveMap = new HashMap<String, Class<?>>();

    /**
     * Check if the given collection is a uniform collection of the given type.
     */
    public static boolean isUniformCollection(Collection<?> c, Class<?> e) {
        if (e == null) {
            throw new IllegalArgumentException("Null reference type");
        }
        if (c == null) {
            throw new IllegalArgumentException("Null collection");
        }
        if (c.isEmpty()) {
            return false;
        }
        for (Object o : c) {
            if (o == null || !e.isAssignableFrom(o.getClass())) {
                return false;
            }
        }
        return true;
    }

    /**
     * This method returns the class matching the name className.
     * It's used to cater for the primitive types.
     */
    public static Class<?> getClass(String className) throws ClassNotFoundException {
        Class<?> c;
        if ((c = primitiveMap.get(className)) != null)
            return c;
        return Class.forName(className);
    }
}

Related

  1. isSimpleClass(Class clazz)
  2. isSimpleType(Class clazz)
  3. isSupportedParameter(Class type)
  4. isTerminal(Class clazz)
  5. isTypeConvertible(Class srcType, Class destType)
  6. isWrapperOfPrimitiveType(Class primitiveType, Class otherType)
  7. isWrapperTypeOf(Class propertyType, Object propertyValue)