1 package com.github.davidmoten.guavamini;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5
6 public final class Iterators {
7
8 private Iterators() {
9 // prevent instantiation
10 }
11
12 /**
13 * Adds all elements in {@code iterator} to {@code collection}. The iterator
14 * will be left exhausted: its {@code hasNext()} method will return
15 * {@code false}.
16 *
17 * @param addTo
18 * collection to add to
19 * @param iterator
20 * iterator whose elements will be added to the collection
21 * @param <T>
22 * generic type of collection
23 * @return {@code true} if {@code collection} was modified as a result of
24 * this operation
25 */
26 public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
27 Preconditions.checkNotNull(addTo);
28 Preconditions.checkNotNull(iterator);
29 boolean wasModified = false;
30 while (iterator.hasNext()) {
31 wasModified |= addTo.add(iterator.next());
32 }
33 return wasModified;
34 }
35
36 }