Example #1
0
 private static <T> Object[] toArray(Iterable<T> elements) {
   if (elements instanceof java.util.List) {
     final java.util.List<T> list = (java.util.List<T>) elements;
     return list.toArray();
   } else {
     final java.util.Iterator<? extends T> it = elements.iterator();
     final java.util.List<T> list = new java.util.ArrayList<>();
     while (it.hasNext()) {
       list.add(it.next());
     }
     return list.toArray();
   }
 }
Example #2
0
 @Override
 public <U> Array<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
   Objects.requireNonNull(mapper, "mapper is null");
   if (isEmpty()) {
     return empty();
   } else {
     final java.util.List<U> list = new ArrayList<>();
     for (T t : this) {
       for (U u : mapper.apply(t)) {
         list.add(u);
       }
     }
     return wrap(toArray(list));
   }
 }
Example #3
0
 @Override
 public Array<T> filter(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   final java.util.List<T> list = new ArrayList<>();
   for (T t : this) {
     if (predicate.test(t)) {
       list.add(t);
     }
   }
   if (list.isEmpty()) {
     return empty();
   } else if (list.size() == size()) {
     return this;
   } else {
     return wrap(list.toArray());
   }
 }