Java List Cast castList(Class elementType, List list)

Here you can find the source of castList(Class elementType, List list)

Description

cast List

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static <T> List<T> castList(Class<T> elementType, List<?> list) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {
    /**/* w w  w  .  ja  v  a  2 s .c  o m*/
     * @since 1.0
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> castList(Class<T> elementType, List<?> list) throws Exception {
        if (list == null) {
            return null;
        }
        for (Object element : list) {
            if (!elementType.isAssignableFrom(element.getClass())) {
                throw new Exception("List element has invalid type -- expected '" + elementType.getName()
                        + "', got: " + element.getClass().getName());
            }
        }
        return (List<T>) list;
    }
}

Related

  1. cast(List from)
  2. CAST_LIST(Class className, Object obj)
  3. castList(Class clazz, Collection c)
  4. castList(Class clazz, Collection c)
  5. castList(Class clazz, Collection collection)
  6. castList(Class klass, List list)
  7. castList(Collection c)
  8. castList(final List original)
  9. castList(final Object object)