Example #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;
 }
Example #2
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;
    }
  }
Example #3
0
  public static <TO, FROM> List<TO> mapFilterNulls(
      Function<FROM, TO> converter, Collection<FROM> fromCollection) {

    ArrayList<TO> toList = new ArrayList<>(fromCollection.size());

    for (FROM from : fromCollection) {
      TO converted = converter.apply(from);
      if (converted != null) {
        toList.add(converted);
      }
    }

    return toList;
  }