Пример #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;
    }
  }
Пример #2
0
 @SuppressWarnings("unchecked")
 public static <T> T toCollection(Class<T> clz, Object value) {
   if (Typ.isList(clz)) {
     return (T) toList(value);
   } else if (Typ.isSortedSet(clz)) {
     return (T) toSortedSet(value);
   } else if (Typ.isSet(clz)) {
     return (T) toSet(value);
   } else {
     return (T) toList(value);
   }
 }
Пример #3
0
  public static <T> Iterator<T> iterator(Class<T> class1, final Object value) {

    if (value == null) {
      return Collections.EMPTY_LIST.iterator();
    }

    if (Boon.isArray(value)) {
      final int length = Arry.len(value);

      return new Iterator<T>() {
        int i = 0;

        @Override
        public boolean hasNext() {
          return i < length;
        }

        @Override
        public T next() {
          if (i >= length) throw new NoSuchElementException("No more properties");
          T next = (T) BeanUtils.idx(value, i);
          i++;
          return next;
        }

        @Override
        public void remove() {}
      };
    } else if (Typ.isCollection(value.getClass())) {
      return ((Collection<T>) value).iterator();
    } else {
      return (Iterator<T>) Collections.singleton(value).iterator();
    }
  }
Пример #4
0
  public static Collection<Object> createCollection(Class<?> type, int size) {

    if (type == List.class) {
      return new ArrayList<>(size);
    } else if (type == SortedSet.class) {
      return new TreeSet<>();
    } else if (type == Set.class) {
      return new LinkedHashSet<>(size);
    } else if (Typ.isList(type)) {
      return new ArrayList<>();
    } else if (Typ.isSortedSet(type)) {
      return new TreeSet<>();
    } else if (Typ.isSet(type)) {
      return new LinkedHashSet<>(size);
    } else {
      return new ArrayList(size);
    }
  }