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;
220888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Predicates.equalTo;
230888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Predicates.in;
240888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Predicates.instanceOf;
250888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Predicates.not;
260888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.collect.CollectPreconditions.checkRemove;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtIncompatible;
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function;
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects;
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional;
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions;
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate;
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays;
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections;
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator;
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Enumeration;
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
447dd252788645e940eada959bdde927426e2531c9Paul Duffinimport java.util.ListIterator;
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException;
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.PriorityQueue;
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue;
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class contains static utility methods that operate on or return objects
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of type {@link Iterator}. Except as noted, each method has a corresponding
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterable}-based method in the {@link Iterables} class.
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterators
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that they only advance
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the backing iteration when absolutely necessary.
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
607dd252788645e940eada959bdde927426e2531c9Paul Duffin * <p>See the Guava User Guide section on <a href=
617dd252788645e940eada959bdde927426e2531c9Paul Duffin * "http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained#Iterables">
627dd252788645e940eada959bdde927426e2531c9Paul Duffin * {@code Iterators}</a>.
637dd252788645e940eada959bdde927426e2531c9Paul Duffin *
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true)
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Iterators {
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Iterators() {}
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
720888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static final UnmodifiableListIterator<Object> EMPTY_LIST_ITERATOR
730888a09821a98ac0680fad765217302858e70fa4Paul Duffin      = new UnmodifiableListIterator<Object>() {
740888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public boolean hasNext() {
760888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return false;
770888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
780888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public Object next() {
800888a09821a98ac0680fad765217302858e70fa4Paul Duffin          throw new NoSuchElementException();
810888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
820888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
830888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public boolean hasPrevious() {
840888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return false;
850888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
860888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
870888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public Object previous() {
880888a09821a98ac0680fad765217302858e70fa4Paul Duffin          throw new NoSuchElementException();
890888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
900888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
910888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public int nextIndex() {
920888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return 0;
930888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
940888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
950888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public int previousIndex() {
960888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return -1;
970888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
980888a09821a98ac0680fad765217302858e70fa4Paul Duffin      };
997dd252788645e940eada959bdde927426e2531c9Paul Duffin
1007dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
1017dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns the empty iterator.
1027dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
1037dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>The {@link Iterable} equivalent of this method is {@link
1047dd252788645e940eada959bdde927426e2531c9Paul Duffin   * ImmutableSet#of()}.
1053ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffin   *
1063ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffin   * @deprecated Use {@code ImmutableSet.<T>of().iterator()} instead; or for
1073ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffin   *     Java 7 or later, {@link Collections#emptyIterator}. This method is
1083ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffin   *     scheduled for removal in May 2016.
1097dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
1103ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffin  @Deprecated
1117dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> UnmodifiableIterator<T> emptyIterator() {
1127dd252788645e940eada959bdde927426e2531c9Paul Duffin    return emptyListIterator();
1137dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty iterator.
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * ImmutableSet#of()}.
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
1237dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> UnmodifiableListIterator<T> emptyListIterator() {
1247dd252788645e940eada959bdde927426e2531c9Paul Duffin    return (UnmodifiableListIterator<T>) EMPTY_LIST_ITERATOR;
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1270888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static final Iterator<Object> EMPTY_MODIFIABLE_ITERATOR =
1280888a09821a98ac0680fad765217302858e70fa4Paul Duffin      new Iterator<Object>() {
1290888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override public boolean hasNext() {
1300888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return false;
1310888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1330888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override public Object next() {
1340888a09821a98ac0680fad765217302858e70fa4Paul Duffin          throw new NoSuchElementException();
1350888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1370888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override public void remove() {
1380888a09821a98ac0680fad765217302858e70fa4Paul Duffin          checkRemove(false);
1390888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
1400888a09821a98ac0680fad765217302858e70fa4Paul Duffin      };
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the empty {@code Iterator} that throws
144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link IllegalStateException} instead of
145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link UnsupportedOperationException} on a call to
146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@link Iterator#remove()}.
147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Casting to any type is safe since there are no actual elements.
149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @SuppressWarnings("unchecked")
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> Iterator<T> emptyModifiableIterator() {
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return (Iterator<T>) EMPTY_MODIFIABLE_ITERATOR;
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /** Returns an unmodifiable view of {@code iterator}. */
1550888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<T> unmodifiableIterator(
1560888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterator<T> iterator) {
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (iterator instanceof UnmodifiableIterator) {
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return (UnmodifiableIterator<T>) iterator;
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1620888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1660888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1790888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Deprecated public static <T> UnmodifiableIterator<T> unmodifiableIterator(
1800888a09821a98ac0680fad765217302858e70fa4Paul Duffin      UnmodifiableIterator<T> iterator) {
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements remaining in {@code iterator}. The iterator
186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int size(Iterator<?> iterator) {
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int count = 0;
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.next();
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      count++;
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return count;
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if {@code iterator} contains {@code element}.
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2017dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static boolean contains(Iterator<?> iterator, @Nullable Object element) {
2020888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return any(iterator, equalTo(element));
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that belongs to the
207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRemove the elements to remove
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2140888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean removeAll(
2150888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<?> removeFrom, Collection<?> elementsToRemove) {
2160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return removeIf(removeFrom, in(elementsToRemove));
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Removes every element that satisfies the provided predicate from the
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterator. The iterator will be left exhausted: its {@code hasNext()}
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method will return {@code false}.
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param predicate a predicate that determines whether an element should
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     be removed
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if any elements were removed from the iterator
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2300888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean removeIf(
2310888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> removeFrom, Predicate<? super T> predicate) {
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean modified = false;
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (removeFrom.hasNext()) {
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (predicate.apply(removeFrom.next())) {
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        modified = true;
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return modified;
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Traverses an iterator and removes every element that does not belong to the
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * provided collection. The iterator will be left exhausted: its
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param removeFrom the iterator to (potentially) remove elements from
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param elementsToRetain the elements to retain
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return {@code true} if any element was removed from {@code iterator}
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2520888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean retainAll(
2530888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<?> removeFrom, Collection<?> elementsToRetain) {
2540888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return removeIf(removeFrom, not(in(elementsToRetain)));
255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Determines whether two iterators contain equal elements in the same order.
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * More specifically, this method returns {@code true} if {@code iterator1}
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * and {@code iterator2} contain the same number of elements and every element
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * of {@code iterator1} is equal to the corresponding element of
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code iterator2}.
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note that this will modify the supplied iterators, since they will have
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * been advanced some number of elements forward.
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
2670888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean elementsEqual(
2680888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<?> iterator1, Iterator<?> iterator2) {
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator1.hasNext()) {
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator2.hasNext()) {
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o1 = iterator1.next();
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Object o2 = iterator2.next();
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!Objects.equal(o1, o2)) {
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return !iterator2.hasNext();
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a string representation of {@code iterator}, with the format
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [e1, e2, ..., en]}. The iterator will be left exhausted: its
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static String toString(Iterator<?> iterator) {
2880888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Collections2.STANDARD_JOINER
2890888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .appendTo(new StringBuilder().append('['), iterator)
2900888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .append(']')
2910888a09821a98ac0680fad765217302858e70fa4Paul Duffin        .toString();
292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}.
296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if the iterator is empty
298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getOnlyElement(Iterator<T> iterator) {
302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    T first = iterator.next();
303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (!iterator.hasNext()) {
304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return first;
305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    StringBuilder sb = new StringBuilder();
308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    sb.append("expected one element but was: <" + first);
309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (int i = 0; i < 4 && iterator.hasNext(); i++) {
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", " + iterator.next());
311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator.hasNext()) {
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      sb.append(", ...");
314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    sb.append('>');
316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    throw new IllegalArgumentException(sb.toString());
318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the single element contained in {@code iterator}, or {@code
322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * defaultValue} if the iterator is empty.
323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if the iterator contains multiple
325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     elements.  The state of the iterator is unspecified.
326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3277dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
3287dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getOnlyElement(Iterator<? extends T> iterator, @Nullable T defaultValue) {
329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue;
330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Copies an iterator's elements into an array. The iterator will be left
334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * exhausted: its {@code hasNext()} method will return {@code false}.
335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to copy
337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param type the type of the elements
338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a newly-allocated array into which all the elements of the iterator
339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *         have been copied
340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @GwtIncompatible("Array.newInstance(Class, int)")
3420888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T[] toArray(
3430888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> iterator, Class<T> type) {
344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    List<T> list = Lists.newArrayList(iterator);
345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return Iterables.toArray(list, type);
346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adds all elements in {@code iterator} to {@code collection}. The iterator
350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be left exhausted: its {@code hasNext()} method will return
351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code false}.
352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return {@code true} if {@code collection} was modified as a result of this
354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *         operation
355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
3560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean addAll(
3570888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<T> addTo, Iterator<? extends T> iterator) {
358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(addTo);
3590888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterator);
360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    boolean wasModified = false;
361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      wasModified |= addTo.add(iterator.next());
363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return wasModified;
365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the number of elements in the specified iterator that equal the
369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * specified object. The iterator will be left exhausted: its
370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method will return {@code false}.
371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @see Collections#frequency
373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static int frequency(Iterator<?> iterator, @Nullable Object element) {
3750888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return size(filter(iterator, equalTo(element)));
376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the elements of {@code
380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterable}.
381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After {@code remove()} is called, subsequent cycles omit the removed
384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, which is no longer in {@code iterable}. The iterator's
385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until {@code iterable} is
386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * empty.
387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterable);
394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> iterator = emptyIterator();
396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<T> removeFrom;
397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
3980888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!iterator.hasNext()) {
401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          iterator = iterable.iterator();
402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
4050888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = iterator;
411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
4130888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
4150888a09821a98ac0680fad765217302858e70fa4Paul Duffin        checkRemove(removeFrom != null);
416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that cycles indefinitely over the provided elements.
424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
4257dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p>The returned iterator supports {@code remove()}. After {@code remove()}
4267dd252788645e940eada959bdde927426e2531c9Paul Duffin   * is called, subsequent cycles omit the removed
427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * element, but {@code elements} does not change. The iterator's
428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code hasNext()} method returns {@code true} until all of the original
429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * elements have been removed.
430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * infinite loop. You should use an explicit {@code break} or be certain that
433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * you will eventually remove all the elements.
434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> cycle(T... elements) {
436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return cycle(Lists.newArrayList(elements));
437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines two iterators into a single iterator. The returned iterator
441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}. The source iterators are not polled until necessary.
443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
4467dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
4477dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
4487dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
4497dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
4507dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
4520888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a,
4530888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> b) {
4540888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b).iterator());
455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines three iterators into a single iterator. The returned iterator
459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}. The source iterators
461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * are not polled until necessary.
462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
4657dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
4667dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
4677dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
4687dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
4697dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
4710888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a,
4720888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> b, Iterator<? extends T> c) {
4730888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b, c).iterator());
474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines four iterators into a single iterator. The returned iterator
478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements in {@code a}, followed by the elements in
479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code b}, followed by the elements in {@code c}, followed by the elements
480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * in {@code d}. The source iterators are not polled until necessary.
481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
4847dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
4857dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
4867dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
4877dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
4887dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
489090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
4900888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterator<T> concat(Iterator<? extends T> a,
4910888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> b, Iterator<? extends T> c,
4920888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> d) {
4930888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b, c, d).iterator());
494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it.
503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
5047dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
5057dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
5067dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
5077dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
5087dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NullPointerException if any of the provided iterators is null
510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Iterator<T> concat(Iterator<? extends T>... inputs) {
5121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return concat(ImmutableList.copyOf(inputs).iterator());
513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Combines multiple iterators into a single iterator. The returned iterator
517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterates across the elements of each iterator in {@code inputs}. The input
518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterators are not polled until necessary.
519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} when the corresponding
521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * input iterator supports it. The methods of the returned iterator may throw
5221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code NullPointerException} if any of the input iterators is null.
5237dd252788645e940eada959bdde927426e2531c9Paul Duffin   *
5247dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <p><b>Note:</b> the current implementation is not suitable for nested
5257dd252788645e940eada959bdde927426e2531c9Paul Duffin   * concatenated iterators, i.e. the following should be avoided when in a loop:
5267dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code iterator = Iterators.concat(iterator, suffix);}, since iteration over the
5277dd252788645e940eada959bdde927426e2531c9Paul Duffin   * resulting iterator has a cubic complexity to the depth of the nesting.
528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
5290888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterator<T> concat(
5300888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterator<? extends Iterator<? extends T>> inputs) {
531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(inputs);
532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Iterator<T>() {
533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> current = emptyIterator();
534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterator<? extends T> removeFrom;
535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
5360888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
538090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // http://code.google.com/p/google-collections/issues/detail?id=151
539090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // current.hasNext() might be relatively expensive, worth minimizing.
540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        boolean currentHasNext;
5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // checkNotNull eager for GWT
5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // note: it must be here & not where 'current' is assigned,
5431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // because otherwise we'll have called inputs.next() before throwing
5441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // the first NPE, and the next time around we'll call inputs.next()
5451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // again, incorrectly moving beyond the error.
5460888a09821a98ac0680fad765217302858e70fa4Paul Duffin        while (!(currentHasNext = checkNotNull(current).hasNext())
5470888a09821a98ac0680fad765217302858e70fa4Paul Duffin            && inputs.hasNext()) {
548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          current = inputs.next();
549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return currentHasNext;
551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5520888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
556090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = current;
558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current.next();
559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
5600888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public void remove() {
5620888a09821a98ac0680fad765217302858e70fa4Paul Duffin        checkRemove(removeFrom != null);
563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom.remove();
564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        removeFrom = null;
565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size (the final
571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * list may be smaller). For example, partitioning an iterator containing
572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code
573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * [[a, b, c], [d, e]]} -- an outer iterator containing two inner lists of
574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * three and two elements, all in the original order.
575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition (the last may be smaller)
580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions
582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
5840888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<List<T>> partition(
5850888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, int size) {
586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, false);
587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Divides an iterator into unmodifiable sublists of the given size, padding
591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * the final iterator with null values if necessary. For example, partitioning
592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * an iterator containing {@code [a, b, c, d, e]} with a partition size of 3
593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterator containing
594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * two inner lists of three elements each, all in the original order.
595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned lists implement {@link java.util.RandomAccess}.
597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the iterator to return a partitioned view of
599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param size the desired size of each partition
600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an iterator of immutable lists containing the elements of {@code
601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator} divided into partitions (the final iterable may have
602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     trailing null elements)
603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IllegalArgumentException if {@code size} is nonpositive
604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6050888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<List<T>> paddedPartition(
6060888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, int size) {
607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return partitionImpl(iterator, size, true);
608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
609090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
6100888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> UnmodifiableIterator<List<T>> partitionImpl(
6110888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterator<T> iterator, final int size, final boolean pad) {
612090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
613090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(size > 0);
614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<List<T>>() {
6150888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
616090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
6190888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public List<T> next() {
621090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!hasNext()) {
622090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
623090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
624090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Object[] array = new Object[size];
625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        int count = 0;
626090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        for (; count < size && iterator.hasNext(); count++) {
627090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          array[count] = iterator.next();
628090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
6291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        for (int i = count; i < size; i++) {
6301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          array[i] = null; // for GWT
6311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
632090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
6330888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @SuppressWarnings("unchecked") // we only put Ts in it
6340888a09821a98ac0680fad765217302858e70fa4Paul Duffin        List<T> list = Collections.unmodifiableList(
6350888a09821a98ac0680fad765217302858e70fa4Paul Duffin            (List<T>) Arrays.asList(array));
6361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return (pad || count == size) ? list : list.subList(0, count);
637090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
638090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
639090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
640090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
641090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
642090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the elements of {@code unfiltered} that satisfy a predicate.
643090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6440888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<T> filter(
6450888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterator<T> unfiltered, final Predicate<? super T> predicate) {
646090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(unfiltered);
647090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
648090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new AbstractIterator<T>() {
6490888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override protected T computeNext() {
650090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        while (unfiltered.hasNext()) {
651090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          T element = unfiltered.next();
652090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          if (predicate.apply(element)) {
653090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson            return element;
654090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          }
655090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
656090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return endOfData();
657090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
658090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
659090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
660090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
661090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
662090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns all instances of class {@code type} in {@code unfiltered}. The
663090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned iterator has elements whose class is {@code type} or a subclass of
664090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code type}.
665090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
666090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param unfiltered an iterator containing objects of any type
667090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param type the type of elements desired
668090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return an unmodifiable iterator containing all elements of the original
669090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     iterator that were of the requested type
670090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6710888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @SuppressWarnings("unchecked") // can cast to <T> because non-Ts are removed
672090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @GwtIncompatible("Class.isInstance")
6730888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<T> filter(
6740888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<?> unfiltered, Class<T> type) {
6750888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (UnmodifiableIterator<T>) filter(unfiltered, instanceOf(type));
676090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
677090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
678090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
679090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if one or more elements returned by {@code iterator}
680090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfy the given predicate.
681090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6820888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean any(
6830888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, Predicate<? super T> predicate) {
6840888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return indexOf(iterator, predicate) != -1;
685090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
686090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
687090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
688090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns {@code true} if every element returned by {@code iterator}
689090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * satisfies the given predicate. If {@code iterator} is empty, {@code true}
690090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * is returned.
691090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
6920888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean all(
6930888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, Predicate<? super T> predicate) {
694090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(predicate);
695090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (iterator.hasNext()) {
696090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T element = iterator.next();
697090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!predicate.apply(element)) {
698090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
699090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
700090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
701090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return true;
702090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
703090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
704090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
705090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the first element in {@code iterator} that satisfies the given
7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate; use this method only when such an element is known to exist. If
7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, the iterator will be left exhausted: its {@code
7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}. If it is possible that
7097dd252788645e940eada959bdde927426e2531c9Paul Duffin   * <i>no</i> element will match, use {@link #tryFind} or {@link
7107dd252788645e940eada959bdde927426e2531c9Paul Duffin   * #find(Iterator, Predicate, Object)} instead.
711090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
712090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws NoSuchElementException if no element in {@code iterator} matches
713090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     the given predicate
714090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
7150888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T find(
7160888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, Predicate<? super T> predicate) {
717090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return filter(iterator, predicate).next();
718090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
719090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
720090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the first element in {@code iterator} that satisfies the given
7221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * predicate. If no such element is found, {@code defaultValue} will be
7231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * returned from this method and the iterator will be left exhausted: its
7241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code hasNext()} method will return {@code false}. Note that this can
7251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * usually be handled more naturally using {@code
7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * tryFind(iterator, predicate).or(defaultValue)}.
7271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
7291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7307dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
7317dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T find(Iterator<? extends T> iterator, Predicate<? super T> predicate,
7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Nullable T defaultValue) {
7330888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return getNext(filter(iterator, predicate), defaultValue);
7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
7371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an {@link Optional} containing the first element in {@code
7381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator} that satisfies the given predicate, if such an element exists. If
7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * no such element is found, an empty {@link Optional} will be returned from
7400888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * this method and the iterator will be left exhausted: its {@code
7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * hasNext()} method will return {@code false}.
7421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code
7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * null}. If {@code null} is matched in {@code iterator}, a
7451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * NullPointerException will be thrown.
7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
7471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
7481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
7490888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Optional<T> tryFind(
7500888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, Predicate<? super T> predicate) {
7511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate);
7520888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return filteredIterator.hasNext()
7530888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? Optional.of(filteredIterator.next())
7540888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Optional.<T>absent();
7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
758bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns the index in {@code iterator} of the first element that satisfies
759bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the provided {@code predicate}, or {@code -1} if the Iterator has no such
760bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * elements.
761bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
762bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>More formally, returns the lowest index {@code i} such that
7631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code predicate.apply(Iterators.get(iterator, i))} returns {@code true},
7641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@code -1} if there is no such index.
765bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
766bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>If -1 is returned, the iterator will be left exhausted: its
767bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code hasNext()} method will return {@code false}.  Otherwise,
768bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * the iterator will be set to the element which satisfies the
769bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * {@code predicate}.
770bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
7711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
772bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
7730888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> int indexOf(
7740888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<T> iterator, Predicate<? super T> predicate) {
7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(predicate, "predicate");
7760888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (int i = 0; iterator.hasNext(); i++) {
777bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      T current = iterator.next();
778bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      if (predicate.apply(current)) {
779bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return i;
780bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
781bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    }
782bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return -1;
783bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
784bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
785bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
786090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator that applies {@code function} to each element of {@code
787090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * fromIterator}.
788090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
789090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator supports {@code remove()} if the provided iterator
790090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * does. After a successful {@code remove()} call, {@code fromIterator} no
791090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * longer contains the corresponding element.
792090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
793090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator,
794090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      final Function<? super F, ? extends T> function) {
795090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(function);
7967dd252788645e940eada959bdde927426e2531c9Paul Duffin    return new TransformedIterator<F, T>(fromIterator) {
7971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      @Override
7987dd252788645e940eada959bdde927426e2531c9Paul Duffin      T transform(F from) {
799090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return function.apply(from);
800090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
801090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
802090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
803090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
804090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
8061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position.
807090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
808090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param position position of the element to return
809090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the element at the specified position in {@code iterator}
810090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @throws IndexOutOfBoundsException if {@code position} is negative or
811090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     greater than or equal to the number of elements remaining in
812090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     {@code iterator}
813090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
814090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T get(Iterator<T> iterator, int position) {
8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
8160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    int skipped = advance(iterator, position);
8170888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (!iterator.hasNext()) {
8180888a09821a98ac0680fad765217302858e70fa4Paul Duffin      throw new IndexOutOfBoundsException("position (" + position
8190888a09821a98ac0680fad765217302858e70fa4Paul Duffin          + ") must be less than the number of elements that remained ("
8200888a09821a98ac0680fad765217302858e70fa4Paul Duffin          + skipped + ")");
821090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
8220888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return iterator.next();
823090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
824090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
8250888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static void checkNonnegative(int position) {
8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (position < 0) {
8270888a09821a98ac0680fad765217302858e70fa4Paul Duffin      throw new IndexOutOfBoundsException("position (" + position
8280888a09821a98ac0680fad765217302858e70fa4Paul Duffin          + ") must not be negative");
8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} {@code position + 1} times, returning the
8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * element at the {@code position}th position or {@code defaultValue}
8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * otherwise.
8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param position position of the element to return
8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     or if {@code position} is greater than the number of elements
8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     remaining in {@code iterator}
8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the element at the specified position in {@code iterator} or
8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code defaultValue} if {@code iterator} produces fewer than
8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *     {@code position + 1} elements.
8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IndexOutOfBoundsException if {@code position} is negative
8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 4.0
8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8477dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
8487dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T get(Iterator<? extends T> iterator, int position, @Nullable T defaultValue) {
8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNonnegative(position);
8500888a09821a98ac0680fad765217302858e70fa4Paul Duffin    advance(iterator, position);
8510888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return getNext(iterator, defaultValue);
8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the next element in {@code iterator} or {@code defaultValue} if
8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the iterator is empty.  The {@link Iterables} analog to this method is
8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Iterables#getFirst}.
8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the next element of {@code iterator} or the default value
8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 7.0
8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8637dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
8647dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getNext(Iterator<? extends T> iterator, @Nullable T defaultValue) {
8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? iterator.next() : defaultValue;
8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
868090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
869090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Advances {@code iterator} to the end, returning the last element.
870090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
871090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return the last element of {@code iterator}
8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws NoSuchElementException if the iterator is empty
873090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
874090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> T getLast(Iterator<T> iterator) {
875090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    while (true) {
876090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      T current = iterator.next();
877090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!iterator.hasNext()) {
878090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return current;
879090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
880090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
881090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
882090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
883bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Advances {@code iterator} to the end, returning the last element or
8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code defaultValue} if the iterator is empty.
8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param defaultValue the default value to return if the iterator is empty
8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @return the last element of {@code iterator}
8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
8917dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
8927dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static <T> T getLast(Iterator<? extends T> iterator, @Nullable T defaultValue) {
8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return iterator.hasNext() ? getLast(iterator) : defaultValue;
8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
8977dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Calls {@code next()} on {@code iterator}, either {@code numberToAdvance} times
8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or until {@code hasNext()} returns {@code false}, whichever comes first.
8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9007dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @return the number of elements the iterator was advanced
9017dd252788645e940eada959bdde927426e2531c9Paul Duffin   * @since 13.0 (since 3.0 as {@code Iterators.skip})
9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9037dd252788645e940eada959bdde927426e2531c9Paul Duffin  public static int advance(Iterator<?> iterator, int numberToAdvance) {
9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9050888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkArgument(numberToAdvance >= 0, "numberToAdvance must be nonnegative");
9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int i;
9087dd252788645e940eada959bdde927426e2531c9Paul Duffin    for (i = 0; i < numberToAdvance && iterator.hasNext(); i++) {
9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return i;
9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates an iterator returning the first {@code limitSize} elements of the
9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * given iterator. If the original iterator does not contain that many
9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * elements, the returned iterator will have the same behavior as the original
9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator. The returned iterator supports {@code remove()} if the original
9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iterator does.
9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param iterator the iterator to limit
9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param limitSize the maximum number of elements in the returned iterator
9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @throws IllegalArgumentException if {@code limitSize} is negative
9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 3.0
9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
9260888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterator<T> limit(
9270888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterator<T> iterator, final int limitSize) {
9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(limitSize >= 0, "limit is negative");
9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new Iterator<T>() {
9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      private int count;
9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9330888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public boolean hasNext() {
9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return count < limitSize && iterator.hasNext();
9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9380888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public T next() {
9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (!hasNext()) {
9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          throw new NoSuchElementException();
9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        count++;
9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return iterator.next();
9451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9470888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      public void remove() {
9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        iterator.remove();
9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
9511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    };
9521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
9531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
955bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Returns a view of the supplied {@code iterator} that removes each element
956bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * from the supplied {@code iterator} as it is returned.
957bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
958bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * <p>The provided iterator must support {@link Iterator#remove()} or
959bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * else the returned iterator will fail on the first call to {@code
960bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * next}.
961bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *
962bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @param iterator the iterator to remove and return elements from
963bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * @return an iterator that removes and returns elements from the
964bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   *     supplied iterator
9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 2.0
966bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
967bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  public static <T> Iterator<T> consumingIterator(final Iterator<T> iterator) {
968bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    checkNotNull(iterator);
969bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return new UnmodifiableIterator<T>() {
9700888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
971bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public boolean hasNext() {
972bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return iterator.hasNext();
973bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
974bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
9750888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
976bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      public T next() {
977bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        T next = iterator.next();
978bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        iterator.remove();
979bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor        return next;
980bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      }
9810888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9820888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9830888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public String toString() {
9840888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return "Iterators.consumingIterator(...)";
9850888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
986bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    };
987bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
988bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
9897dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
9907dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Deletes and returns the next value from the iterator, or returns
9910888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code null} if there is no such value.
9927dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
9937dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Nullable
9947dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> T pollNext(Iterator<T> iterator) {
9957dd252788645e940eada959bdde927426e2531c9Paul Duffin    if (iterator.hasNext()) {
9967dd252788645e940eada959bdde927426e2531c9Paul Duffin      T result = iterator.next();
9977dd252788645e940eada959bdde927426e2531c9Paul Duffin      iterator.remove();
9987dd252788645e940eada959bdde927426e2531c9Paul Duffin      return result;
9997dd252788645e940eada959bdde927426e2531c9Paul Duffin    } else {
10007dd252788645e940eada959bdde927426e2531c9Paul Duffin      return null;
10017dd252788645e940eada959bdde927426e2531c9Paul Duffin    }
10027dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
10037dd252788645e940eada959bdde927426e2531c9Paul Duffin
1004090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  // Methods only in Iterators, not in Iterables
1005090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1006090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
10071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Clears the iterator using its remove method.
10081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
10091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static void clear(Iterator<?> iterator) {
10101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterator);
10111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    while (iterator.hasNext()) {
10121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.next();
10131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      iterator.remove();
10141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
10151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
10161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
10171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
1018090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing the elements of {@code array} in order. The
1019090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned iterator is a view of the array; subsequent changes to the array
1020090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will be reflected in the iterator.
1021090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1022090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p><b>Note:</b> It is often preferable to represent your data using a
1023090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * collection type, for example using {@link Arrays#asList(Object[])}, making
1024090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * this method unnecessary.
1025090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1026090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
10271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Arrays#asList(Object[])}, {@link ImmutableList#copyOf(Object[])}},
10281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * or {@link ImmutableList#of}.
1029090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1030090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> UnmodifiableIterator<T> forArray(final T... array) {
10310888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return forArray(array, 0, array.length, 0);
1032090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1033090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1034090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
10357dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Returns a list iterator containing the elements in the specified range of
10367dd252788645e940eada959bdde927426e2531c9Paul Duffin   * {@code array} in order, starting at the specified index.
1037090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1038090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is {@code
10397dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Arrays.asList(array).subList(offset, offset + length).listIterator(index)}.
1040090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
10410888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <T> UnmodifiableListIterator<T> forArray(
10420888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final T[] array, final int offset, int length, int index) {
1043090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkArgument(length >= 0);
10441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    int end = offset + length;
1045090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1046090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // Technically we should give a slightly more descriptive error on overflow
1047090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Preconditions.checkPositionIndexes(offset, end, array.length);
10480888a09821a98ac0680fad765217302858e70fa4Paul Duffin    Preconditions.checkPositionIndex(index, length);
10490888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (length == 0) {
10500888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return emptyListIterator();
10510888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
1052090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
10531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /*
10541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * We can't use call the two-arg constructor with arguments (offset, end)
10551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * because the returned Iterator is a ListIterator that may be moved back
10561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * past the beginning of the iteration.
10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
10587dd252788645e940eada959bdde927426e2531c9Paul Duffin    return new AbstractIndexedListIterator<T>(length, index) {
10590888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override protected T get(int index) {
10601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return array[offset + index];
1061090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1062090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1063090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1064090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1065090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1066090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns an iterator containing only {@code value}.
1067090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1068090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@link Iterable} equivalent of this method is {@link
1069090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#singleton}.
1070090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
10710888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<T> singletonIterator(
10720888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Nullable final T value) {
1073090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
1074090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      boolean done;
10750888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1076090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1077090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return !done;
1078090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
10790888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1080090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1081090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (done) {
1082090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          throw new NoSuchElementException();
1083090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
1084090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        done = true;
1085090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return value;
1086090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1087090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1088090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1089090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1090090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1091090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Enumeration} to the {@code Iterator} interface.
1092090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1093090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>This method has no equivalent in {@link Iterables} because viewing an
1094090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Enumeration} as an {@code Iterable} is impossible. However, the
1095090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * contents can be <i>copied</i> into a collection using {@link
1096090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#list}.
1097090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
10980888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> UnmodifiableIterator<T> forEnumeration(
10990888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Enumeration<T> enumeration) {
1100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(enumeration);
1101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new UnmodifiableIterator<T>() {
11020888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasNext() {
1104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.hasMoreElements();
1105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11060888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T next() {
1108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return enumeration.nextElement();
1109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Adapts an {@code Iterator} to the {@code Enumeration} interface.
1115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The {@code Iterable} equivalent of this method is either {@link
1117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Collections#enumeration} (if you have a {@link Collection}), or
1118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code Iterators.asEnumeration(collection.iterator())}.
1119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <T> Enumeration<T> asEnumeration(final Iterator<T> iterator) {
1121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    checkNotNull(iterator);
1122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new Enumeration<T>() {
11230888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public boolean hasMoreElements() {
1125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.hasNext();
1126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
11270888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
1128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      public T nextElement() {
1129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    };
1132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Implementation of PeekingIterator that avoids peeking unless necessary.
1136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
1137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static class PeekingImpl<E> implements PeekingIterator<E> {
1138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private final Iterator<? extends E> iterator;
1140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private boolean hasPeeked;
1141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    private E peekedElement;
1142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public PeekingImpl(Iterator<? extends E> iterator) {
1144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.iterator = checkNotNull(iterator);
1145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11470888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
1148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean hasNext() {
1149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return hasPeeked || iterator.hasNext();
1150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11520888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
1153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E next() {
1154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return iterator.next();
1156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      E result = peekedElement;
1158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      hasPeeked = false;
1159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      peekedElement = null;
1160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return result;
1161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11630888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
1164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public void remove() {
1165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkState(!hasPeeked, "Can't remove after you've peeked at next");
1166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      iterator.remove();
1167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
11690888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
1170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public E peek() {
1171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      if (!hasPeeked) {
1172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        peekedElement = iterator.next();
1173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        hasPeeked = true;
1174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
1175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peekedElement;
1176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
1178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
1180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a {@code PeekingIterator} backed by the given iterator.
1181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Calls to the {@code peek} method with no intervening calls to {@code
1183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * next} do not affect the iteration, and hence return the same object each
1184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * time. A subsequent call to {@code next} is guaranteed to return the same
1185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * object again. For example: <pre>   {@code
1186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   PeekingIterator<String> peekingIterator =
1188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *       Iterators.peekingIterator(Iterators.forArray("a", "b"));
1189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a1 = peekingIterator.peek(); // returns "a"
1190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a2 = peekingIterator.peek(); // also returns "a"
1191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *   String a3 = peekingIterator.next(); // also returns "a"}</pre>
1192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
11930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Any structural changes to the underlying iteration (aside from those
1194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * performed by the iterator's own {@link PeekingIterator#remove()} method)
1195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * will leave the iterator in an undefined state.
1196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned iterator does not support removal after peeking, as
1198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * explained by {@link PeekingIterator#remove()}.
1199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Note: If the given iterator is already a {@code PeekingIterator},
1201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it <i>might</i> be returned to the caller, although this is neither
1202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * guaranteed to occur nor required to be consistent.  For example, this
1203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method <i>might</i> choose to pass through recognized implementations of
1204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code PeekingIterator} when the behavior of the implementation is
1205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * known to meet the contract guaranteed by this method.
1206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>There is no {@link Iterable} equivalent to this method, so use this
1208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * method to wrap each individual iterator as it is generated.
1209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
1210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param iterator the backing iterator. The {@link PeekingIterator} assumes
1211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     ownership of this iterator, so users should cease making direct calls
1212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     to it after calling this method.
1213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @return a peeking iterator backed by that iterator. Apart from the
1214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     additional {@link PeekingIterator#peek()} method, this iterator behaves
1215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *     exactly the same as {@code iterator}.
1216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
12170888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> PeekingIterator<T> peekingIterator(
12180888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> iterator) {
1219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (iterator instanceof PeekingImpl) {
1220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // Safe to cast <? extends T> to <T> because PeekingImpl only uses T
1221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // covariantly (and cannot be subclassed to add non-covariant uses).
1222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      @SuppressWarnings("unchecked")
1223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      PeekingImpl<T> peeking = (PeekingImpl<T>) iterator;
1224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return peeking;
1225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
1226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new PeekingImpl<T>(iterator);
1227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
12281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Simply returns its argument.
12311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @deprecated no need to use this
12331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 10.0
12341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12350888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Deprecated public static <T> PeekingIterator<T> peekingIterator(
12360888a09821a98ac0680fad765217302858e70fa4Paul Duffin      PeekingIterator<T> iterator) {
12371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return checkNotNull(iterator);
12381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns an iterator over the merged contents of all given
12421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code iterators}, traversing every element of the input iterators.
12431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Equivalent entries will not be de-duplicated.
12441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Callers must ensure that the source {@code iterators} are in
12461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * non-descending order as this method does not sort its input.
12471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>For any equivalent elements across all {@code iterators}, it is
12491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * undefined which element is returned first.
12501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @since 11.0
12521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Beta
12541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public static <T> UnmodifiableIterator<T> mergeSorted(
12550888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends Iterator<? extends T>> iterators,
12560888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Comparator<? super T> comparator) {
12571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(iterators, "iterators");
12581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(comparator, "comparator");
12591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new MergingIterator<T>(iterators, comparator);
12611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
12621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
12641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An iterator that performs a lazy N-way merge, calculating the next value
12651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each time the iterator is polled. This amortizes the sorting cost over the
12661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * iteration and requires less memory than sorting all elements at once.
12671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
12681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>Retrieving a single element takes approximately O(log(M)) time, where M
12691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * is the number of iterators. (Retrieving all elements takes approximately
12701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * O(N*log(M)) time, where N is the total number of elements.)
12711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
12720888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static class MergingIterator<T> extends UnmodifiableIterator<T> {
12731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    final Queue<PeekingIterator<T>> queue;
12741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    public MergingIterator(Iterable<? extends Iterator<? extends T>> iterators,
12760888a09821a98ac0680fad765217302858e70fa4Paul Duffin        final Comparator<? super T> itemComparator) {
12771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // A comparator that's used by the heap, allowing the heap
12781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      // to be sorted based on the top of each iterator.
12790888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Comparator<PeekingIterator<T>> heapComparator =
12800888a09821a98ac0680fad765217302858e70fa4Paul Duffin          new Comparator<PeekingIterator<T>>() {
12810888a09821a98ac0680fad765217302858e70fa4Paul Duffin            @Override
12820888a09821a98ac0680fad765217302858e70fa4Paul Duffin            public int compare(PeekingIterator<T> o1, PeekingIterator<T> o2) {
12830888a09821a98ac0680fad765217302858e70fa4Paul Duffin              return itemComparator.compare(o1.peek(), o2.peek());
12840888a09821a98ac0680fad765217302858e70fa4Paul Duffin            }
12850888a09821a98ac0680fad765217302858e70fa4Paul Duffin          };
12861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      queue = new PriorityQueue<PeekingIterator<T>>(2, heapComparator);
12881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      for (Iterator<? extends T> iterator : iterators) {
12901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        if (iterator.hasNext()) {
12911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          queue.add(Iterators.peekingIterator(iterator));
12921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }
12931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
12941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
12951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
12961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
12970888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public boolean hasNext() {
12980888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return !queue.isEmpty();
12990888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
13001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
13010888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
13020888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public T next() {
13030888a09821a98ac0680fad765217302858e70fa4Paul Duffin      PeekingIterator<T> nextIter = queue.remove();
13041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      T next = nextIter.next();
13051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (nextIter.hasNext()) {
13061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        queue.add(nextIter);
13071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
13081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return next;
13091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
13101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
13117dd252788645e940eada959bdde927426e2531c9Paul Duffin
13127dd252788645e940eada959bdde927426e2531c9Paul Duffin  /**
13137dd252788645e940eada959bdde927426e2531c9Paul Duffin   * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
13147dd252788645e940eada959bdde927426e2531c9Paul Duffin   */
13157dd252788645e940eada959bdde927426e2531c9Paul Duffin  static <T> ListIterator<T> cast(Iterator<T> iterator) {
13167dd252788645e940eada959bdde927426e2531c9Paul Duffin    return (ListIterator<T>) iterator;
13177dd252788645e940eada959bdde927426e2531c9Paul Duffin  }
1318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
1319