Esempio n. 1
0
 public static Object toArrayGuessType(Collection<?> value) {
   Class<?> componentType = Reflection.getComponentType(value);
   Object array = Array.newInstance(componentType, value.size());
   @SuppressWarnings("unchecked")
   Iterator<Object> iterator = (Iterator<Object>) value.iterator();
   int index = 0;
   while (iterator.hasNext()) {
     BeanUtils.idx(array, index, iterator.next());
     index++;
   }
   return array;
 }
Esempio n. 2
0
 public static byte[] barray(Object value) {
   // You could handleUnexpectedException shorts, ints, longs and chars more efficiently
   if (value.getClass() == Typ.byteArray) {
     return (byte[]) value;
   }
   byte[] values = new byte[Boon.len(value)];
   int index = 0;
   Iterator<Object> iterator = iterator(Object.class, value);
   while (iterator.hasNext()) {
     values[index] = toByte(iterator.next());
     index++;
   }
   return values;
 }
Esempio n. 3
0
 public static char[] carray(Object value) {
   // You could handleUnexpectedException shorts, ints, longs and chars more efficiently
   if (value.getClass() == Typ.charArray) {
     return (char[]) value;
   }
   char[] values = new char[Boon.len(value)];
   int index = 0;
   Iterator<Object> iterator = iterator(Typ.object, value);
   while (iterator.hasNext()) {
     values[index] = toChar(iterator.next());
     index++;
   }
   return values;
 }
Esempio n. 4
0
 @SuppressWarnings({"rawtypes", "unchecked"})
 public static SortedSet toSortedSet(Object value) {
   if (value instanceof Set) {
     return (SortedSet) value;
   } else if (value instanceof Collection) {
     return new TreeSet((Collection) value);
   } else {
     TreeSet set = new TreeSet();
     Iterator<Object> iterator = iterator(Typ.object, value);
     while (iterator.hasNext()) {
       set.add(iterator.next());
     }
     return set;
   }
 }
Esempio n. 5
0
 public static Collection toCollection(Object value) {
   if (value instanceof Collection) {
     return (Collection) value;
   } else if (value == null) {
     return new ArrayList();
   } else if (value instanceof Map) {
     return ((Map) value).entrySet();
   } else {
     ArrayList list = new ArrayList(Boon.len(value));
     Iterator<Object> iterator = iterator(Typ.object, value);
     while (iterator.hasNext()) {
       list.add(iterator.next());
     }
     return list;
   }
 }
Esempio n. 6
0
 @SuppressWarnings({"rawtypes", "unchecked"})
 public static List toList(Object value) {
   if (value instanceof List) {
     return (List) value;
   } else if (value instanceof Collection) {
     return new ArrayList((Collection) value);
   } else if (value == null) {
     return new ArrayList();
   } else if (value instanceof Map) {
     return new ArrayList(((Map) value).entrySet());
   } else {
     ArrayList list = new ArrayList(Boon.len(value));
     Iterator<Object> iterator = iterator(Typ.object, value);
     while (iterator.hasNext()) {
       list.add(iterator.next());
     }
     return list;
   }
 }
Esempio n. 7
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;
    }
  }