Example #1
0
  @SuppressWarnings("unchecked")
  public static <T> T coerceClassic(Class<T> clz, Object value) {

    if (clz == null || value == null) {
      return null;
    }

    if (clz == value.getClass()) {
      return (T) value;
    }

    if (clz == Typ.string || clz == Typ.chars) {
      return (T) value.toString();
    } else if (clz == Typ.integer || clz == Typ.intgr) {
      Integer i = toInt(value);
      return (T) i;
    } else if (clz == Typ.longWrapper || clz == Typ.lng) {
      Long l = toLong(value);
      return (T) l;
    } else if (clz == Typ.doubleWrapper || clz == Typ.dbl) {
      Double i = toDouble(value);
      return (T) i;
    } else if (clz == Typ.date) {
      return (T) toDate(value);
    } else if (clz == Typ.bigInteger) {
      return (T) toBigInteger(value);
    } else if (clz == Typ.bigDecimal) {
      return (T) toBigDecimal(value);
    } else if (clz == Typ.calendar) {
      return (T) toCalendar(toDate(value));
    } else if (clz == Typ.floatWrapper || clz == Typ.flt) {
      Float i = toFloat(value);
      return (T) i;
    } else if (clz == Typ.stringArray) {
      die("Need to fix this");
      return null;
    } else if (clz == Typ.bool || clz == Typ.bln) {
      Boolean b = toBoolean(value);
      return (T) b;
    } else if (Typ.isMap(clz)) {
      return (T) toMap(value);
    } else if (clz.isArray()) {
      return toPrimitiveArrayIfPossible(clz, value);
    } else if (Typ.isCollection(clz)) {
      return toCollection(clz, value);
    } else if (clz.getPackage() != null
        && !clz.getPackage().getName().startsWith("java")
        && Typ.isMap(value.getClass())
        && Typ.doesMapHaveKeyTypeString(value)) {
      return (T) MapObjectConversion.fromMap((Map<String, Object>) value);
    } else if (clz.isEnum()) {
      return (T) toEnum((Class<? extends Enum>) clz, value);

    } else {
      return null;
    }
  }