Exemplo n.º 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;
    }
  }
Exemplo n.º 2
0
  public static <T extends Enum> T toEnum(Class<T> cls, String value, Enum defaultEnum) {

    T[] enumConstants = cls.getEnumConstants();
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    value = value.toUpperCase().replace('-', '_');
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    value = Str.underBarCase(value);
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    return (T) defaultEnum;
  }
Exemplo n.º 3
0
  public static Class<?> toClass(String str) {

    try {
      return Class.forName(str);
    } catch (ClassNotFoundException ex) {
      return (Class<?>) handle(Object.class, ex);
    }
  }
Exemplo n.º 4
0
  public static <T extends Enum> T toEnum(Class<T> cls, int value) {

    T[] enumConstants = cls.getEnumConstants();
    for (T e : enumConstants) {
      if (e.ordinal() == value) {
        return e;
      }
    }
    return null;
  }
Exemplo n.º 5
0
  public static <T extends Enum> T toEnumOrDie(Class<T> cls, int value) {

    T[] enumConstants = cls.getEnumConstants();
    for (T e : enumConstants) {
      if (e.ordinal() == value) {
        return e;
      }
    }
    die("Can't convert ordinal value " + value + " into enum of type " + cls);
    return null;
  }
Exemplo n.º 6
0
  @SuppressWarnings("unchecked")
  public static <T> T toPrimitiveArrayIfPossible(Class<T> clz, Object value) {
    if (clz == Typ.intArray) {
      return (T) iarray(value);
    } else if (clz == Typ.byteArray) {
      return (T) barray(value);
    } else if (clz == Typ.charArray) {
      return (T) carray(value);
    } else if (clz == Typ.shortArray) {
      return (T) sarray(value);
    } else if (clz == Typ.longArray) {
      return (T) larray(value);
    } else if (clz == Typ.floatArray) {
      return (T) farray(value);
    } else if (clz == Typ.doubleArray) {
      return (T) darray(value);
    } else if (value.getClass() == clz) {
      return (T) value;
    } else {
      int index = 0;
      Object newInstance = Array.newInstance(clz.getComponentType(), Boon.len(value));
      Iterator<Object> iterator = iterator(Typ.object, value);
      while (iterator.hasNext()) {

        Object item = iterator.next();

        if (clz.getComponentType().isAssignableFrom(item.getClass())) {

          BeanUtils.idx(newInstance, index, item);

        } else {

          item = coerce(clz.getComponentType(), item);

          BeanUtils.idx(newInstance, index, item);
        }
        index++;
      }
      return (T) newInstance;
    }
  }
Exemplo n.º 7
0
  public static <T> T createFromArg(Class<T> clz, Object value) {
    if (value == null) {
      return null;
    }
    ClassMeta meta = ClassMeta.classMeta(clz);
    List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

    if (constructors.size() == 0) {
      return null;
    } else if (constructors.size() == 1) {
      ConstructorAccess constructorAccess = constructors.get(0);
      Class<?> arg1Type = constructorAccess.parameterTypes()[0];
      if (arg1Type.isInstance(value)) {
        return (T) constructorAccess.create(value);
      } else {
        return (T) constructorAccess.create(coerce(arg1Type, value));
      }
    } else {
      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isInstance(value)) {
          return (T) c.create(value);
        }
      }

      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isAssignableFrom(value.getClass())) {
          return (T) c.create(value);
        }
      }
    }
    return null;
  }
Exemplo n.º 8
0
  public static <T> T[] toArray(Class<T> componentType, Collection<T> collection) {
    T[] array = (T[]) Array.newInstance(componentType, collection.size());

    if (componentType.isAssignableFrom(getComponentType(collection))) {
      return collection.toArray(array);
    } else {

      int index = 0;
      for (Object o : collection) {
        array[index] = Conversions.coerce(componentType, o);
        index++;
      }
      return array;
    }
  }
