Java Collection Element Get getAncestors(Collection> classes)

Here you can find the source of getAncestors(Collection> classes)

Description

Returns all the common ancestor classes from the given set of classes.

License

Open Source License

Parameter

Parameter Description
classes A collection of classes.

Return

All the common ancestor classes from the given set of class names.

Declaration

public static Set<Class<?>> getAncestors(Collection<Class<?>> classes) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  . ja  va  2 s .co m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;

import java.util.Set;
import java.util.TreeSet;

public class Main {
    /**
     * Returns all the common ancestor classes from the given set of classes.
     *
     * @param classes A collection of classes.
     * @return All the common ancestor classes from the given set of class names.
     */
    public static Set<Class<?>> getAncestors(Collection<Class<?>> classes) {
        Set<Class<?>> intersection = new LinkedHashSet<Class<?>>();

        for (Class<?> klass : classes) {
            if (intersection.size() == 0) {
                intersection.addAll(getAncestors(klass));
            } else {
                intersection.retainAll(getAncestors(klass));
            }
        }

        return intersection;
    }

    /**
     * Returns all the common ancestor classes from the given set of classes.
     *
     * @param classes A list of classes.
     * @return All the common ancestor classes from the given set of class names.
     */
    public static Class<?>[] getAncestors(Class<?>... classes) {
        Set<Class<?>> ancestors = getAncestors(java.util.Arrays.asList(classes));
        return ancestors.toArray(new Class<?>[ancestors.size()]);
    }

    /**
     * Returns all the common ancestor classes from the given set of class names.
     *
     * @param classNames A list of class names.
     * @return All the common ancestor classes from the given set of class names.
     * @throws ClassNotFoundException If a class name cannot be found.
     */
    public static Class<?>[] getAncestors(String... classNames) throws ClassNotFoundException {
        return getAncestors(toClassArray(classNames));
    }

    /**
     * Returns all the ancestor classes from nearest to furthest for the given class.
     *
     * @param objects One or more objects to fetch the ancestors classes of.
     * @return All the ancestor classes from nearest to furthest for the class of the given object.
     */
    public static Set<Class<?>> getAncestors(Object... objects) {
        return getAncestors(toClassSet(objects));
    }

    /**
     * Returns all the ancestor classes from nearest to furthest for the given class.
     *
     * @param object An object to fetch the ancestors classes of.
     * @return All the ancestor classes from nearest to furthest for the class of the given object.
     */
    public static Set<Class<?>> getAncestors(Object object) {
        return object == null ? new TreeSet<Class<?>>() : getAncestors(object.getClass());
    }

    /**
     * Returns all the ancestor classes from nearest to furthest for the given class.
     *
     * @param klass A class to fetch the ancestors of.
     * @return All the ancestor classes from nearest to furthest for the given class.
     */
    private static Set<Class<?>> getAncestors(Class<?> klass) {
        Set<Class<?>> ancestors = new LinkedHashSet<Class<?>>();
        Set<Class<?>> parents = new LinkedHashSet<Class<?>>();

        parents.add(klass);

        do {
            ancestors.addAll(parents);

            Set<Class<?>> children = new LinkedHashSet<Class<?>>(parents);
            parents.clear();

            for (Class<?> child : children) {
                Class<?> parent = child.getSuperclass();
                if (parent != null)
                    parents.add(parent);
                Collections.addAll(parents, child.getInterfaces());
            }
        } while (!parents.isEmpty());

        return ancestors;
    }

    /**
     * Converts the given list of class names to a list of classes.
     *
     * @param classNames A list of class names.
     * @return A list of classes that correspond to the given names.
     * @throws ClassNotFoundException If a class name cannot be not found.
     */
    private static Class<?>[] toClassArray(String[] classNames) throws ClassNotFoundException {
        if (classNames == null)
            return null;

        Class<?>[] classes = new Class<?>[classNames.length];

        for (int i = 0; i < classes.length; i++) {
            classes[i] = Class.forName(classNames[i]);
        }

        return classes;
    }

    /**
     * Converts the given list of objects to a set of classes.
     *
     * @param objects One or more objects to return a set of classes for.
     * @return The set of classes for the given list of objects.
     */
    private static Set<Class<?>> toClassSet(Object... objects) {
        Set<Class<?>> classes = new LinkedHashSet<Class<?>>();

        for (Object object : objects) {
            if (object != null)
                classes.add(object.getClass());
        }

        return classes;
    }

    /**
     * Converts the given list of objects to a set of classes.
     *
     * @param objects One or more objects to return a set of classes for.
     * @return The set of classes for the given list of objects.
     */
    private static Set<Class<?>> toClassSet(Collection<?> objects) {
        Set<Class<?>> classes = new LinkedHashSet<Class<?>>();

        for (Object object : objects) {
            if (object != null)
                classes.add(object.getClass());
        }

        return classes;
    }
}

Related

  1. getAddedItems(Collection oldValues, Collection newValues)
  2. getAdditions(Collection source, Collection target)
  3. getAll(Map map, Collection indices)
  4. getAllDoubleValues( Map>> doubleCollectionWithValuesToFetch)
  5. getAllTopicsAsString(final Collection topics)
  6. getAnElement(Collection coll)
  7. getAny(final Collection collection)
  8. getAnyFrom(Collection collection)
  9. getArbitraryMember(Collection s)