Java Object Create as(Class targetClass, Object entity)

Here you can find the source of as(Class targetClass, Object entity)

Description

Casts entity to targetClass or return null, if it is impossible

License

Open Source License

Parameter

Parameter Description
targetClass a parameter
entity a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T> T as(Class<T> targetClass, Object entity) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w  ww . j av a2 s.  c om
     * Casts {@link entity} to {@link targetClass} or return null, if it is impossible
     * 
     * @param targetClass
     * @param entity
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T as(Class<T> targetClass, Object entity) {
        T result = null;
        if ((entity != null) && targetClass.isAssignableFrom(entity.getClass())) {
            result = (T) entity;
        }
        return result;
    }
}

Related

  1. as(Class clazz, I instance)
  2. as(Class cls, Object o)
  3. as(Class t, Object o)
  4. as(Class type, Object value)
  5. as(Class type, U o)
  6. as(Object cst)
  7. as(Object instance, Class clazz)