Java Collection Check isCollection(Class c)

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

Description

Returns true if the specified class is a Collection, otherwise it returns false

License

Open Source License

Parameter

Parameter Description
c The class to examine

Exception

Parameter Description

Return

True if the specified class is a Collection, otherwise false

Declaration

public static boolean isCollection(Class<?> c) throws Exception 

Method Source Code

//package com.java2s;
/*//from  w  w w. ja v  a  2  s .  c  o  m
Copyright (c) 2009 Mindaugas Greibus (spantus@gmail.com)
Part of program for analyze speech signal 
http://spantus.sourceforge.net
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

public class Main {
    /**
     * Returns true if the specified class is a Collection, otherwise it returns
     * false
     * 
     * @param c
     *            The class to examine
     * @return True if the specified class is a Collection, otherwise false
     * @throws com.javasrc.rtools.rxml.RXMLException
     */
    public static boolean isCollection(Class<?> c) throws Exception {
        return containsInterface(c, "java.util.Collection") || isSet(c) || isList(c);
    }

    /**
     * Examines a class and searches through all interfaces that it implements.
     * If it implements the specified "interfaceName" then this method returns
     * true, otherwise it returns false.
     * 
     * @param c
     *            The class to examine
     * @param interfaceName
     *            The name of the interface to look for
     * @return True if the class implements the specified interface, otherwise
     *         false
     * @throws RXMLException
     */
    public static boolean containsInterface(Class<?> c, String interfaceName) throws Exception {
        try {
            // If this class is the interface then it will not implement itself
            if (c.getName().equals(interfaceName)) {
                return true;
            }

            // Extract all of the interfaces from the specified class
            Class<?>[] interfaces = c.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                // Compare the interface against the specified criteria
                String iName = interfaces[i].getName();
                if (iName.equals(interfaceName)) {
                    // Found it
                    return true;
                }
            }

            // Completed iterating over all interfaces and this class does not
            // implement the
            // specified interface
            return false;
        } catch (Exception e) {
            // Wrap the exception in an RXMLException and throw it to the caller
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    public static boolean isSet(Class<?> c) throws Exception {
        return containsInterface(c, "java.util.Set");
    }

    public static boolean isList(Class<?> c) throws Exception {
        return containsInterface(c, "java.util.List");
    }
}

Related

  1. isBlankAll(Collection... collections)
  2. isC2InC1(Collection c1, Collection c2)
  3. isClassCollection(Class c)
  4. isClassCollection(Class c)
  5. isClassCollection(Class type)
  6. isCollection(Class clazz)
  7. isCollection(Class clazz)
  8. isCollection(Class clazz)
  9. isCollection(Class klass)