1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkArgument;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkState;
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional;
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions;
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays;
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections;
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator;
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Enumeration;
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException;
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.PriorityQueue;
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue;
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class contains static utility methods that operate on or return objects
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of type {@link Iterator}. Except as noted, each method has a corresponding
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterable}-based method in the {@link Iterables} class.
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterators
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that they only advance
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the backing iteration when absolutely necessary.
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Iterators {
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Iterators() {}
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static final UnmodifiableIterator<Object> EMPTY_ITERATOR
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      = new UnmodifiableIterator<Object>() {
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        public boolean hasNext() {
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return false;
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        public Object next() {
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      };
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty iterator.
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ImmutableSet#of()}.
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> emptyIterator() {
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (UnmodifiableIterator<T>) EMPTY_ITERATOR;
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final Iterator<Object> EMPTY_MODIFIABLE_ITERATOR =
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      new Iterator<Object>() {
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public boolean hasNext() {
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return false;
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public Object next() {
92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override public void remove() {
96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new IllegalStateException();
97090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      };
99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty {@code Iterator} that throws
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link IllegalStateException} instead of
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link UnsupportedOperationException} on a call to
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link Iterator#remove()}.
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> Iterator<T> emptyModifiableIterator() {
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Iterator<T>) EMPTY_MODIFIABLE_ITERATOR;
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Returns an unmodifiable view of {@code iterator}. */
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> unmodifiableIterator(
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Iterator<T> iterator) {
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (iterator instanceof UnmodifiableIterator) {
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return (UnmodifiableIterator<T>) iterator;
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated public static <T> UnmodifiableIterator<T> unmodifiableIterator(
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      UnmodifiableIterator<T> iterator) {
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements remaining in {@code iterator}. The iterator
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int size(Iterator<?> iterator) {
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int count = 0;
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.next();
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      count++;
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return count;
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if {@code iterator} contains {@code element}.
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static boolean contains(Iterator<?> iterator, @Nullable Object element)
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  {
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (element == null) {
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (iterator.next() == null) {
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return true;
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    } else {
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (element.equals(iterator.next())) {
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return true;
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that belongs to the
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRemove the elements to remove
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static boolean removeAll(
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<?> removeFrom, Collection<?> elementsToRemove) {
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(elementsToRemove);
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (elementsToRemove.contains(removeFrom.next())) {
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Removes every element that satisfies the provided predicate from the
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterator. The iterator will be left exhausted: its {@code hasNext()}
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method will return {@code false}.
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param predicate a predicate that determines whether an element should
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     be removed
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if any elements were removed from the iterator
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
210bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public static <T> boolean removeIf(
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> removeFrom, Predicate<? super T> predicate) {
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (predicate.apply(removeFrom.next())) {
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that does not belong to the
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRetain the elements to retain
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static boolean retainAll(
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<?> removeFrom, Collection<?> elementsToRetain) {
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(elementsToRetain);
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!elementsToRetain.contains(removeFrom.next())) {
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Determines whether two iterators contain equal elements in the same order.
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * More specifically, this method returns {@code true} if {@code iterator1}
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * and {@code iterator2} contain the same number of elements and every element
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * of {@code iterator1} is equal to the corresponding element of
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code iterator2}.
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note that this will modify the supplied iterators, since they will have
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * been advanced some number of elements forward.
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static boolean elementsEqual(
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<?> iterator1, Iterator<?> iterator2) {
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator1.hasNext()) {
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator2.hasNext()) {
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o1 = iterator1.next();
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o2 = iterator2.next();
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!Objects.equal(o1, o2)) {
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return !iterator2.hasNext();
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a string representation of {@code iterator}, with the format
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [e1, e2, ..., en]}. The iterator will be left exhausted: its
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static String toString(Iterator<?> iterator) {
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (!iterator.hasNext()) {
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return "[]";
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    StringBuilder builder = new StringBuilder();
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    builder.append('[').append(iterator.next());
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      builder.append(", ").append(iterator.next());
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.append(']').toString();
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}.
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if the iterator is empty
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getOnlyElement(Iterator<T> iterator) {
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    T first = iterator.next();
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (!iterator.hasNext()) {
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return first;
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    StringBuilder sb = new StringBuilder();
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    sb.append("expected one element but was: <" + first);
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (int i = 0; i < 4 && iterator.hasNext(); i++) {
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", " + iterator.next());
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator.hasNext()) {
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", ...");
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    sb.append('>');
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new IllegalArgumentException(sb.toString());
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}, or {@code
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * defaultValue} if the iterator is empty.
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getOnlyElement(
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator, @Nullable T defaultValue) {
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue;
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adds all elements in {@code iterator} to {@code collection}. The iterator
327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if {@code collection} was modified as a result of this
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *         operation
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> boolean addAll(
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Collection<T> addTo, Iterator<? extends T> iterator) {
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(addTo);
336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean wasModified = false;
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      wasModified |= addTo.add(iterator.next());
339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return wasModified;
341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements in the specified iterator that equal the
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * specified object. The iterator will be left exhausted: its
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @see Collections#frequency
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int frequency(Iterator<?> iterator, @Nullable Object element) {
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int result = 0;
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (element == null) {
353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (iterator.next() == null) {
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          result++;
356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    } else {
359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (element.equals(iterator.next())) {
361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          result++;
362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return result;
366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the elements of {@code
370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterable}.
371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After {@code remove()} is called, subsequent cycles omit the removed
374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, which is no longer in {@code iterable}. The iterator's
375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until {@code iterable} is
376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * empty.
377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterable);
384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator = emptyIterator();
386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> removeFrom;
387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
3881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!iterator.hasNext()) {
391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          iterator = iterable.iterator();
392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
3951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = iterator;
401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
4031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkState(removeFrom != null,
406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            "no calls to next() since last call to remove()");
407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the provided elements.
415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After {@code remove()} is called, subsequent cycles omit the removed
418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, but {@code elements} does not change. The iterator's
419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until all of the original
420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * elements have been removed.
421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(T... elements) {
427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return cycle(Lists.newArrayList(elements));
428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines two iterators into a single iterator. The returned iterator
432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}. The source iterators are not polled until necessary.
434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T> a,
440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> b) {
441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b).iterator());
444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines three iterators into a single iterator. The returned iterator
448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}. The source iterators
450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * are not polled until necessary.
451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T> a,
457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> b, Iterator<? extends T> c) {
458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(c);
461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b, c).iterator());
462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines four iterators into a single iterator. The returned iterator
466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}, followed by the elements
468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in {@code d}. The source iterators are not polled until necessary.
469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T> a,
475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> b, Iterator<? extends T> c,
476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> d) {
477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(c);
480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(d);
481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b, c, d).iterator());
482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
487090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
489090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
490090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any of the provided iterators is null
493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T>... inputs) {
4951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return concat(ImmutableList.copyOf(inputs).iterator());
496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it. The methods of the returned iterator may throw
5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code NullPointerException} if any of the input iterators is null.
506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(
508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Iterator<? extends Iterator<? extends T>> inputs) {
509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(inputs);
510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> current = emptyIterator();
512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> removeFrom;
513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
5141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // http://code.google.com/p/google-collections/issues/detail?id=151
517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // current.hasNext() might be relatively expensive, worth minimizing.
518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        boolean currentHasNext;
5191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // checkNotNull eager for GWT
5201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // note: it must be here & not where 'current' is assigned,
5211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // because otherwise we'll have called inputs.next() before throwing
5221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the first NPE, and the next time around we'll call inputs.next()
5231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // again, incorrectly moving beyond the error.
5241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        while (!(currentHasNext = checkNotNull(current).hasNext())
5251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            && inputs.hasNext()) {
526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          current = inputs.next();
527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return currentHasNext;
529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = current;
536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current.next();
537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
539090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkState(removeFrom != null,
541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            "no calls to next() since last call to remove()");
542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size (the final
550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * list may be smaller). For example, partitioning an iterator containing
551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code
552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * [[a, b, c], [d, e]]} -- an outer iterator containing two inner lists of
553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * three and two elements, all in the original order.
554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
556090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition (the last may be smaller)
559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions
561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<List<T>> partition(
564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator, int size) {
565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, false);
566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size, padding
570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * the final iterator with null values if necessary. For example, partitioning
571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * an iterator containing {@code [a, b, c, d, e]} with a partition size of 3
572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterator containing
573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * two inner lists of three elements each, all in the original order.
574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition
579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions (the final iterable may have
581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     trailing null elements)
582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<List<T>> paddedPartition(
585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator, int size) {
586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, true);
587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static <T> UnmodifiableIterator<List<T>> partitionImpl(
590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Iterator<T> iterator, final int size, final boolean pad) {
591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(size > 0);
593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<List<T>>() {
5941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public List<T> next() {
600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Object[] array = new Object[size];
604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        int count = 0;
605090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        for (; count < size && iterator.hasNext(); count++) {
606090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          array[count] = iterator.next();
607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
6081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        for (int i = count; i < size; i++) {
6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          array[i] = null; // for GWT
6101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
611090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
612090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        @SuppressWarnings("unchecked") // we only put Ts in it
613090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        List<T> list = Collections.unmodifiableList(
614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            (List<T>) Arrays.asList(array));
6151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return (pad || count == size) ? list : list.subList(0, count);
616090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
621090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the elements of {@code unfiltered} that satisfy a predicate.
622090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
623090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> filter(
624090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Iterator<T> unfiltered, final Predicate<? super T> predicate) {
625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(unfiltered);
626090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
627090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AbstractIterator<T>() {
628090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @Override protected T computeNext() {
629090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        while (unfiltered.hasNext()) {
630090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          T element = unfiltered.next();
631090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          if (predicate.apply(element)) {
632090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            return element;
633090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          }
634090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
635090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return endOfData();
636090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
637090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
638090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
639090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
640090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
641090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if one or more elements returned by {@code iterator}
642090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfy the given predicate.
643090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
644090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> boolean any(
645090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator, Predicate<? super T> predicate) {
646090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
647090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
648090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T element = iterator.next();
649090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (predicate.apply(element)) {
650090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return true;
651090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
652090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
653090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
654090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
655090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
656090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
657090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if every element returned by {@code iterator}
658090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfies the given predicate. If {@code iterator} is empty, {@code true}
659090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * is returned.
660090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
661090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> boolean all(
662090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator, Predicate<? super T> predicate) {
663090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
664090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
665090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T element = iterator.next();
666090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!predicate.apply(element)) {
667090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
668090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
669090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
670090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return true;
671090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
672090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
673090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
674090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the first element in {@code iterator} that satisfies the given
6751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate; use this method only when such an element is known to exist. If
6761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, the iterator will be left exhausted: its {@code
6771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}. If it is possible that
6781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <i>no</i> element will match, use {@link #tryFind)} or {@link
6791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * #find(Iterator, Predicate, T)} instead.
680090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
681090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if no element in {@code iterator} matches
682090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     the given predicate
683090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> T find(
6851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterator<T> iterator, Predicate<? super T> predicate) {
686090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return filter(iterator, predicate).next();
687090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
688090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
689090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
6901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the first element in {@code iterator} that satisfies the given
6911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate. If no such element is found, {@code defaultValue} will be
6921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned from this method and the iterator will be left exhausted: its
6931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code hasNext()} method will return {@code false}. Note that this can
6941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * usually be handled more naturally using {@code
6951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * tryFind(iterator, predicate).or(defaultValue)}.
6961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> T find(Iterator<T> iterator, Predicate<? super T> predicate,
7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Nullable T defaultValue) {
7011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate);
7021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return filteredIterator.hasNext() ? filteredIterator.next() : defaultValue;
7031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an {@link Optional} containing the first element in {@code
7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator} that satisfies the given predicate, if such an element exists. If
7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, an empty {@link Optional} will be returned from
7091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * this method and the the iterator will be left exhausted: its {@code
7101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}.
7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code
7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * null}. If {@code null} is matched in {@code iterator}, a
7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * NullPointerException will be thrown.
7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
7171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> Optional<T> tryFind(
7191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterator<T> iterator, Predicate<? super T> predicate) {
7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate);
7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return filteredIterator.hasNext()
7221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        ? Optional.of(filteredIterator.next())
7231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        : Optional.<T>absent();
7241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
727bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns the index in {@code iterator} of the first element that satisfies
728bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the provided {@code predicate}, or {@code -1} if the Iterator has no such
729bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * elements.
730bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
731bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>More formally, returns the lowest index {@code i} such that
7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code predicate.apply(Iterators.get(iterator, i))} returns {@code true},
7331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@code -1} if there is no such index.
734bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
735bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>If -1 is returned, the iterator will be left exhausted: its
736bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code hasNext()} method will return {@code false}.  Otherwise,
737bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the iterator will be set to the element which satisfies the
738bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code predicate}.
739bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
741bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
742bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public static <T> int indexOf(
743bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      Iterator<T> iterator, Predicate<? super T> predicate) {
7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(predicate, "predicate");
745bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    int i = 0;
746bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    while (iterator.hasNext()) {
747bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      T current = iterator.next();
748bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      if (predicate.apply(current)) {
749bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return i;
750bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
751bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      i++;
752bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    }
753bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return -1;
754bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
755bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
756bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
757090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that applies {@code function} to each element of {@code
758090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * fromIterator}.
759090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
760090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
761090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After a successful {@code remove()} call, {@code fromIterator} no
762090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * longer contains the corresponding element.
763090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
764090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator,
765090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Function<? super F, ? extends T> function) {
766090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(fromIterator);
767090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(function);
768090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
770090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
771090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return fromIterator.hasNext();
772090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
774090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
775090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        F from = fromIterator.next();
776090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return function.apply(from);
777090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
779090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
780090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        fromIterator.remove();
781090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
782090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
783090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
784090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
785090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
7861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
7871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position.
788090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
789090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param position position of the element to return
790090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the element at the specified position in {@code iterator}
791090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IndexOutOfBoundsException if {@code position} is negative or
792090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     greater than or equal to the number of elements remaining in
793090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     {@code iterator}
794090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
795090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T get(Iterator<T> iterator, int position) {
7961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
797090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
798090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int skipped = 0;
799090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
800090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T t = iterator.next();
801090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (skipped++ == position) {
802090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return t;
803090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
804090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
805090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
806090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new IndexOutOfBoundsException("position (" + position
807090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        + ") must be less than the number of elements that remained ("
808090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        + skipped + ")");
809090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
810090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
8111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static void checkNonnegative(int position) {
8121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (position < 0) {
8131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new IndexOutOfBoundsException("position (" + position
8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          + ") must not be negative");
8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
8201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position or {@code defaultValue}
8211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * otherwise.
8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param position position of the element to return
8241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     or if {@code position} is greater than the number of elements
8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     remaining in {@code iterator}
8271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the element at the specified position in {@code iterator} or
8281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code defaultValue} if {@code iterator} produces fewer than
8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code position + 1} elements.
8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IndexOutOfBoundsException if {@code position} is negative
8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 4.0
8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> T get(Iterator<T> iterator, int position,
8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Nullable T defaultValue) {
8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return get(iterator, position);
8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (IndexOutOfBoundsException e) {
8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return defaultValue;
8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the next element in {@code iterator} or {@code defaultValue} if
8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iterator is empty.  The {@link Iterables} analog to this method is
8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Iterables#getFirst}.
8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the next element of {@code iterator} or the default value
8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> T getNext(Iterator<T> iterator, @Nullable T defaultValue) {
8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? iterator.next() : defaultValue;
8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
857090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
858090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Advances {@code iterator} to the end, returning the last element.
859090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
860090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the last element of {@code iterator}
8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws NoSuchElementException if the iterator is empty
862090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
863090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getLast(Iterator<T> iterator) {
864090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (true) {
865090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T current = iterator.next();
866090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator.hasNext()) {
867090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current;
868090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
869090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
870090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
871090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
872bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} to the end, returning the last element or
8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code defaultValue} if the iterator is empty.
8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the last element of {@code iterator}
8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> T getLast(Iterator<T> iterator, @Nullable T defaultValue) {
8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? getLast(iterator) : defaultValue;
8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Calls {@code next()} on {@code iterator}, either {@code numberToSkip} times
8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or until {@code hasNext()} returns {@code false}, whichever comes first.
8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the number of elements skipped
8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> int skip(Iterator<T> iterator, int numberToSkip) {
8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(numberToSkip >= 0, "number to skip cannot be negative");
8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int i;
8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (i = 0; i < numberToSkip && iterator.hasNext(); i++) {
8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return i;
9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates an iterator returning the first {@code limitSize} elements of the
9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * given iterator. If the original iterator does not contain that many
9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * elements, the returned iterator will have the same behavior as the original
9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator. The returned iterator supports {@code remove()} if the original
9081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator does.
9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param iterator the iterator to limit
9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param limitSize the maximum number of elements in the returned iterator
9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code limitSize} is negative
9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> Iterator<T> limit(
9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      final Iterator<T> iterator, final int limitSize) {
9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(limitSize >= 0, "limit is negative");
9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new Iterator<T>() {
9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      private int count;
9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean hasNext() {
9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return count < limitSize && iterator.hasNext();
9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public T next() {
9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (!hasNext()) {
9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new NoSuchElementException();
9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        count++;
9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return iterator.next();
9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public void remove() {
9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        iterator.remove();
9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
944bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns a view of the supplied {@code iterator} that removes each element
945bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * from the supplied {@code iterator} as it is returned.
946bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
947bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>The provided iterator must support {@link Iterator#remove()} or
948bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * else the returned iterator will fail on the first call to {@code
949bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * next}.
950bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
951bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @param iterator the iterator to remove and return elements from
952bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @return an iterator that removes and returns elements from the
953bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *     supplied iterator
9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
955bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
956bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public static <T> Iterator<T> consumingIterator(final Iterator<T> iterator) {
957bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    checkNotNull(iterator);
958bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return new UnmodifiableIterator<T>() {
9591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
960bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public boolean hasNext() {
961bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return iterator.hasNext();
962bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
963bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
965bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public T next() {
966bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        T next = iterator.next();
967bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        iterator.remove();
968bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return next;
969bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
970bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    };
971bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
972bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
973090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Methods only in Iterators, not in Iterables
974090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
975090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
9761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Clears the iterator using its remove method.
9771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static void clear(Iterator<?> iterator) {
9791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    while (iterator.hasNext()) {
9811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
9821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.remove();
9831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
987090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing the elements of {@code array} in order. The
988090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned iterator is a view of the array; subsequent changes to the array
989090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be reflected in the iterator.
990090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
991090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Note:</b> It is often preferable to represent your data using a
992090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * collection type, for example using {@link Arrays#asList(Object[])}, making
993090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * this method unnecessary.
994090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
995090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
9961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Arrays#asList(Object[])}, {@link ImmutableList#copyOf(Object[])}},
9971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@link ImmutableList#of}.
998090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
999090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> forArray(final T... array) {
10001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // TODO(kevinb): compare performance with Arrays.asList(array).iterator().
1001bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    checkNotNull(array);  // eager for GWT.
10021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new AbstractIndexedListIterator<T>(array.length) {
10031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override protected T get(int index) {
10041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return array[index];
1005090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1006090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1007090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1008090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1009090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1010090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing the elements in the specified range of
1011090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code array} in order. The returned iterator is a view of the array;
1012090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * subsequent changes to the array will be reflected in the iterator.
1013090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1014090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is {@code
1015090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Arrays.asList(array).subList(offset, offset + length)}.
1016090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1017090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param array array to read elements out of
1018090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param offset index of first array element to retrieve
1019090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param length number of elements in iteration
10201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IndexOutOfBoundsException if {@code offset} is negative, {@code
10211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     length} is negative, or {@code offset + length > array.length}
1022090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1023090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> UnmodifiableIterator<T> forArray(
1024090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final T[] array, final int offset, int length) {
1025090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(length >= 0);
10261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int end = offset + length;
1027090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1028090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // Technically we should give a slightly more descriptive error on overflow
1029090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Preconditions.checkPositionIndexes(offset, end, array.length);
1030090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
10311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /*
10321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * We can't use call the two-arg constructor with arguments (offset, end)
10331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * because the returned Iterator is a ListIterator that may be moved back
10341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * past the beginning of the iteration.
10351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
10361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new AbstractIndexedListIterator<T>(length) {
10371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override protected T get(int index) {
10381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return array[offset + index];
1039090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1040090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1041090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1042090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1043090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1044090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing only {@code value}.
1045090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1046090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
1047090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#singleton}.
1048090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1049090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> singletonIterator(
1050090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @Nullable final T value) {
1051090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1052090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      boolean done;
10531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1054090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1055090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return !done;
1056090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1058090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1059090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (done) {
1060090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
1061090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
1062090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        done = true;
1063090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return value;
1064090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1065090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1066090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1067090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1068090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1069090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Enumeration} to the {@code Iterator} interface.
1070090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1071090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>This method has no equivalent in {@link Iterables} because viewing an
1072090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Enumeration} as an {@code Iterable} is impossible. However, the
1073090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contents can be <i>copied</i> into a collection using {@link
1074090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#list}.
1075090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1076090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> forEnumeration(
1077090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Enumeration<T> enumeration) {
1078090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(enumeration);
1079090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
10801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1081090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1082090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.hasMoreElements();
1083090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
10841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1085090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1086090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.nextElement();
1087090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1088090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1089090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1090090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1091090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1092090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Iterator} to the {@code Enumeration} interface.
1093090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1094090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
1095090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#enumeration} (if you have a {@link Collection}), or
1096090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Iterators.asEnumeration(collection.iterator())}.
1097090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1098090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Enumeration<T> asEnumeration(final Iterator<T> iterator) {
1099090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Enumeration<T>() {
11011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasMoreElements() {
1103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
1104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
1106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T nextElement() {
1107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Implementation of PeekingIterator that avoids peeking unless necessary.
1114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class PeekingImpl<E> implements PeekingIterator<E> {
1116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Iterator<? extends E> iterator;
1118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private boolean hasPeeked;
1119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private E peekedElement;
1120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public PeekingImpl(Iterator<? extends E> iterator) {
1122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.iterator = checkNotNull(iterator);
1123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
1126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean hasNext() {
1127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return hasPeeked || iterator.hasNext();
1128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
1131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E next() {
1132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      E result = peekedElement;
1136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      hasPeeked = false;
1137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      peekedElement = null;
1138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
1139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
1142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public void remove() {
1143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkState(!hasPeeked, "Can't remove after you've peeked at next");
1144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.remove();
1145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
1148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E peek() {
1149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        peekedElement = iterator.next();
1151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        hasPeeked = true;
1152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peekedElement;
1154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a {@code PeekingIterator} backed by the given iterator.
1159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Calls to the {@code peek} method with no intervening calls to {@code
1161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * next} do not affect the iteration, and hence return the same object each
1162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * time. A subsequent call to {@code next} is guaranteed to return the same
1163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * object again. For example: <pre>   {@code
1164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   PeekingIterator<String> peekingIterator =
1166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       Iterators.peekingIterator(Iterators.forArray("a", "b"));
1167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a1 = peekingIterator.peek(); // returns "a"
1168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a2 = peekingIterator.peek(); // also returns "a"
1169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a3 = peekingIterator.next(); // also returns "a"}</pre>
1170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Any structural changes to the underlying iteration (aside from those
1172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * performed by the iterator's own {@link PeekingIterator#remove()} method)
1173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will leave the iterator in an undefined state.
1174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator does not support removal after peeking, as
1176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * explained by {@link PeekingIterator#remove()}.
1177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note: If the given iterator is already a {@code PeekingIterator},
1179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it <i>might</i> be returned to the caller, although this is neither
1180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * guaranteed to occur nor required to be consistent.  For example, this
1181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method <i>might</i> choose to pass through recognized implementations of
1182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code PeekingIterator} when the behavior of the implementation is
1183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * known to meet the contract guaranteed by this method.
1184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>There is no {@link Iterable} equivalent to this method, so use this
1186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method to wrap each individual iterator as it is generated.
1187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the backing iterator. The {@link PeekingIterator} assumes
1189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     ownership of this iterator, so users should cease making direct calls
1190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     to it after calling this method.
1191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a peeking iterator backed by that iterator. Apart from the
1192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     additional {@link PeekingIterator#peek()} method, this iterator behaves
1193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     exactly the same as {@code iterator}.
1194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> PeekingIterator<T> peekingIterator(
1196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> iterator) {
1197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator instanceof PeekingImpl) {
1198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // Safe to cast <? extends T> to <T> because PeekingImpl only uses T
1199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // covariantly (and cannot be subclassed to add non-covariant uses).
1200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked")
1201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      PeekingImpl<T> peeking = (PeekingImpl<T>) iterator;
1202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peeking;
1203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new PeekingImpl<T>(iterator);
1205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
12061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
12091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
12111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
12121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Deprecated public static <T> PeekingIterator<T> peekingIterator(
12141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      PeekingIterator<T> iterator) {
12151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
12161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an iterator over the merged contents of all given
12201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code iterators}, traversing every element of the input iterators.
12211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Equivalent entries will not be de-duplicated.
12221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Callers must ensure that the source {@code iterators} are in
12241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * non-descending order as this method does not sort its input.
12251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For any equivalent elements across all {@code iterators}, it is
12271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * undefined which element is returned first.
12281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
12301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
12321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> UnmodifiableIterator<T> mergeSorted(
12331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterable<? extends Iterator<? extends T>> iterators,
12341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Comparator<? super T> comparator) {
12351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterators, "iterators");
12361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(comparator, "comparator");
12371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new MergingIterator<T>(iterators, comparator);
12391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An iterator that performs a lazy N-way merge, calculating the next value
12431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each time the iterator is polled. This amortizes the sorting cost over the
12441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iteration and requires less memory than sorting all elements at once.
12451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Retrieving a single element takes approximately O(log(M)) time, where M
12471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is the number of iterators. (Retrieving all elements takes approximately
12481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * O(N*log(M)) time, where N is the total number of elements.)
12491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static class MergingIterator<T> extends AbstractIterator<T> {
12511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Queue<PeekingIterator<T>> queue;
12521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Comparator<? super T> comparator;
12531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public MergingIterator(Iterable<? extends Iterator<? extends T>> iterators,
12551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Comparator<? super T> itemComparator) {
12561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.comparator = itemComparator;
12571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // A comparator that's used by the heap, allowing the heap
12591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // to be sorted based on the top of each iterator.
12601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Comparator<PeekingIterator<T>> heapComparator =
12611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          new Comparator<PeekingIterator<T>>() {
12621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            @Override
12631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            public int compare(PeekingIterator<T> o1, PeekingIterator<T> o2) {
12641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert              return comparator.compare(o1.peek(), o2.peek());
12651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            }
12661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          };
12671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      queue = new PriorityQueue<PeekingIterator<T>>(2, heapComparator);
12691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      for (Iterator<? extends T> iterator : iterators) {
12711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (iterator.hasNext()) {
12721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          queue.add(Iterators.peekingIterator(iterator));
12731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
12741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
12781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    protected T computeNext() {
12791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (queue.isEmpty()) {
12801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return endOfData();
12811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      PeekingIterator<T> nextIter = queue.poll();
12841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      T next = nextIter.next();
12851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (nextIter.hasNext()) {
12871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        queue.add(nextIter);
12881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return next;
12911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
1294