Iterators.java revision 7dd252788645e940eada959bdde927426e2531c9
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.annotations.GwtIncompatible;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function;
277dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.base.Joiner;
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects;
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions;
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate;
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicates;
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays;
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections;
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator;
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Enumeration;
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
417dd252788645e940eada959bdde927426e2531c9Paul Duffinimport java.util.ListIterator;
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException;
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.PriorityQueue;
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue;
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class contains static utility methods that operate on or return objects
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of type {@link Iterator}. Except as noted, each method has a corresponding
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterable}-based method in the {@link Iterables} class.
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterators
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that they only advance
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the backing iteration when absolutely necessary.
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
577dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>See the Guava User Guide section on <a href=
587dd252788645e940eada959bdde927426e2531c9Paul Duffin * "http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained#Iterables">
597dd252788645e940eada959bdde927426e2531c9Paul Duffin * {@code Iterators}</a>.
607dd252788645e940eada959bdde927426e2531c9Paul Duffin *
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Iterators {
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Iterators() {}
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
697dd252788645e940eada959bdde927426e2531c9Paul Duffin  static final UnmodifiableListIterator<Object> EMPTY_LIST_ITERATOR = new UnmodifiableListIterator<Object>() {
707dd252788645e940eada959bdde927426e2531c9Paul Duffin
717dd252788645e940eada959bdde927426e2531c9Paul Duffin    public boolean hasNext() {
727dd252788645e940eada959bdde927426e2531c9Paul Duffin      return false;
737dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
747dd252788645e940eada959bdde927426e2531c9Paul Duffin
757dd252788645e940eada959bdde927426e2531c9Paul Duffin    public Object next() {
767dd252788645e940eada959bdde927426e2531c9Paul Duffin      throw new NoSuchElementException();
777dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
787dd252788645e940eada959bdde927426e2531c9Paul Duffin
797dd252788645e940eada959bdde927426e2531c9Paul Duffin    public boolean hasPrevious() {
807dd252788645e940eada959bdde927426e2531c9Paul Duffin      return false;
817dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
827dd252788645e940eada959bdde927426e2531c9Paul Duffin
837dd252788645e940eada959bdde927426e2531c9Paul Duffin    public Object previous() {
847dd252788645e940eada959bdde927426e2531c9Paul Duffin      throw new NoSuchElementException();
857dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
867dd252788645e940eada959bdde927426e2531c9Paul Duffin
877dd252788645e940eada959bdde927426e2531c9Paul Duffin    public int nextIndex() {
887dd252788645e940eada959bdde927426e2531c9Paul Duffin      return 0;
897dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
907dd252788645e940eada959bdde927426e2531c9Paul Duffin
917dd252788645e940eada959bdde927426e2531c9Paul Duffin    public int previousIndex() {
927dd252788645e940eada959bdde927426e2531c9Paul Duffin      return -1;
937dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
947dd252788645e940eada959bdde927426e2531c9Paul Duffin  };
957dd252788645e940eada959bdde927426e2531c9Paul Duffin
967dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
977dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns the empty iterator.
987dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
997dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>The {@link Iterable} equivalent of this method is {@link
1007dd252788645e940eada959bdde927426e2531c9Paul Duffin   * ImmutableSet#of()}.
1017dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
1027dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> emptyIterator() {
1037dd252788645e940eada959bdde927426e2531c9Paul Duffin    return emptyListIterator();
1047dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty iterator.
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ImmutableSet#of()}.
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
1147dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> UnmodifiableListIterator<T> emptyListIterator() {
1157dd252788645e940eada959bdde927426e2531c9Paul Duffin    return (UnmodifiableListIterator<T>) EMPTY_LIST_ITERATOR;
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1187dd252788645e940eada959bdde927426e2531c9Paul Duffin  private static final Iterator<Object> EMPTY_MODIFIABLE_ITERATOR = new Iterator<Object>() {
1197dd252788645e940eada959bdde927426e2531c9Paul Duffin    public boolean hasNext() {
1207dd252788645e940eada959bdde927426e2531c9Paul Duffin      return false;
1217dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1237dd252788645e940eada959bdde927426e2531c9Paul Duffin    public Object next() {
1247dd252788645e940eada959bdde927426e2531c9Paul Duffin      throw new NoSuchElementException();
1257dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1277dd252788645e940eada959bdde927426e2531c9Paul Duffin    public void remove() {
1287dd252788645e940eada959bdde927426e2531c9Paul Duffin      throw new IllegalStateException();
1297dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
1307dd252788645e940eada959bdde927426e2531c9Paul Duffin  };
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty {@code Iterator} that throws
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link IllegalStateException} instead of
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link UnsupportedOperationException} on a call to
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link Iterator#remove()}.
137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> Iterator<T> emptyModifiableIterator() {
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Iterator<T>) EMPTY_MODIFIABLE_ITERATOR;
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Returns an unmodifiable view of {@code iterator}. */
1457dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> unmodifiableIterator(final Iterator<T> iterator) {
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (iterator instanceof UnmodifiableIterator) {
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return (UnmodifiableIterator<T>) iterator;
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1517dd252788645e940eada959bdde927426e2531c9Paul Duffin
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1557dd252788645e940eada959bdde927426e2531c9Paul Duffin
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1687dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Deprecated
1697dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> unmodifiableIterator(UnmodifiableIterator<T> iterator) {
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements remaining in {@code iterator}. The iterator
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int size(Iterator<?> iterator) {
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int count = 0;
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.next();
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      count++;
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return count;
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if {@code iterator} contains {@code element}.
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1907dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static boolean contains(Iterator<?> iterator, @Nullable Object element) {
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (element == null) {
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (iterator.next() == null) {
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return true;
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    } else {
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (element.equals(iterator.next())) {
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return true;
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that belongs to the
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRemove the elements to remove
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2167dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static boolean removeAll(Iterator<?> removeFrom, Collection<?> elementsToRemove) {
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(elementsToRemove);
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (elementsToRemove.contains(removeFrom.next())) {
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Removes every element that satisfies the provided predicate from the
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterator. The iterator will be left exhausted: its {@code hasNext()}
231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method will return {@code false}.
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param predicate a predicate that determines whether an element should
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     be removed
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if any elements were removed from the iterator
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2397dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> boolean removeIf(Iterator<T> removeFrom, Predicate<? super T> predicate) {
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (predicate.apply(removeFrom.next())) {
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that does not belong to the
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRetain the elements to retain
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2607dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static boolean retainAll(Iterator<?> removeFrom, Collection<?> elementsToRetain) {
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(elementsToRetain);
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!elementsToRetain.contains(removeFrom.next())) {
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Determines whether two iterators contain equal elements in the same order.
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * More specifically, this method returns {@code true} if {@code iterator1}
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * and {@code iterator2} contain the same number of elements and every element
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * of {@code iterator1} is equal to the corresponding element of
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code iterator2}.
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note that this will modify the supplied iterators, since they will have
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * been advanced some number of elements forward.
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2827dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static boolean elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2) {
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator1.hasNext()) {
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator2.hasNext()) {
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o1 = iterator1.next();
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o2 = iterator2.next();
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!Objects.equal(o1, o2)) {
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return !iterator2.hasNext();
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a string representation of {@code iterator}, with the format
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [e1, e2, ..., en]}. The iterator will be left exhausted: its
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static String toString(Iterator<?> iterator) {
3027dd252788645e940eada959bdde927426e2531c9Paul Duffin    return Joiner.on(", ").useForNull("null").appendTo(new StringBuilder().append('['), iterator)
3037dd252788645e940eada959bdde927426e2531c9Paul Duffin        .append(']').toString();
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}.
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if the iterator is empty
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getOnlyElement(Iterator<T> iterator) {
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    T first = iterator.next();
315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (!iterator.hasNext()) {
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return first;
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    StringBuilder sb = new StringBuilder();
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    sb.append("expected one element but was: <" + first);
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (int i = 0; i < 4 && iterator.hasNext(); i++) {
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", " + iterator.next());
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator.hasNext()) {
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", ...");
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    sb.append('>');
328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new IllegalArgumentException(sb.toString());
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}, or {@code
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * defaultValue} if the iterator is empty.
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3397dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
3407dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getOnlyElement(Iterator<? extends T> iterator, @Nullable T defaultValue) {
341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue;
342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Copies an iterator's elements into an array. The iterator will be left
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * exhausted: its {@code hasNext()} method will return {@code false}.
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to copy
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param type the type of the elements
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a newly-allocated array into which all the elements of the iterator
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *         have been copied
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("Array.newInstance(Class, int)")
3547dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) {
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    List<T> list = Lists.newArrayList(iterator);
356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Iterables.toArray(list, type);
357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adds all elements in {@code iterator} to {@code collection}. The iterator
361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if {@code collection} was modified as a result of this
365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *         operation
366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3677dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(addTo);
369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean wasModified = false;
370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      wasModified |= addTo.add(iterator.next());
372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return wasModified;
374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements in the specified iterator that equal the
378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * specified object. The iterator will be left exhausted: its
379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @see Collections#frequency
382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int frequency(Iterator<?> iterator, @Nullable Object element) {
384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int result = 0;
385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (element == null) {
386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (iterator.next() == null) {
388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          result++;
389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    } else {
392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      while (iterator.hasNext()) {
393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (element.equals(iterator.next())) {
394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          result++;
395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return result;
399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the elements of {@code
403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterable}.
404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After {@code remove()} is called, subsequent cycles omit the removed
407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, which is no longer in {@code iterable}. The iterator's
408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until {@code iterable} is
409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * empty.
410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterable);
417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator = emptyIterator();
419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> removeFrom;
420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!iterator.hasNext()) {
423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          iterator = iterable.iterator();
424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
4277dd252788645e940eada959bdde927426e2531c9Paul Duffin
428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = iterator;
433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
4357dd252788645e940eada959bdde927426e2531c9Paul Duffin
436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
4377dd252788645e940eada959bdde927426e2531c9Paul Duffin        checkState(removeFrom != null, "no calls to next() since last call to remove()");
438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the provided elements.
446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
4477dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>The returned iterator supports {@code remove()}. After {@code remove()}
4487dd252788645e940eada959bdde927426e2531c9Paul Duffin   * is called, subsequent cycles omit the removed
449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, but {@code elements} does not change. The iterator's
450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until all of the original
451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * elements have been removed.
452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(T... elements) {
458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return cycle(Lists.newArrayList(elements));
459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines two iterators into a single iterator. The returned iterator
463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}. The source iterators are not polled until necessary.
465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
4687dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
4697dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
4707dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
4717dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
4727dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
4757dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a, Iterator<? extends T> b) {
476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b).iterator());
479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines three iterators into a single iterator. The returned iterator
483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}. The source iterators
485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * are not polled until necessary.
486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
487090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
4897dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
4907dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
4917dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
4927dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
4937dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
4967dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a, Iterator<? extends T> b,
4977dd252788645e940eada959bdde927426e2531c9Paul Duffin      Iterator<? extends T> c) {
498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(c);
501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b, c).iterator());
502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
505090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines four iterators into a single iterator. The returned iterator
506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}, followed by the elements
508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in {@code d}. The source iterators are not polled until necessary.
509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
5127dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
5137dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
5147dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
5157dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
5167dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
5197dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a, Iterator<? extends T> b,
5207dd252788645e940eada959bdde927426e2531c9Paul Duffin      Iterator<? extends T> c, Iterator<? extends T> d) {
521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(a);
522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(b);
523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(c);
524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(d);
525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return concat(Arrays.asList(a, b, c, d).iterator());
526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
5367dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
5377dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
5387dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
5397dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
5407dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any of the provided iterators is null
542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T>... inputs) {
5441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return concat(ImmutableList.copyOf(inputs).iterator());
545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it. The methods of the returned iterator may throw
5541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code NullPointerException} if any of the input iterators is null.
5557dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
5567dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
5577dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
5587dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
5597dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
5617dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Iterator<T> concat(final Iterator<? extends Iterator<? extends T>> inputs) {
562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(inputs);
563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> current = emptyIterator();
565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> removeFrom;
566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // http://code.google.com/p/google-collections/issues/detail?id=151
569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // current.hasNext() might be relatively expensive, worth minimizing.
570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        boolean currentHasNext;
5711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // checkNotNull eager for GWT
5721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // note: it must be here & not where 'current' is assigned,
5731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // because otherwise we'll have called inputs.next() before throwing
5741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the first NPE, and the next time around we'll call inputs.next()
5751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // again, incorrectly moving beyond the error.
5767dd252788645e940eada959bdde927426e2531c9Paul Duffin        while (!(currentHasNext = checkNotNull(current).hasNext()) && inputs.hasNext()) {
577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          current = inputs.next();
578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return currentHasNext;
580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5817dd252788645e940eada959bdde927426e2531c9Paul Duffin
582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = current;
587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current.next();
588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5897dd252788645e940eada959bdde927426e2531c9Paul Duffin
590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
5917dd252788645e940eada959bdde927426e2531c9Paul Duffin        checkState(removeFrom != null, "no calls to next() since last call to remove()");
592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size (the final
600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * list may be smaller). For example, partitioning an iterator containing
601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code
602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * [[a, b, c], [d, e]]} -- an outer iterator containing two inner lists of
603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * three and two elements, all in the original order.
604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
605090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
606090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition (the last may be smaller)
609090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
610090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions
611090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
612090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6137dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<List<T>> partition(Iterator<T> iterator, int size) {
614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, false);
615090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
616090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size, padding
619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * the final iterator with null values if necessary. For example, partitioning
620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * an iterator containing {@code [a, b, c, d, e]} with a partition size of 3
621090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterator containing
622090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * two inner lists of three elements each, all in the original order.
623090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
624090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
626090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
627090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition
628090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
629090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions (the final iterable may have
630090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     trailing null elements)
631090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
632090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6337dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<List<T>> paddedPartition(Iterator<T> iterator, int size) {
634090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, true);
635090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
636090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
6377dd252788645e940eada959bdde927426e2531c9Paul Duffin  private static <T> UnmodifiableIterator<List<T>> partitionImpl(final Iterator<T> iterator,
6387dd252788645e940eada959bdde927426e2531c9Paul Duffin      final int size, final boolean pad) {
639090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
640090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(size > 0);
641090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<List<T>>() {
6427dd252788645e940eada959bdde927426e2531c9Paul Duffin
643090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
644090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
645090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
6467dd252788645e940eada959bdde927426e2531c9Paul Duffin
647090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public List<T> next() {
648090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
649090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
650090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
651090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Object[] array = new Object[size];
652090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        int count = 0;
653090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        for (; count < size && iterator.hasNext(); count++) {
654090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          array[count] = iterator.next();
655090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        for (int i = count; i < size; i++) {
6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          array[i] = null; // for GWT
6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
659090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
6607dd252788645e940eada959bdde927426e2531c9Paul Duffin        @SuppressWarnings("unchecked")
6617dd252788645e940eada959bdde927426e2531c9Paul Duffin        // we only put Ts in it
6627dd252788645e940eada959bdde927426e2531c9Paul Duffin        List<T> list = Collections.unmodifiableList((List<T>) Arrays.asList(array));
6631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return (pad || count == size) ? list : list.subList(0, count);
664090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
665090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
666090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
667090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
668090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
669090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the elements of {@code unfiltered} that satisfy a predicate.
670090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6717dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> filter(final Iterator<T> unfiltered,
6727dd252788645e940eada959bdde927426e2531c9Paul Duffin      final Predicate<? super T> predicate) {
673090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(unfiltered);
674090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
675090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AbstractIterator<T>() {
6767dd252788645e940eada959bdde927426e2531c9Paul Duffin      @Override
6777dd252788645e940eada959bdde927426e2531c9Paul Duffin      protected T computeNext() {
678090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        while (unfiltered.hasNext()) {
679090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          T element = unfiltered.next();
680090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          if (predicate.apply(element)) {
681090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            return element;
682090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          }
683090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
684090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return endOfData();
685090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
686090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
687090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
688090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
689090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
690090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns all instances of class {@code type} in {@code unfiltered}. The
691090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned iterator has elements whose class is {@code type} or a subclass of
692090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code type}.
693090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
694090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param unfiltered an iterator containing objects of any type
695090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param type the type of elements desired
696090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an unmodifiable iterator containing all elements of the original
697090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator that were of the requested type
698090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6997dd252788645e940eada959bdde927426e2531c9Paul Duffin  @SuppressWarnings("unchecked")
7007dd252788645e940eada959bdde927426e2531c9Paul Duffin  // can cast to <T> because non-Ts are removed
701090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @GwtIncompatible("Class.isInstance")
7027dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> filter(Iterator<?> unfiltered, Class<T> type) {
7037dd252788645e940eada959bdde927426e2531c9Paul Duffin    return (UnmodifiableIterator<T>) filter(unfiltered, Predicates.instanceOf(type));
704090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
705090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
706090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
707090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if one or more elements returned by {@code iterator}
708090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfy the given predicate.
709090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
7107dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> boolean any(Iterator<T> iterator, Predicate<? super T> predicate) {
711090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
712090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
713090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T element = iterator.next();
714090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (predicate.apply(element)) {
715090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return true;
716090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
717090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
718090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
719090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
720090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
721090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
722090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if every element returned by {@code iterator}
723090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfies the given predicate. If {@code iterator} is empty, {@code true}
724090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * is returned.
725090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
7267dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> boolean all(Iterator<T> iterator, Predicate<? super T> predicate) {
727090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
728090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
729090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T element = iterator.next();
730090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!predicate.apply(element)) {
731090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
732090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
733090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
734090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return true;
735090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
736090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
737090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
738090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the first element in {@code iterator} that satisfies the given
7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate; use this method only when such an element is known to exist. If
7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, the iterator will be left exhausted: its {@code
7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}. If it is possible that
7427dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <i>no</i> element will match, use {@link #tryFind} or {@link
7437dd252788645e940eada959bdde927426e2531c9Paul Duffin   * #find(Iterator, Predicate, Object)} instead.
744090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
745090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if no element in {@code iterator} matches
746090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     the given predicate
747090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
7487dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T find(Iterator<T> iterator, Predicate<? super T> predicate) {
749090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return filter(iterator, predicate).next();
750090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
751090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
752090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
7531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the first element in {@code iterator} that satisfies the given
7541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate. If no such element is found, {@code defaultValue} will be
7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned from this method and the iterator will be left exhausted: its
7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code hasNext()} method will return {@code false}. Note that this can
7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * usually be handled more naturally using {@code
7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * tryFind(iterator, predicate).or(defaultValue)}.
7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7627dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
7637dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T find(Iterator<? extends T> iterator, Predicate<? super T> predicate,
7641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Nullable T defaultValue) {
7657dd252788645e940eada959bdde927426e2531c9Paul Duffin    UnmodifiableIterator<? extends T> filteredIterator = filter(iterator, predicate);
7661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return filteredIterator.hasNext() ? filteredIterator.next() : defaultValue;
7671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an {@link Optional} containing the first element in {@code
7711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator} that satisfies the given predicate, if such an element exists. If
7721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, an empty {@link Optional} will be returned from
7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * this method and the the iterator will be left exhausted: its {@code
7741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}.
7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code
7771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * null}. If {@code null} is matched in {@code iterator}, a
7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * NullPointerException will be thrown.
7791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
7811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7827dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Optional<T> tryFind(Iterator<T> iterator, Predicate<? super T> predicate) {
7831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate);
7847dd252788645e940eada959bdde927426e2531c9Paul Duffin    return filteredIterator.hasNext() ? Optional.of(filteredIterator.next()) : Optional
7857dd252788645e940eada959bdde927426e2531c9Paul Duffin        .<T> absent();
7861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
789bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns the index in {@code iterator} of the first element that satisfies
790bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the provided {@code predicate}, or {@code -1} if the Iterator has no such
791bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * elements.
792bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
793bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>More formally, returns the lowest index {@code i} such that
7941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code predicate.apply(Iterators.get(iterator, i))} returns {@code true},
7951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@code -1} if there is no such index.
796bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
797bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>If -1 is returned, the iterator will be left exhausted: its
798bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code hasNext()} method will return {@code false}.  Otherwise,
799bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the iterator will be set to the element which satisfies the
800bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code predicate}.
801bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
8021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
803bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
8047dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> int indexOf(Iterator<T> iterator, Predicate<? super T> predicate) {
8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(predicate, "predicate");
806bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    int i = 0;
807bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    while (iterator.hasNext()) {
808bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      T current = iterator.next();
809bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      if (predicate.apply(current)) {
810bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return i;
811bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
812bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      i++;
813bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    }
814bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return -1;
815bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
816bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
817bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
818090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that applies {@code function} to each element of {@code
819090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * fromIterator}.
820090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
821090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
822090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After a successful {@code remove()} call, {@code fromIterator} no
823090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * longer contains the corresponding element.
824090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
825090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator,
826090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Function<? super F, ? extends T> function) {
827090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(function);
8287dd252788645e940eada959bdde927426e2531c9Paul Duffin    return new TransformedIterator<F, T>(fromIterator) {
8297dd252788645e940eada959bdde927426e2531c9Paul Duffin
8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
8317dd252788645e940eada959bdde927426e2531c9Paul Duffin      T transform(F from) {
832090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return function.apply(from);
833090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
834090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
835090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
836090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
837090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position.
840090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
841090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param position position of the element to return
842090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the element at the specified position in {@code iterator}
843090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IndexOutOfBoundsException if {@code position} is negative or
844090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     greater than or equal to the number of elements remaining in
845090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     {@code iterator}
846090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
847090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T get(Iterator<T> iterator, int position) {
8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
849090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
850090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int skipped = 0;
851090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
852090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T t = iterator.next();
853090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (skipped++ == position) {
854090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return t;
855090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
856090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
857090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
858090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new IndexOutOfBoundsException("position (" + position
8597dd252788645e940eada959bdde927426e2531c9Paul Duffin        + ") must be less than the number of elements that remained (" + skipped + ")");
860090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
861090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static void checkNonnegative(int position) {
8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (position < 0) {
8647dd252788645e940eada959bdde927426e2531c9Paul Duffin      throw new IndexOutOfBoundsException("position (" + position + ") must not be negative");
8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position or {@code defaultValue}
8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * otherwise.
8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param position position of the element to return
8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     or if {@code position} is greater than the number of elements
8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     remaining in {@code iterator}
8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the element at the specified position in {@code iterator} or
8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code defaultValue} if {@code iterator} produces fewer than
8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code position + 1} elements.
8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IndexOutOfBoundsException if {@code position} is negative
8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 4.0
8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8837dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
8847dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T get(Iterator<? extends T> iterator, int position, @Nullable T defaultValue) {
8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return get(iterator, position);
8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } catch (IndexOutOfBoundsException e) {
8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return defaultValue;
8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the next element in {@code iterator} or {@code defaultValue} if
8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iterator is empty.  The {@link Iterables} analog to this method is
8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Iterables#getFirst}.
8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the next element of {@code iterator} or the default value
9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9037dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
9047dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getNext(Iterator<? extends T> iterator, @Nullable T defaultValue) {
9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? iterator.next() : defaultValue;
9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
908090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
909090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Advances {@code iterator} to the end, returning the last element.
910090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
911090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the last element of {@code iterator}
9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws NoSuchElementException if the iterator is empty
913090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
914090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getLast(Iterator<T> iterator) {
915090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (true) {
916090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T current = iterator.next();
917090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator.hasNext()) {
918090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current;
919090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
920090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
921090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
922090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
923bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} to the end, returning the last element or
9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code defaultValue} if the iterator is empty.
9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the last element of {@code iterator}
9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9317dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
9327dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getLast(Iterator<? extends T> iterator, @Nullable T defaultValue) {
9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? getLast(iterator) : defaultValue;
9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9377dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Calls {@code next()} on {@code iterator}, either {@code numberToAdvance} times
9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or until {@code hasNext()} returns {@code false}, whichever comes first.
9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9407dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @return the number of elements the iterator was advanced
9417dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @since 13.0 (since 3.0 as {@code Iterators.skip})
9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9437dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static int advance(Iterator<?> iterator, int numberToAdvance) {
9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9457dd252788645e940eada959bdde927426e2531c9Paul Duffin    checkArgument(numberToAdvance >= 0, "number to advance cannot be negative");
9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int i;
9487dd252788645e940eada959bdde927426e2531c9Paul Duffin    for (i = 0; i < numberToAdvance && iterator.hasNext(); i++) {
9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return i;
9521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates an iterator returning the first {@code limitSize} elements of the
9561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * given iterator. If the original iterator does not contain that many
9571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * elements, the returned iterator will have the same behavior as the original
9581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator. The returned iterator supports {@code remove()} if the original
9591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator does.
9601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param iterator the iterator to limit
9621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param limitSize the maximum number of elements in the returned iterator
9631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code limitSize} is negative
9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9667dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> Iterator<T> limit(final Iterator<T> iterator, final int limitSize) {
9671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(limitSize >= 0, "limit is negative");
9691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new Iterator<T>() {
9701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      private int count;
9711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean hasNext() {
9731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return count < limitSize && iterator.hasNext();
9741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public T next() {
9771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (!hasNext()) {
9781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new NoSuchElementException();
9791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
9801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        count++;
9811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return iterator.next();
9821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public void remove() {
9851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        iterator.remove();
9861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
9881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
991bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns a view of the supplied {@code iterator} that removes each element
992bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * from the supplied {@code iterator} as it is returned.
993bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
994bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>The provided iterator must support {@link Iterator#remove()} or
995bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * else the returned iterator will fail on the first call to {@code
996bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * next}.
997bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
998bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @param iterator the iterator to remove and return elements from
999bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @return an iterator that removes and returns elements from the
1000bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *     supplied iterator
10011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
1002bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
1003bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public static <T> Iterator<T> consumingIterator(final Iterator<T> iterator) {
1004bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    checkNotNull(iterator);
1005bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return new UnmodifiableIterator<T>() {
10067dd252788645e940eada959bdde927426e2531c9Paul Duffin
1007bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public boolean hasNext() {
1008bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return iterator.hasNext();
1009bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
1010bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
1011bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public T next() {
1012bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        T next = iterator.next();
1013bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        iterator.remove();
1014bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return next;
1015bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
1016bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    };
1017bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
1018bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
10197dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
10207dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Deletes and returns the next value from the iterator, or returns
10217dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code defaultValue} if there is no such value.
10227dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
10237dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
10247dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> T pollNext(Iterator<T> iterator) {
10257dd252788645e940eada959bdde927426e2531c9Paul Duffin    if (iterator.hasNext()) {
10267dd252788645e940eada959bdde927426e2531c9Paul Duffin      T result = iterator.next();
10277dd252788645e940eada959bdde927426e2531c9Paul Duffin      iterator.remove();
10287dd252788645e940eada959bdde927426e2531c9Paul Duffin      return result;
10297dd252788645e940eada959bdde927426e2531c9Paul Duffin    } else {
10307dd252788645e940eada959bdde927426e2531c9Paul Duffin      return null;
10317dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
10327dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
10337dd252788645e940eada959bdde927426e2531c9Paul Duffin
1034090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Methods only in Iterators, not in Iterables
1035090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1036090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
10371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Clears the iterator using its remove method.
10381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
10391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static void clear(Iterator<?> iterator) {
10401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
10411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    while (iterator.hasNext()) {
10421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
10431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.remove();
10441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
10451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
10461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
10471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1048090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing the elements of {@code array} in order. The
1049090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned iterator is a view of the array; subsequent changes to the array
1050090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be reflected in the iterator.
1051090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1052090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Note:</b> It is often preferable to represent your data using a
1053090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * collection type, for example using {@link Arrays#asList(Object[])}, making
1054090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * this method unnecessary.
1055090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1056090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Arrays#asList(Object[])}, {@link ImmutableList#copyOf(Object[])}},
10581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@link ImmutableList#of}.
1059090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1060090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> forArray(final T... array) {
10611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // TODO(kevinb): compare performance with Arrays.asList(array).iterator().
10627dd252788645e940eada959bdde927426e2531c9Paul Duffin    checkNotNull(array); // eager for GWT.
10631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new AbstractIndexedListIterator<T>(array.length) {
10647dd252788645e940eada959bdde927426e2531c9Paul Duffin      @Override
10657dd252788645e940eada959bdde927426e2531c9Paul Duffin      protected T get(int index) {
10661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return array[index];
1067090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1068090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1069090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1070090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1071090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
10727dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns a list iterator containing the elements in the specified range of
10737dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code array} in order, starting at the specified index.
1074090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1075090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is {@code
10767dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Arrays.asList(array).subList(offset, offset + length).listIterator(index)}.
1077090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
10787dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> UnmodifiableListIterator<T> forArray(final T[] array, final int offset, int length,
10797dd252788645e940eada959bdde927426e2531c9Paul Duffin      int index) {
1080090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(length >= 0);
10811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int end = offset + length;
1082090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1083090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // Technically we should give a slightly more descriptive error on overflow
1084090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Preconditions.checkPositionIndexes(offset, end, array.length);
1085090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
10861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /*
10871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * We can't use call the two-arg constructor with arguments (offset, end)
10881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * because the returned Iterator is a ListIterator that may be moved back
10891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * past the beginning of the iteration.
10901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
10917dd252788645e940eada959bdde927426e2531c9Paul Duffin    return new AbstractIndexedListIterator<T>(length, index) {
10927dd252788645e940eada959bdde927426e2531c9Paul Duffin      @Override
10937dd252788645e940eada959bdde927426e2531c9Paul Duffin      protected T get(int index) {
10941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return array[offset + index];
1095090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1096090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1097090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1098090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1099090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing only {@code value}.
1101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
1103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#singleton}.
1104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
11057dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> singletonIterator(@Nullable final T value) {
1106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      boolean done;
11087dd252788645e940eada959bdde927426e2531c9Paul Duffin
1109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return !done;
1111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11127dd252788645e940eada959bdde927426e2531c9Paul Duffin
1113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (done) {
1115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
1116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
1117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        done = true;
1118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return value;
1119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Enumeration} to the {@code Iterator} interface.
1125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>This method has no equivalent in {@link Iterables} because viewing an
1127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Enumeration} as an {@code Iterable} is impossible. However, the
1128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contents can be <i>copied</i> into a collection using {@link
1129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#list}.
1130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
11317dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> forEnumeration(final Enumeration<T> enumeration) {
1132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(enumeration);
1133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
11347dd252788645e940eada959bdde927426e2531c9Paul Duffin
1135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.hasMoreElements();
1137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11387dd252788645e940eada959bdde927426e2531c9Paul Duffin
1139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.nextElement();
1141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Iterator} to the {@code Enumeration} interface.
1147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
1149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#enumeration} (if you have a {@link Collection}), or
1150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Iterators.asEnumeration(collection.iterator())}.
1151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Enumeration<T> asEnumeration(final Iterator<T> iterator) {
1153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Enumeration<T>() {
11557dd252788645e940eada959bdde927426e2531c9Paul Duffin
1156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasMoreElements() {
1157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
1158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11597dd252788645e940eada959bdde927426e2531c9Paul Duffin
1160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T nextElement() {
1161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Implementation of PeekingIterator that avoids peeking unless necessary.
1168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class PeekingImpl<E> implements PeekingIterator<E> {
1170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Iterator<? extends E> iterator;
1172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private boolean hasPeeked;
1173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private E peekedElement;
1174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public PeekingImpl(Iterator<? extends E> iterator) {
1176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.iterator = checkNotNull(iterator);
1177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean hasNext() {
1180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return hasPeeked || iterator.hasNext();
1181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E next() {
1184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      E result = peekedElement;
1188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      hasPeeked = false;
1189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      peekedElement = null;
1190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
1191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public void remove() {
1194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkState(!hasPeeked, "Can't remove after you've peeked at next");
1195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.remove();
1196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E peek() {
1199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        peekedElement = iterator.next();
1201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        hasPeeked = true;
1202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peekedElement;
1204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a {@code PeekingIterator} backed by the given iterator.
1209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Calls to the {@code peek} method with no intervening calls to {@code
1211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * next} do not affect the iteration, and hence return the same object each
1212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * time. A subsequent call to {@code next} is guaranteed to return the same
1213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * object again. For example: <pre>   {@code
1214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   PeekingIterator<String> peekingIterator =
1216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       Iterators.peekingIterator(Iterators.forArray("a", "b"));
1217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a1 = peekingIterator.peek(); // returns "a"
1218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a2 = peekingIterator.peek(); // also returns "a"
1219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a3 = peekingIterator.next(); // also returns "a"}</pre>
1220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Any structural changes to the underlying iteration (aside from those
1222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * performed by the iterator's own {@link PeekingIterator#remove()} method)
1223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will leave the iterator in an undefined state.
1224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator does not support removal after peeking, as
1226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * explained by {@link PeekingIterator#remove()}.
1227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note: If the given iterator is already a {@code PeekingIterator},
1229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it <i>might</i> be returned to the caller, although this is neither
1230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * guaranteed to occur nor required to be consistent.  For example, this
1231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method <i>might</i> choose to pass through recognized implementations of
1232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code PeekingIterator} when the behavior of the implementation is
1233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * known to meet the contract guaranteed by this method.
1234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>There is no {@link Iterable} equivalent to this method, so use this
1236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method to wrap each individual iterator as it is generated.
1237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the backing iterator. The {@link PeekingIterator} assumes
1239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     ownership of this iterator, so users should cease making direct calls
1240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     to it after calling this method.
1241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a peeking iterator backed by that iterator. Apart from the
1242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     additional {@link PeekingIterator#peek()} method, this iterator behaves
1243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     exactly the same as {@code iterator}.
1244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
12457dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> PeekingIterator<T> peekingIterator(Iterator<? extends T> iterator) {
1246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator instanceof PeekingImpl) {
1247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // Safe to cast <? extends T> to <T> because PeekingImpl only uses T
1248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // covariantly (and cannot be subclassed to add non-covariant uses).
1249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked")
1250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      PeekingImpl<T> peeking = (PeekingImpl<T>) iterator;
1251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peeking;
1252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new PeekingImpl<T>(iterator);
1254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
12551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
12581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
12601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
12611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12627dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Deprecated
12637dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> PeekingIterator<T> peekingIterator(PeekingIterator<T> iterator) {
12641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
12651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an iterator over the merged contents of all given
12691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code iterators}, traversing every element of the input iterators.
12701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Equivalent entries will not be de-duplicated.
12711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Callers must ensure that the source {@code iterators} are in
12731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * non-descending order as this method does not sort its input.
12741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For any equivalent elements across all {@code iterators}, it is
12761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * undefined which element is returned first.
12771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
12791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
12811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> UnmodifiableIterator<T> mergeSorted(
12827dd252788645e940eada959bdde927426e2531c9Paul Duffin      Iterable<? extends Iterator<? extends T>> iterators, Comparator<? super T> comparator) {
12831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterators, "iterators");
12841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(comparator, "comparator");
12851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new MergingIterator<T>(iterators, comparator);
12871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An iterator that performs a lazy N-way merge, calculating the next value
12911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each time the iterator is polled. This amortizes the sorting cost over the
12921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iteration and requires less memory than sorting all elements at once.
12931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Retrieving a single element takes approximately O(log(M)) time, where M
12951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is the number of iterators. (Retrieving all elements takes approximately
12961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * O(N*log(M)) time, where N is the total number of elements.)
12971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private static class MergingIterator<T> extends AbstractIterator<T> {
12991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Queue<PeekingIterator<T>> queue;
13001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Comparator<? super T> comparator;
13011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public MergingIterator(Iterable<? extends Iterator<? extends T>> iterators,
13031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        Comparator<? super T> itemComparator) {
13041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      this.comparator = itemComparator;
13051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // A comparator that's used by the heap, allowing the heap
13071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // to be sorted based on the top of each iterator.
13087dd252788645e940eada959bdde927426e2531c9Paul Duffin      Comparator<PeekingIterator<T>> heapComparator = new Comparator<PeekingIterator<T>>() {
13097dd252788645e940eada959bdde927426e2531c9Paul Duffin
13107dd252788645e940eada959bdde927426e2531c9Paul Duffin        public int compare(PeekingIterator<T> o1, PeekingIterator<T> o2) {
13117dd252788645e940eada959bdde927426e2531c9Paul Duffin          return comparator.compare(o1.peek(), o2.peek());
13127dd252788645e940eada959bdde927426e2531c9Paul Duffin        }
13137dd252788645e940eada959bdde927426e2531c9Paul Duffin      };
13141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      queue = new PriorityQueue<PeekingIterator<T>>(2, heapComparator);
13161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      for (Iterator<? extends T> iterator : iterators) {
13181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (iterator.hasNext()) {
13191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          queue.add(Iterators.peekingIterator(iterator));
13201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
13211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
13231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
13251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    protected T computeNext() {
13261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (queue.isEmpty()) {
13271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return endOfData();
13281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      PeekingIterator<T> nextIter = queue.poll();
13311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      T next = nextIter.next();
13321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (nextIter.hasNext()) {
13341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        queue.add(nextIter);
13351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return next;
13381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
13391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
13407dd252788645e940eada959bdde927426e2531c9Paul Duffin
13417dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
13427dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Precondition tester for {@code Iterator.remove()} that throws an exception with a consistent
13437dd252788645e940eada959bdde927426e2531c9Paul Duffin   * error message.
13447dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
13457dd252788645e940eada959bdde927426e2531c9Paul Duffin  static void checkRemove(boolean canRemove) {
13467dd252788645e940eada959bdde927426e2531c9Paul Duffin    checkState(canRemove, "no calls to next() since the last call to remove()");
13477dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
13487dd252788645e940eada959bdde927426e2531c9Paul Duffin
13497dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
13507dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
13517dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
13527dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> ListIterator<T> cast(Iterator<T> iterator) {
13537dd252788645e940eada959bdde927426e2531c9Paul Duffin    return (ListIterator<T>) iterator;
13547dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
1355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
1356