Java List from Array asList(Object o, Class cls)

Here you can find the source of asList(Object o, Class cls)

Description

as List

License

Open Source License

Declaration

public static <V, T> List<V> asList(Object o, Class<V> cls) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.List;

public class Main {
    /**// ww w  .  j  av  a2s. c o  m
     * @param c
     * @return c if c is a {@link List} or, otherwise, a new List containing
     *         the elements of c
     */
    public static <V, T extends V> List<V> asList(Collection<T> c) {
        if (c instanceof List)
            return (List<V>) c;
        List<V> list = new ArrayList<V>(c);
        return list;
    }

    public static <V, T> List<V> asList(Object o, Class<V> cls) {
        if (o == null)
            return null;
        if (o instanceof Collection) {
            return asList((Collection<?>) o, cls);
        }
        return asList(newList(o), cls);
    }

    /**
     * @param c
     * @param cls
     * @return a new {@link List} containing
     *         the elements of c cast to type V
     */
    public static <V, T> ArrayList<V> asList(Collection<T> c, Class<V> cls) {
        ArrayList<V> list = new ArrayList<V>();
        for (T t : c) {
            if (t == null || cls == null || cls.isAssignableFrom(t.getClass())) {
                try {
                    V v = cls.cast(t);
                    list.add(v);
                } catch (ClassCastException e) {
                }
            }
        }
        return list;
    }

    /**
     * Creates a new {@link ArrayList} and inserts the arguments, {@code ts}.
     * @param ts
     * @return the new {@link ArrayList}
     */
    public static <T> ArrayList<T> newList(T... ts) {
        ArrayList<T> newList = new ArrayList<T>();
        if (ts != null && ts.length > 0) {
            newList.addAll(Arrays.asList(ts));
        }
        return newList;
    }

    public static <T, C extends Collection<T>> C add(C coll, T... items) {
        return plus(coll, items);
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C add(C coll, D items) {
        return plus(coll, items);
    }

    /**
     * A potentially more efficient addAll() for unordered Collections.
     * @param coll1
     * @param coll2
     * @return the longer of the two collections after adding the shorter to the longer.  
     */
    public static <T, C extends Collection<T>> C addAll(Collection<T> coll1, Collection<T> coll2) {
        if (coll1 == null)
            return (C) coll2;
        if (coll2 == null)
            return (C) coll1;

        Collection<T> cSmaller, cBigger;
        if (coll1.size() < coll2.size()) {
            cSmaller = coll1;
            cBigger = coll2;
        } else {
            cSmaller = coll2;
            cBigger = coll1;
        }
        try {
            cBigger.addAll(cSmaller);
            return (C) cBigger;
        } catch (UnsupportedOperationException e) {
        }
        try {
            cSmaller.addAll(cBigger);
            return (C) cSmaller;
        } catch (UnsupportedOperationException e) {
        }
        ArrayList<T> newList = new ArrayList<T>(cBigger);
        newList.addAll(cSmaller);
        return (C) newList;
    }

    public static <T, C extends Collection<T>> C plus(C coll, T... items) {
        coll.addAll(newList(items));
        return coll;
    }

    public static <T, C extends Collection<T>, D extends Collection<T>> C plus(C coll, D items) {
        coll.addAll(items);
        return coll;
    }
}

Related

  1. asList(Iterable iterable)
  2. asList(Iterator iterator)
  3. asList(List list)
  4. asList(long... array)
  5. asList(Object info)
  6. asList(Object theone)
  7. asList(Object value)
  8. asList(Object value)
  9. asList(Object[] array)