Exemplo n.º 9
0
  public static <T> T coerceWithFlag(
      TypeType coerceTo, Class<T> clz, boolean[] flag, Object value) {

    flag[0] = true;
    if (value == null) {
      return null;
    }

    if (clz.isInstance(value)) {
      return (T) value;
    }

    switch (coerceTo) {
      case STRING:
      case CHAR_SEQUENCE:
        return (T) value.toString();

      case INT:
      case INTEGER_WRAPPER:
        Integer i = toInt(value);
        if (i == Integer.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) i;

      case SHORT:
      case SHORT_WRAPPER:
        Short s = toShort(value);
        if (s == Short.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) s;

      case BYTE:
      case BYTE_WRAPPER:
        Byte by = toByte(value);
        if (by == Byte.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) by;

      case CHAR:
      case CHAR_WRAPPER:
        Character ch = toChar(value);
        if (ch == (char) 0) {
          flag[0] = false;
        }
        return (T) ch;

      case LONG:
      case LONG_WRAPPER:
        Long l = toLong(value);
        if (l == Long.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) l;

      case DOUBLE:
      case DOUBLE_WRAPPER:
        Double d = toDouble(value);
        if (d == Double.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) d;

      case FLOAT:
      case FLOAT_WRAPPER:
        Float f = toFloat(value);
        if (f == Float.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) f;

      case DATE:
        return (T) toDate(value);

      case BIG_DECIMAL:
        return (T) toBigDecimal(value);

      case BIG_INT:
        return (T) toBigInteger(value);

      case CALENDAR:
        return (T) toCalendar(toDate(value));

      case BOOLEAN:
      case BOOLEAN_WRAPPER:
        return (T) (Boolean) toBooleanOrDie(value);

      case MAP:
        return (T) toMap(value);

      case ARRAY:
      case ARRAY_INT:
      case ARRAY_BYTE:
      case ARRAY_SHORT:
      case ARRAY_FLOAT:
      case ARRAY_DOUBLE:
      case ARRAY_LONG:
      case ARRAY_STRING:
      case ARRAY_OBJECT:
        return toPrimitiveArrayIfPossible(clz, value);

      case COLLECTION:
        return toCollection(clz, value);

      case INSTANCE:
        if (value instanceof Map) {
          return MapObjectConversion.fromMap((Map<String, Object>) value, clz);
        } else if (value instanceof List) {
          return MapObjectConversion.fromList((List<Object>) value, clz);
        } else if (clz.isInstance(value)) {
          return (T) value;
        } else {
          ClassMeta meta = ClassMeta.classMeta(clz);
          List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

          if (constructors.size() == 0) {
            return null;
          } else if (constructors.size() == 1) {
            ConstructorAccess constructorAccess = constructors.get(0);
            Class<?> arg1Type = constructorAccess.parameterTypes()[0];
            if (arg1Type.isInstance(value)) {
              return (T) constructorAccess.create(value);
            } else {
              return (T) constructorAccess.create(coerce(arg1Type, value));
            }
          } else {
            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isInstance(value)) {
                return (T) c.create(value);
              }
            }

            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isAssignableFrom(value.getClass())) {
                return (T) c.create(value);
              }
            }

            flag[0] = false;
            break;
          }
        }

      case ENUM:
        return (T) toEnum((Class<? extends Enum>) clz, value);

      case CLASS:
        return (T) toClass(value);

      case TIME_ZONE:
        return (T) toTimeZone(flag, value);

      case UUID:
        return (T) toUUID(flag, value);

      case CURRENCY:
        return (T) toCurrency(value);

      case OBJECT:
        return (T) value;

      default:
        flag[0] = false;
        break;
    }
    return null;
  }
Exemplo n.º 10
0
  public static <T> T coerce(TypeType coerceTo, Class<T> clz, Object value) {
    if (value == null) {
      if (coerceTo != TypeType.INSTANCE && !clz.isPrimitive()) {

        return null;
      } else if (clz.isPrimitive()) {
        if (clz == boolean.class) {
          return (T) (Boolean) false;
        }
        return (T) (Number) 0;
      }
    }

    switch (coerceTo) {
      case STRING:
      case CHAR_SEQUENCE:
        return (T) value.toString();

      case NUMBER:
        if (value instanceof Number) {
          return (T) value;
        } else {
          Double d = toDouble(value);
          return (T) d;
        }

      case INT:
      case INTEGER_WRAPPER:
        Integer i = toInt(value);
        return (T) i;

      case SHORT:
      case SHORT_WRAPPER:
        Short s = toShort(value);
        return (T) s;

      case BYTE:
      case BYTE_WRAPPER:
        Byte by = toByte(value);
        return (T) by;

      case CHAR:
      case CHAR_WRAPPER:
        Character ch = toChar(value);
        return (T) ch;

      case LONG:
      case LONG_WRAPPER:
        Long l = toLong(value);
        return (T) l;

      case DOUBLE:
      case DOUBLE_WRAPPER:
        Double d = toDouble(value);
        return (T) d;

      case FLOAT:
      case FLOAT_WRAPPER:
        Float f = toFloat(value);
        return (T) f;

      case DATE:
        return (T) toDate(value);

      case BIG_DECIMAL:
        return (T) toBigDecimal(value);

      case BIG_INT:
        return (T) toBigInteger(value);

      case CALENDAR:
        return (T) toCalendar(toDate(value));

      case BOOLEAN:
      case BOOLEAN_WRAPPER:
        return (T) (Boolean) toBoolean(value);

      case MAP:
        return (T) toMap(value);

      case ARRAY:
      case ARRAY_INT:
      case ARRAY_BYTE:
      case ARRAY_SHORT:
      case ARRAY_FLOAT:
      case ARRAY_DOUBLE:
      case ARRAY_LONG:
      case ARRAY_STRING:
      case ARRAY_OBJECT:
        return toPrimitiveArrayIfPossible(clz, value);

      case LIST:
      case SET:
      case COLLECTION:
        return toCollection(clz, value);

      case INSTANCE:
        if (value instanceof Value) {
          value = ((Value) value).toValue();
        }
        if (value instanceof Map) {
          return MapObjectConversion.fromMap((Map<String, Object>) value, clz);
        } else if (value instanceof List) {
          return MapObjectConversion.fromList((List<Object>) value, clz);
        } else if (clz.isInstance(value)) {
          return (T) value;
        } else {
          return createFromArg(clz, value);
        }

      case ENUM:
        return (T) toEnum((Class<? extends Enum>) clz, value);

      case PATH:
        return (T) IO.path(value.toString());

      case CLASS:
        return (T) toClass(value);

      case TIME_ZONE:
        return (T) toTimeZone(value);

      case UUID:
        return (T) toUUID(value);

      case CURRENCY:
        return (T) toCurrency(value);

      case OBJECT:
        return (T) value;

      case HANDLER:
        return (T) value;

      default:
        return createFromArg(clz, value);
    }
  }