Java Object Type Case castOrNull(Object o, Class clazz)

Here you can find the source of castOrNull(Object o, Class clazz)

Description

cast Or Null

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <C> C castOrNull(Object o, Class<C> clazz) 

Method Source Code

//package com.java2s;
/*/*from w ww.j a  v a 2  s .  c  o m*/
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    @SuppressWarnings("unchecked")
    public static <C> C castOrNull(Object o, Class<C> clazz) {
        if (clazz.isAssignableFrom(o.getClass())) {
            return (C) o;
        } else {
            return null;
        }
    }
}

Related

  1. castObject(Class clazz, Object object)
  2. castObject(Object fromObj, Class toClass)
  3. castObject(Object ob)
  4. castOrDefault(Class type, Object value, T defaultValue, boolean allowNull)
  5. castOrDefault(Object o, Class type, T def)
  6. castOrNull(Object o, Class cls)
  7. castOrThrowError(Class clazz, T1 object, String messageTemplate)
  8. castSafe(Class inClass, Object inValue, T inDefault)
  9. caststring1(String tmpq)