Java List Cast castNonNullListParameterTo(String parameterName, List value, Class requiredType)

Here you can find the source of castNonNullListParameterTo(String parameterName, List value, Class requiredType)

Description

cast Non Null List Parameter To

License

Mozilla Public License

Declaration

public static <T, R extends T> List<R> castNonNullListParameterTo(String parameterName, List<T> value,
            Class<R> requiredType) 

Method Source Code

//package com.java2s;
/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****//www. java2 s .c om
 *
 * Version: MPL 2.0
 *
 * echocat RedPrecursor, Copyright (c) 2011-2012 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

import java.util.List;

public class Main {
    public static <T, R extends T> List<R> castNonNullListParameterTo(String parameterName, List<T> value,
            Class<R> requiredType) {
        if (value == null) {
            throw new IllegalArgumentException(parameterName + " is null");
        }
        for (T entry : value) {
            if (entry == null) {
                throw new IllegalArgumentException("One entry of " + parameterName + " is null.");
            }
            if (!requiredType.isInstance(entry)) {
                throw new IllegalArgumentException(
                        "One entry of " + parameterName + " is not of type " + requiredType.getName() + ".");
            }
        }
        //noinspection unchecked
        return (List<R>) value;
    }
}

Related

  1. castList(final List original)
  2. castList(final Object object)
  3. castList(List list, Class clazz)
  4. castListElem(List list, V target)
  5. castListUnchecked(Object list)
  6. castOrCopyToList(Iterable iterable)
  7. castTo(final List list, final Class clasz)
  8. castToDocumentList(Object obj)
  9. castToList(U[] array, Class clazz)