Java Collection Flatten flatten(Class cls, Collection> collections)

Here you can find the source of flatten(Class cls, Collection> collections)

Description

flatten

License

Open Source License

Declaration

public static <T, C extends Collection<T>> Collection<T> flatten(Class<C> cls,
            Collection<? extends Collection<T>> collections) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    public static <T, C extends Collection<T>> Collection<T> flatten(Class<C> cls,
            Collection<? extends Collection<T>> collections) {
        try {/*w  ww.  ja v a  2 s  . co m*/
            Collection<T> flatCollection = cls.newInstance();
            for (Collection<T> collection : collections) {
                flatCollection.addAll(collection);
            }
            return flatCollection;
        } catch (RuntimeException re) {
            throw re;
        } catch (Throwable t) {
            throw new IllegalArgumentException(t);
        }
    }

    public static <T> T newInstance(String cls, Class<T> interfaceType) {
        Class<?> clazz;
        try {
            clazz = Class.forName(cls);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("class not found: " + cls, e);
        }
        Object obj;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalArgumentException("cannot instantiate class: " + cls, e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("cannot access no-args constructor of class: " + cls, e);
        }
        T t;
        try {
            t = interfaceType.cast(obj);
        } catch (ClassCastException e) {
            throw new IllegalArgumentException(
                    "cannot cast new instance of " + cls + " to interface type " + interfaceType.getName());
        }

        return t;
    }

    private static int cast(byte b) {
        if (b >= 0) {
            return b;
        }
        return 256 + b;
    }
}

Related

  1. flatten( Collection colOfCols)
  2. flatten(Collection> collections)
  3. flatten(Collection> cols)
  4. flatten(Collection l)
  5. flatten(Collection list, Class type)