Java Object Type Case castNullableParameterTo(String parameterName, T value, Class requiredType)

Here you can find the source of castNullableParameterTo(String parameterName, T value, Class requiredType)

Description

cast Nullable Parameter To

License

Mozilla Public License

Declaration

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

Method Source Code

//package com.java2s;
/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****/*ww w.  j  av a2s  . c o m*/
 *
 * 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 *****
 ****************************************************************************************/

public class Main {
    public static <T, R extends T> R castNullableParameterTo(String parameterName, T value, Class<R> requiredType) {
        final R result;
        if (value != null) {
            if (!requiredType.isInstance(value)) {
                throw new IllegalArgumentException(
                        parameterName + " is not of type " + requiredType.getName() + ".");
            } else {
                result = requiredType.cast(value);
            }
        } else {
            result = null;
        }
        return result;
    }
}

Related

  1. castingUtil(String value, Class classCasting)
  2. castInstance(Object o, Class clazz)
  3. castInttoLong(int x)
  4. castNode(Class jsonNode, Class classz)
  5. castNonNullArray( T [] arr)
  6. castNumber(Number num, Class clazz)
  7. castObject(Class clazz, Object object)
  8. castObject(Object fromObj, Class toClass)
  9. castObject(Object ob)