Java Type Coerce coerce(Object value, Class clazz)

Here you can find the source of coerce(Object value, Class clazz)

Description

coerce

License

Open Source License

Declaration

public static <T> T coerce(Object value, Class<T> clazz) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

public class Main {
    public static <T> T coerce(Object value, Class<T> clazz) {
        return coerce(value, clazz, null);
    }//  ww  w  .j av a 2  s.c  o m

    public static <T> T coerce(Object value, Class<T> clazz, T def) {
        if (value == null) {
            return def;
        } else if (clazz == Integer.class) {
            return (T) Integer.valueOf(((Number) value).intValue());
        } else if (clazz == Double.class) {
            return (T) Double.valueOf(((Number) value).doubleValue());
        } else if (clazz.isInstance(value)) {
            return clazz.cast(value);
        }
        throw new ClassCastException("Cannot cast " + value + " to " + clazz);
    }
}

Related

  1. coerce(final Class klass)
  2. coerce(Object obj, Class c)
  3. coerce(Object val, Class clz)
  4. coerce(Object val, Class c)
  5. coerce(Object value, Class clazz)
  6. coerceBool(Object obj)
  7. coerceBool(Object val)
  8. coerceIntoComboId(Long entityId)