10888a09821a98ac0680fad765217302858e70fa4Paul Duffin/*
20888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Copyright (C) 2007 The Guava Authors
30888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
40888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
50888a09821a98ac0680fad765217302858e70fa4Paul Duffin * you may not use this file except in compliance with the License.
60888a09821a98ac0680fad765217302858e70fa4Paul Duffin * You may obtain a copy of the License at
70888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
80888a09821a98ac0680fad765217302858e70fa4Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0
90888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
100888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Unless required by applicable law or agreed to in writing, software
110888a09821a98ac0680fad765217302858e70fa4Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
120888a09821a98ac0680fad765217302858e70fa4Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130888a09821a98ac0680fad765217302858e70fa4Paul Duffin * See the License for the specific language governing permissions and
140888a09821a98ac0680fad765217302858e70fa4Paul Duffin * limitations under the License.
150888a09821a98ac0680fad765217302858e70fa4Paul Duffin */
160888a09821a98ac0680fad765217302858e70fa4Paul Duffin
170888a09821a98ac0680fad765217302858e70fa4Paul Duffinpackage com.google.common.collect;
180888a09821a98ac0680fad765217302858e70fa4Paul Duffin
190888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Preconditions.checkArgument;
200888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.base.Preconditions.checkNotNull;
210888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport static com.google.common.collect.CollectPreconditions.checkRemove;
220888a09821a98ac0680fad765217302858e70fa4Paul Duffin
230888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.annotations.Beta;
240888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.annotations.GwtCompatible;
250888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Function;
260888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Optional;
270888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Predicate;
280888a09821a98ac0680fad765217302858e70fa4Paul Duffin
290888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Collection;
300888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Comparator;
310888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Iterator;
320888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.List;
330888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.NoSuchElementException;
340888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Queue;
350888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.RandomAccess;
360888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Set;
370888a09821a98ac0680fad765217302858e70fa4Paul Duffin
380888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport javax.annotation.Nullable;
390888a09821a98ac0680fad765217302858e70fa4Paul Duffin
400888a09821a98ac0680fad765217302858e70fa4Paul Duffin/**
410888a09821a98ac0680fad765217302858e70fa4Paul Duffin * This class contains static utility methods that operate on or return objects
420888a09821a98ac0680fad765217302858e70fa4Paul Duffin * of type {@code Iterable}. Except as noted, each method has a corresponding
430888a09821a98ac0680fad765217302858e70fa4Paul Duffin * {@link Iterator}-based method in the {@link Iterators} class.
440888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
450888a09821a98ac0680fad765217302858e70fa4Paul Duffin * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterables
460888a09821a98ac0680fad765217302858e70fa4Paul Duffin * produced in this class are <i>lazy</i>, which means that their iterators
470888a09821a98ac0680fad765217302858e70fa4Paul Duffin * only advance the backing iteration when absolutely necessary.
480888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
490888a09821a98ac0680fad765217302858e70fa4Paul Duffin * <p>See the Guava User Guide article on <a href=
500888a09821a98ac0680fad765217302858e70fa4Paul Duffin * "http://code.google.com/p/guava-libraries/wiki/CollectionUtilitiesExplained#Iterables">
510888a09821a98ac0680fad765217302858e70fa4Paul Duffin * {@code Iterables}</a>.
520888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
530888a09821a98ac0680fad765217302858e70fa4Paul Duffin * @author Kevin Bourrillion
540888a09821a98ac0680fad765217302858e70fa4Paul Duffin * @author Jared Levy
550888a09821a98ac0680fad765217302858e70fa4Paul Duffin * @since 2.0 (imported from Google Collections Library)
560888a09821a98ac0680fad765217302858e70fa4Paul Duffin */
570888a09821a98ac0680fad765217302858e70fa4Paul Duffin@GwtCompatible(emulated = true)
580888a09821a98ac0680fad765217302858e70fa4Paul Duffinpublic final class Iterables {
590888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private Iterables() {}
600888a09821a98ac0680fad765217302858e70fa4Paul Duffin
610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /** Returns an unmodifiable view of {@code iterable}. */
620888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> unmodifiableIterable(
630888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<T> iterable) {
640888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
650888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof UnmodifiableIterable ||
660888a09821a98ac0680fad765217302858e70fa4Paul Duffin        iterable instanceof ImmutableCollection) {
670888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return iterable;
680888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
690888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new UnmodifiableIterable<T>(iterable);
700888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
710888a09821a98ac0680fad765217302858e70fa4Paul Duffin
720888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
730888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Simply returns its argument.
740888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @deprecated no need to use this
760888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 10.0
770888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
780888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Deprecated public static <E> Iterable<E> unmodifiableIterable(
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin      ImmutableCollection<E> iterable) {
800888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return checkNotNull(iterable);
810888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
820888a09821a98ac0680fad765217302858e70fa4Paul Duffin
830888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static final class UnmodifiableIterable<T> extends FluentIterable<T> {
840888a09821a98ac0680fad765217302858e70fa4Paul Duffin    private final Iterable<T> iterable;
850888a09821a98ac0680fad765217302858e70fa4Paul Duffin
860888a09821a98ac0680fad765217302858e70fa4Paul Duffin    private UnmodifiableIterable(Iterable<T> iterable) {
870888a09821a98ac0680fad765217302858e70fa4Paul Duffin      this.iterable = iterable;
880888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
890888a09821a98ac0680fad765217302858e70fa4Paul Duffin
900888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
910888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public Iterator<T> iterator() {
920888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return Iterators.unmodifiableIterator(iterable.iterator());
930888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
940888a09821a98ac0680fad765217302858e70fa4Paul Duffin
950888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
960888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public String toString() {
970888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return iterable.toString();
980888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
990888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // no equals and hashCode; it would break the contract!
1000888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1010888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1020888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
1030888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the number of elements in {@code iterable}.
1040888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
1050888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static int size(Iterable<?> iterable) {
1060888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (iterable instanceof Collection)
1070888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? ((Collection<?>) iterable).size()
1080888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Iterators.size(iterable.iterator());
1090888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1100888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1110888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
1120888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns {@code true} if {@code iterable} contains any object for which {@code equals(element)}
1130888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * is true.
1140888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
1150888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean contains(Iterable<?> iterable, @Nullable Object element) {
1160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof Collection) {
1170888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<?> collection = (Collection<?>) iterable;
1180888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return Collections2.safeContains(collection, element);
1190888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
1200888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.contains(iterable.iterator(), element);
1210888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1220888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1230888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
1240888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Removes, from an iterable, every element that belongs to the provided
1250888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * collection.
1260888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1270888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>This method calls {@link Collection#removeAll} if {@code iterable} is a
1280888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * collection, and {@link Iterators#removeAll} otherwise.
1290888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1300888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param removeFrom the iterable to (potentially) remove elements from
1310888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param elementsToRemove the elements to remove
1320888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return {@code true} if any element was removed from {@code iterable}
1330888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
1340888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean removeAll(
1350888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<?> removeFrom, Collection<?> elementsToRemove) {
1360888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (removeFrom instanceof Collection)
1370888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? ((Collection<?>) removeFrom).removeAll(checkNotNull(elementsToRemove))
1380888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Iterators.removeAll(removeFrom.iterator(), elementsToRemove);
1390888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1400888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1410888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
1420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Removes, from an iterable, every element that does not belong to the
1430888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * provided collection.
1440888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1450888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>This method calls {@link Collection#retainAll} if {@code iterable} is a
1460888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * collection, and {@link Iterators#retainAll} otherwise.
1470888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1480888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param removeFrom the iterable to (potentially) remove elements from
1490888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param elementsToRetain the elements to retain
1500888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return {@code true} if any element was removed from {@code iterable}
1510888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
1520888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean retainAll(
1530888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<?> removeFrom, Collection<?> elementsToRetain) {
1540888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (removeFrom instanceof Collection)
1550888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? ((Collection<?>) removeFrom).retainAll(checkNotNull(elementsToRetain))
1560888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Iterators.retainAll(removeFrom.iterator(), elementsToRetain);
1570888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1580888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1590888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
1600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Removes, from an iterable, every element that satisfies the provided
1610888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * predicate.
1620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param removeFrom the iterable to (potentially) remove elements from
1640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param predicate a predicate that determines whether an element should
1650888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     be removed
1660888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return {@code true} if any elements were removed from the iterable
1670888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
1680888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws UnsupportedOperationException if the iterable does not support
1690888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     {@code remove()}.
1700888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 2.0
1710888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
1720888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean removeIf(
1730888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<T> removeFrom, Predicate<? super T> predicate) {
1740888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (removeFrom instanceof RandomAccess && removeFrom instanceof List) {
1750888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return removeIfFromRandomAccessList(
1760888a09821a98ac0680fad765217302858e70fa4Paul Duffin          (List<T>) removeFrom, checkNotNull(predicate));
1770888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
1780888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.removeIf(removeFrom.iterator(), predicate);
1790888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
1800888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1810888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> boolean removeIfFromRandomAccessList(
1820888a09821a98ac0680fad765217302858e70fa4Paul Duffin      List<T> list, Predicate<? super T> predicate) {
1830888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // Note: Not all random access lists support set() so we need to deal with
1840888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // those that don't and attempt the slower remove() based solution.
1850888a09821a98ac0680fad765217302858e70fa4Paul Duffin    int from = 0;
1860888a09821a98ac0680fad765217302858e70fa4Paul Duffin    int to = 0;
1870888a09821a98ac0680fad765217302858e70fa4Paul Duffin
1880888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (; from < list.size(); from++) {
1890888a09821a98ac0680fad765217302858e70fa4Paul Duffin      T element = list.get(from);
1900888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (!predicate.apply(element)) {
1910888a09821a98ac0680fad765217302858e70fa4Paul Duffin        if (from > to) {
1920888a09821a98ac0680fad765217302858e70fa4Paul Duffin          try {
1930888a09821a98ac0680fad765217302858e70fa4Paul Duffin            list.set(to, element);
1940888a09821a98ac0680fad765217302858e70fa4Paul Duffin          } catch (UnsupportedOperationException e) {
1950888a09821a98ac0680fad765217302858e70fa4Paul Duffin            slowRemoveIfForRemainingElements(list, predicate, to, from);
1960888a09821a98ac0680fad765217302858e70fa4Paul Duffin            return true;
1970888a09821a98ac0680fad765217302858e70fa4Paul Duffin          }
1980888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
1990888a09821a98ac0680fad765217302858e70fa4Paul Duffin        to++;
2000888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
2010888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
2020888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2030888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // Clear the tail of any remaining items
2040888a09821a98ac0680fad765217302858e70fa4Paul Duffin    list.subList(to, list.size()).clear();
2050888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return from != to;
2060888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2070888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2080888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> void slowRemoveIfForRemainingElements(List<T> list,
2090888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super T> predicate, int to, int from) {
2100888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // Here we know that:
2110888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // * (to < from) and that both are valid indices.
2120888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // * Everything with (index < to) should be kept.
2130888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // * Everything with (to <= index < from) should be removed.
2140888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // * The element with (index == from) should be kept.
2150888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // * Everything with (index > from) has not been checked yet.
2160888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2170888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // Check from the end of the list backwards (minimize expected cost of
2180888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // moving elements when remove() is called). Stop before 'from' because
2190888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // we already know that should be kept.
2200888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (int n = list.size() - 1; n > from; n--) {
2210888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (predicate.apply(list.get(n))) {
2220888a09821a98ac0680fad765217302858e70fa4Paul Duffin        list.remove(n);
2230888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
2240888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
2250888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // And now remove everything in the range [to, from) (going backwards).
2260888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (int n = from - 1; n >= to; n--) {
2270888a09821a98ac0680fad765217302858e70fa4Paul Duffin      list.remove(n);
2280888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
2290888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2300888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2310888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
2320888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Removes and returns the first matching element, or returns {@code null} if there is none.
2330888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
2340888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
2350888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <T> T removeFirstMatching(Iterable<T> removeFrom, Predicate<? super T> predicate) {
2360888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(predicate);
2370888a09821a98ac0680fad765217302858e70fa4Paul Duffin    Iterator<T> iterator = removeFrom.iterator();
2380888a09821a98ac0680fad765217302858e70fa4Paul Duffin    while (iterator.hasNext()) {
2390888a09821a98ac0680fad765217302858e70fa4Paul Duffin      T next = iterator.next();
2400888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (predicate.apply(next)) {
2410888a09821a98ac0680fad765217302858e70fa4Paul Duffin        iterator.remove();
2420888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return next;
2430888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
2440888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
2450888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return null;
2460888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2470888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2480888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
2490888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Determines whether two iterables contain equal elements in the same order.
2500888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * More specifically, this method returns {@code true} if {@code iterable1}
2510888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * and {@code iterable2} contain the same number of elements and every element
2520888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * of {@code iterable1} is equal to the corresponding element of
2530888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code iterable2}.
2540888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
2550888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean elementsEqual(
2560888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<?> iterable1, Iterable<?> iterable2) {
2570888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable1 instanceof Collection && iterable2 instanceof Collection) {
2580888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<?> collection1 = (Collection<?>) iterable1;
2590888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<?> collection2 = (Collection<?>) iterable2;
2600888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (collection1.size() != collection2.size()) {
2610888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return false;
2620888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
2630888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
2640888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.elementsEqual(iterable1.iterator(), iterable2.iterator());
2650888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2660888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2670888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
2680888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns a string representation of {@code iterable}, with the format {@code
2690888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * [e1, e2, ..., en]} (that is, identical to {@link java.util.Arrays
2700888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Arrays}{@code .toString(Iterables.toArray(iterable))}). Note that for
2710888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <i>most</i> implementations of {@link Collection}, {@code
2720888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * collection.toString()} also gives the same result, but that behavior is not
2730888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * generally guaranteed.
2740888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
2750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static String toString(Iterable<?> iterable) {
2760888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.toString(iterable.iterator());
2770888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2780888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2790888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
2800888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the single element contained in {@code iterable}.
2810888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
2820888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws NoSuchElementException if the iterable is empty
2830888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IllegalArgumentException if the iterable contains multiple
2840888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     elements
2850888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
2860888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T getOnlyElement(Iterable<T> iterable) {
2870888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.getOnlyElement(iterable.iterator());
2880888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
2890888a09821a98ac0680fad765217302858e70fa4Paul Duffin
2900888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
2910888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the single element contained in {@code iterable}, or {@code
2920888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * defaultValue} if the iterable is empty.
2930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
2940888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IllegalArgumentException if the iterator contains multiple
2950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     elements
2960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
2970888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
2980888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T getOnlyElement(
2990888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends T> iterable, @Nullable T defaultValue) {
3000888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.getOnlyElement(iterable.iterator(), defaultValue);
3010888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3020888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3030888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3040888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Copies an iterable's elements into an array.
3050888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3060888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param iterable the iterable to copy
3070888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return a newly-allocated array into which all the elements of the iterable
3080888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     have been copied
3090888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
3100888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static Object[] toArray(Iterable<?> iterable) {
3110888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return toCollection(iterable).toArray();
3120888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3130888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3140888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3150888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Converts an iterable into a collection. If the iterable is already a
3160888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is
3170888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * created with the contents of the iterable in the same iteration order.
3180888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
3190888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <E> Collection<E> toCollection(Iterable<E> iterable) {
3200888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (iterable instanceof Collection)
3210888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? (Collection<E>) iterable
3220888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Lists.newArrayList(iterable.iterator());
3230888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3240888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3250888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3260888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Adds all elements in {@code iterable} to {@code collection}.
3270888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3280888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return {@code true} if {@code collection} was modified as a result of this
3290888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     operation.
3300888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
3310888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean addAll(
3320888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<T> addTo, Iterable<? extends T> elementsToAdd) {
3330888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (elementsToAdd instanceof Collection) {
3340888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<? extends T> c = Collections2.cast(elementsToAdd);
3350888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return addTo.addAll(c);
3360888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
3370888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.addAll(addTo, checkNotNull(elementsToAdd).iterator());
3380888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3390888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3400888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3410888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the number of elements in the specified iterable that equal the
3420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * specified object. This implementation avoids a full iteration when the
3430888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterable is a {@link Multiset} or {@link Set}.
3440888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3450888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @see Collections#frequency
3460888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
3470888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static int frequency(Iterable<?> iterable, @Nullable Object element) {
3480888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if ((iterable instanceof Multiset)) {
3490888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return ((Multiset<?>) iterable).count(element);
3500888a09821a98ac0680fad765217302858e70fa4Paul Duffin    } else if ((iterable instanceof Set)) {
3510888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return ((Set<?>) iterable).contains(element) ? 1 : 0;
3520888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
3530888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.frequency(iterable.iterator(), element);
3540888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3550888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3570888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an iterable whose iterators cycle indefinitely over the elements of
3580888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code iterable}.
3590888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>That iterator supports {@code remove()} if {@code iterable.iterator()}
3610888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * does. After {@code remove()} is called, subsequent cycles omit the removed
3620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * element, which is no longer in {@code iterable}. The iterator's
3630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code hasNext()} method returns {@code true} until {@code iterable} is
3640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * empty.
3650888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3660888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
3670888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * infinite loop. You should use an explicit {@code break} or be certain that
3680888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * you will eventually remove all the elements.
3690888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3700888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>To cycle over the iterable {@code n} times, use the following:
3710888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code Iterables.concat(Collections.nCopies(n, iterable))}
3720888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
3730888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> cycle(final Iterable<T> iterable) {
3740888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
3750888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
3760888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
3770888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
3780888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.cycle(iterable);
3790888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
3800888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override public String toString() {
3810888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return iterable.toString() + " (cycled)";
3820888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
3830888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
3840888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
3850888a09821a98ac0680fad765217302858e70fa4Paul Duffin
3860888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
3870888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an iterable whose iterators cycle indefinitely over the provided
3880888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements.
3890888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3900888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>After {@code remove} is invoked on a generated iterator, the removed
3910888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * element will no longer appear in either that iterator or any other iterator
3920888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * created from the same source iterable. That is, this method behaves exactly
3930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * as {@code Iterables.cycle(Lists.newArrayList(elements))}. The iterator's
3940888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code hasNext} method returns {@code true} until all of the original
3950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements have been removed.
3960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
3970888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an
3980888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * infinite loop. You should use an explicit {@code break} or be certain that
3990888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * you will eventually remove all the elements.
4000888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4010888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>To cycle over the elements {@code n} times, use the following:
4020888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code Iterables.concat(Collections.nCopies(n, Arrays.asList(elements)))}
4030888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4040888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> cycle(T... elements) {
4050888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return cycle(Lists.newArrayList(elements));
4060888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4070888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4080888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4090888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Combines two iterables into a single iterable. The returned iterable has an
4100888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterator that traverses the elements in {@code a}, followed by the elements
4110888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * in {@code b}. The source iterators are not polled until necessary.
4120888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4130888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} when the
4140888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * corresponding input iterator supports it.
4150888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4160888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> concat(
4170888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends T> a, Iterable<? extends T> b) {
4180888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b));
4190888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4200888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4210888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4220888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Combines three iterables into a single iterable. The returned iterable has
4230888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * an iterator that traverses the elements in {@code a}, followed by the
4240888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements in {@code b}, followed by the elements in {@code c}. The source
4250888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterators are not polled until necessary.
4260888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4270888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} when the
4280888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * corresponding input iterator supports it.
4290888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4300888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> concat(Iterable<? extends T> a,
4310888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends T> b, Iterable<? extends T> c) {
4320888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b, c));
4330888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4340888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4350888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4360888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Combines four iterables into a single iterable. The returned iterable has
4370888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * an iterator that traverses the elements in {@code a}, followed by the
4380888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements in {@code b}, followed by the elements in {@code c}, followed by
4390888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * the elements in {@code d}. The source iterators are not polled until
4400888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * necessary.
4410888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} when the
4430888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * corresponding input iterator supports it.
4440888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4450888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> concat(Iterable<? extends T> a,
4460888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends T> b, Iterable<? extends T> c,
4470888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends T> d) {
4480888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.of(a, b, c, d));
4490888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4500888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4510888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4520888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Combines multiple iterables into a single iterable. The returned iterable
4530888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * has an iterator that traverses the elements of each iterable in
4540888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code inputs}. The input iterators are not polled until necessary.
4550888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4560888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} when the
4570888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * corresponding input iterator supports it.
4580888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4590888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws NullPointerException if any of the provided iterables is null
4600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> concat(Iterable<? extends T>... inputs) {
4620888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return concat(ImmutableList.copyOf(inputs));
4630888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4640888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4650888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4660888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Combines multiple iterables into a single iterable. The returned iterable
4670888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * has an iterator that traverses the elements of each iterable in
4680888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code inputs}. The input iterators are not polled until necessary.
4690888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
4700888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} when the
4710888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * corresponding input iterator supports it. The methods of the returned
4720888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterable may throw {@code NullPointerException} if any of the input
4730888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterators is null.
4740888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> concat(
4760888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<? extends Iterable<? extends T>> inputs) {
4770888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(inputs);
4780888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
4790888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
4800888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
4810888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.concat(iterators(inputs));
4820888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
4830888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
4840888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4850888a09821a98ac0680fad765217302858e70fa4Paul Duffin
4860888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
4870888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an iterator over the iterators of the given iterables.
4880888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
4890888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> Iterator<Iterator<? extends T>> iterators(
4900888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<? extends Iterable<? extends T>> iterables) {
4910888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new TransformedIterator<Iterable<? extends T>, Iterator<? extends T>>(
4920888a09821a98ac0680fad765217302858e70fa4Paul Duffin        iterables.iterator()) {
4930888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
4940888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> transform(Iterable<? extends T> from) {
4950888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return from.iterator();
4960888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
4970888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
4980888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
4990888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5000888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5010888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Divides an iterable into unmodifiable sublists of the given size (the final
5020888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterable may be smaller). For example, partitioning an iterable containing
5030888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code
5040888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * [[a, b, c], [d, e]]} -- an outer iterable containing two inner lists of
5050888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * three and two elements, all in the original order.
5060888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
5070888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Iterators returned by the returned iterable do not support the {@link
5080888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Iterator#remove()} method. The returned lists implement {@link
5090888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * RandomAccess}, whether or not the input list does.
5100888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
5110888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p><b>Note:</b> if {@code iterable} is a {@link List}, use {@link
5120888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Lists#partition(List, int)} instead.
5130888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
5140888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param iterable the iterable to return a partitioned view of
5150888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param size the desired size of each partition (the last may be smaller)
5160888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return an iterable of unmodifiable lists containing the elements of {@code
5170888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     iterable} divided into partitions
5180888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IllegalArgumentException if {@code size} is nonpositive
5190888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
5200888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<List<T>> partition(
5210888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<T> iterable, final int size) {
5220888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
5230888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkArgument(size > 0);
5240888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<List<T>>() {
5250888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
5260888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<List<T>> iterator() {
5270888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.partition(iterable.iterator(), size);
5280888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
5290888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
5300888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
5310888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5320888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5330888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Divides an iterable into unmodifiable sublists of the given size, padding
5340888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * the final iterable with null values if necessary. For example, partitioning
5350888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * an iterable containing {@code [a, b, c, d, e]} with a partition size of 3
5360888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterable containing
5370888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * two inner lists of three elements each, all in the original order.
5380888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
5390888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Iterators returned by the returned iterable do not support the {@link
5400888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Iterator#remove()} method.
5410888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
5420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param iterable the iterable to return a partitioned view of
5430888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param size the desired size of each partition
5440888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return an iterable of unmodifiable lists containing the elements of {@code
5450888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     iterable} divided into partitions (the final iterable may have
5460888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     trailing null elements)
5470888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IllegalArgumentException if {@code size} is nonpositive
5480888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
5490888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<List<T>> paddedPartition(
5500888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<T> iterable, final int size) {
5510888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
5520888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkArgument(size > 0);
5530888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<List<T>>() {
5540888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
5550888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<List<T>> iterator() {
5560888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.paddedPartition(iterable.iterator(), size);
5570888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
5580888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
5590888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
5600888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the elements of {@code unfiltered} that satisfy a predicate. The
5630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * resulting iterable's iterator does not support {@code remove()}.
5640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
5650888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> filter(
5660888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<T> unfiltered, final Predicate<? super T> predicate) {
5670888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(unfiltered);
5680888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(predicate);
5690888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
5700888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
5710888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
5720888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.filter(unfiltered.iterator(), predicate);
5730888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
5740888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
5750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
5760888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5770888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5780888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns {@code true} if any element in {@code iterable} satisfies the predicate.
5790888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
5800888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean any(
5810888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<T> iterable, Predicate<? super T> predicate) {
5820888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.any(iterable.iterator(), predicate);
5830888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
5840888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5850888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5860888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns {@code true} if every element in {@code iterable} satisfies the
5870888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * predicate. If {@code iterable} is empty, {@code true} is returned.
5880888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
5890888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> boolean all(
5900888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<T> iterable, Predicate<? super T> predicate) {
5910888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.all(iterable.iterator(), predicate);
5920888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
5930888a09821a98ac0680fad765217302858e70fa4Paul Duffin
5940888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
5950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the first element in {@code iterable} that satisfies the given
5960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * predicate; use this method only when such an element is known to exist. If
5970888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * it is possible that <i>no</i> element will match, use {@link #tryFind} or
5980888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@link #find(Iterable, Predicate, Object)} instead.
5990888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6000888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws NoSuchElementException if no element in {@code iterable} matches
6010888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     the given predicate
6020888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6030888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T find(Iterable<T> iterable,
6040888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super T> predicate) {
6050888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.find(iterable.iterator(), predicate);
6060888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6070888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6080888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6090888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the first element in {@code iterable} that satisfies the given
6100888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * predicate, or {@code defaultValue} if none found. Note that this can
6110888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * usually be handled more naturally using {@code
6120888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * tryFind(iterable, predicate).or(defaultValue)}.
6130888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6140888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 7.0
6150888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6160888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
6170888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T find(Iterable<? extends T> iterable,
6180888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super T> predicate, @Nullable T defaultValue) {
6190888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.find(iterable.iterator(), predicate, defaultValue);
6200888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6210888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6220888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6230888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an {@link Optional} containing the first element in {@code
6240888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterable} that satisfies the given predicate, if such an element exists.
6250888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6260888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code
6270888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * null}. If {@code null} is matched in {@code iterable}, a
6280888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * NullPointerException will be thrown.
6290888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6300888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 11.0
6310888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6320888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Optional<T> tryFind(Iterable<T> iterable,
6330888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super T> predicate) {
6340888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.tryFind(iterable.iterator(), predicate);
6350888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6360888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6370888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6380888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the index in {@code iterable} of the first element that satisfies
6390888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * the provided {@code predicate}, or {@code -1} if the Iterable has no such
6400888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements.
6410888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>More formally, returns the lowest index {@code i} such that
6430888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code predicate.apply(Iterables.get(iterable, i))} returns {@code true},
6440888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * or {@code -1} if there is no such index.
6450888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6460888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 2.0
6470888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6480888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> int indexOf(
6490888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterable<T> iterable, Predicate<? super T> predicate) {
6500888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.indexOf(iterable.iterator(), predicate);
6510888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6520888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6530888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6540888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an iterable that applies {@code function} to each element of {@code
6550888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * fromIterable}.
6560888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6570888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} if the
6580888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * provided iterator does. After a successful {@code remove()} call,
6590888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code fromIterable} no longer contains the corresponding element.
6600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6610888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>If the input {@code Iterable} is known to be a {@code List} or other
6620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code Collection}, consider {@link Lists#transform} and {@link
6630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Collections2#transform}.
6640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6650888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <F, T> Iterable<T> transform(final Iterable<F> fromIterable,
6660888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Function<? super F, ? extends T> function) {
6670888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(fromIterable);
6680888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(function);
6690888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
6700888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
6710888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
6720888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.transform(fromIterable.iterator(), function);
6730888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
6740888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
6750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6760888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6770888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6780888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the element at the specified position in an iterable.
6790888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6800888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param position position of the element to return
6810888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return the element at the specified position in {@code iterable}
6820888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IndexOutOfBoundsException if {@code position} is negative or
6830888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     greater than or equal to the size of {@code iterable}
6840888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
6850888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T get(Iterable<T> iterable, int position) {
6860888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
6870888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (iterable instanceof List)
6880888a09821a98ac0680fad765217302858e70fa4Paul Duffin        ? ((List<T>) iterable).get(position)
6890888a09821a98ac0680fad765217302858e70fa4Paul Duffin        : Iterators.get(iterable.iterator(), position);
6900888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
6910888a09821a98ac0680fad765217302858e70fa4Paul Duffin
6920888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
6930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the element at the specified position in an iterable or a default
6940888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * value otherwise.
6950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
6960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param position position of the element to return
6970888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param defaultValue the default value to return if {@code position} is
6980888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     greater than or equal to the size of the iterable
6990888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return the element at the specified position in {@code iterable} or
7000888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     {@code defaultValue} if {@code iterable} contains fewer than
7010888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     {@code position + 1} elements.
7020888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IndexOutOfBoundsException if {@code position} is negative
7030888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 4.0
7040888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
7050888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
7060888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T get(Iterable<? extends T> iterable, int position, @Nullable T defaultValue) {
7070888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
7080888a09821a98ac0680fad765217302858e70fa4Paul Duffin    Iterators.checkNonnegative(position);
7090888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof List) {
7100888a09821a98ac0680fad765217302858e70fa4Paul Duffin      List<? extends T> list = Lists.cast(iterable);
7110888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return (position < list.size()) ? list.get(position) : defaultValue;
7120888a09821a98ac0680fad765217302858e70fa4Paul Duffin    } else {
7130888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterator<? extends T> iterator = iterable.iterator();
7140888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Iterators.advance(iterator, position);
7150888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return Iterators.getNext(iterator, defaultValue);
7160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
7170888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
7180888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7190888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
7200888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the first element in {@code iterable} or {@code defaultValue} if
7210888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * the iterable is empty.  The {@link Iterators} analog to this method is
7220888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@link Iterators#getNext}.
7230888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7240888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>If no default value is desired (and the caller instead wants a
7250888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@link NoSuchElementException} to be thrown), it is recommended that
7260888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code iterable.iterator().next()} is used instead.
7270888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7280888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param defaultValue the default value to return if the iterable is empty
7290888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return the first element of {@code iterable} or the default value
7300888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 7.0
7310888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
7320888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
7330888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T getFirst(Iterable<? extends T> iterable, @Nullable T defaultValue) {
7340888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.getNext(iterable.iterator(), defaultValue);
7350888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
7360888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7370888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
7380888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the last element of {@code iterable}.
7390888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7400888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return the last element of {@code iterable}
7410888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws NoSuchElementException if the iterable is empty
7420888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
7430888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T getLast(Iterable<T> iterable) {
7440888a09821a98ac0680fad765217302858e70fa4Paul Duffin    // TODO(kevinb): Support a concurrently modified collection?
7450888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof List) {
7460888a09821a98ac0680fad765217302858e70fa4Paul Duffin      List<T> list = (List<T>) iterable;
7470888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (list.isEmpty()) {
7480888a09821a98ac0680fad765217302858e70fa4Paul Duffin        throw new NoSuchElementException();
7490888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
7500888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return getLastInNonemptyList(list);
7510888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
7520888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7530888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.getLast(iterable.iterator());
7540888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
7550888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
7570888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns the last element of {@code iterable} or {@code defaultValue} if
7580888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * the iterable is empty.
7590888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param defaultValue the value to return if {@code iterable} is empty
7610888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return the last element of {@code iterable} or the default value
7620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 3.0
7630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
7640888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Nullable
7650888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> T getLast(Iterable<? extends T> iterable, @Nullable T defaultValue) {
7660888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof Collection) {
7670888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Collection<? extends T> c = Collections2.cast(iterable);
7680888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (c.isEmpty()) {
7690888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return defaultValue;
7700888a09821a98ac0680fad765217302858e70fa4Paul Duffin      } else if (iterable instanceof List) {
7710888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return getLastInNonemptyList(Lists.cast(iterable));
7720888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
7730888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
7740888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7750888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Iterators.getLast(iterable.iterator(), defaultValue);
7760888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
7770888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7780888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> T getLastInNonemptyList(List<T> list) {
7790888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return list.get(list.size() - 1);
7800888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
7810888a09821a98ac0680fad765217302858e70fa4Paul Duffin
7820888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
7830888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns a view of {@code iterable} that skips its first
7840888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code numberToSkip} elements. If {@code iterable} contains fewer than
7850888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code numberToSkip} elements, the returned iterable skips all of its
7860888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * elements.
7870888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7880888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Modifications to the underlying {@link Iterable} before a call to
7890888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code iterator()} are reflected in the returned iterator. That is, the
7900888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterator skips the first {@code numberToSkip} elements that exist when the
7910888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code Iterator} is created, not when {@code skip()} is called.
7920888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
7930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>The returned iterable's iterator supports {@code remove()} if the
7940888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterator of the underlying iterable supports it. Note that it is
7950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <i>not</i> possible to delete the last skipped element by immediately
7960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * calling {@code remove()} on that iterator, as the {@code Iterator}
7970888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * contract states that a call to {@code remove()} before a call to
7980888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code next()} will throw an {@link IllegalStateException}.
7990888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
8000888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 3.0
8010888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
8020888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> skip(final Iterable<T> iterable,
8030888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final int numberToSkip) {
8040888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
8050888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkArgument(numberToSkip >= 0, "number to skip cannot be negative");
8060888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8070888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof List) {
8080888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final List<T> list = (List<T>) iterable;
8090888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return new FluentIterable<T>() {
8100888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
8110888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public Iterator<T> iterator() {
8120888a09821a98ac0680fad765217302858e70fa4Paul Duffin          // TODO(kevinb): Support a concurrently modified collection?
8130888a09821a98ac0680fad765217302858e70fa4Paul Duffin          int toSkip = Math.min(list.size(), numberToSkip);
8140888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return list.subList(toSkip, list.size()).iterator();
8150888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
8160888a09821a98ac0680fad765217302858e70fa4Paul Duffin      };
8170888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
8180888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8190888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
8200888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
8210888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
8220888a09821a98ac0680fad765217302858e70fa4Paul Duffin        final Iterator<T> iterator = iterable.iterator();
8230888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8240888a09821a98ac0680fad765217302858e70fa4Paul Duffin        Iterators.advance(iterator, numberToSkip);
8250888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8260888a09821a98ac0680fad765217302858e70fa4Paul Duffin        /*
8270888a09821a98ac0680fad765217302858e70fa4Paul Duffin         * We can't just return the iterator because an immediate call to its
8280888a09821a98ac0680fad765217302858e70fa4Paul Duffin         * remove() method would remove one of the skipped elements instead of
8290888a09821a98ac0680fad765217302858e70fa4Paul Duffin         * throwing an IllegalStateException.
8300888a09821a98ac0680fad765217302858e70fa4Paul Duffin         */
8310888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return new Iterator<T>() {
8320888a09821a98ac0680fad765217302858e70fa4Paul Duffin          boolean atStart = true;
8330888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8340888a09821a98ac0680fad765217302858e70fa4Paul Duffin          @Override
8350888a09821a98ac0680fad765217302858e70fa4Paul Duffin          public boolean hasNext() {
8360888a09821a98ac0680fad765217302858e70fa4Paul Duffin            return iterator.hasNext();
8370888a09821a98ac0680fad765217302858e70fa4Paul Duffin          }
8380888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8390888a09821a98ac0680fad765217302858e70fa4Paul Duffin          @Override
8400888a09821a98ac0680fad765217302858e70fa4Paul Duffin          public T next() {
8410888a09821a98ac0680fad765217302858e70fa4Paul Duffin            T result = iterator.next();
8420888a09821a98ac0680fad765217302858e70fa4Paul Duffin            atStart = false; // not called if next() fails
8430888a09821a98ac0680fad765217302858e70fa4Paul Duffin            return result;
8440888a09821a98ac0680fad765217302858e70fa4Paul Duffin          }
8450888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8460888a09821a98ac0680fad765217302858e70fa4Paul Duffin          @Override
8470888a09821a98ac0680fad765217302858e70fa4Paul Duffin          public void remove() {
8480888a09821a98ac0680fad765217302858e70fa4Paul Duffin            checkRemove(!atStart);
8490888a09821a98ac0680fad765217302858e70fa4Paul Duffin            iterator.remove();
8500888a09821a98ac0680fad765217302858e70fa4Paul Duffin          }
8510888a09821a98ac0680fad765217302858e70fa4Paul Duffin        };
8520888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
8530888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
8540888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
8550888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
8570888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Creates an iterable with the first {@code limitSize} elements of the given
8580888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterable. If the original iterable does not contain that many elements, the
8590888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * returned iterable will have the same behavior as the original iterable. The
8600888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * returned iterable's iterator supports {@code remove()} if the original
8610888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterator does.
8620888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
8630888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param iterable the iterable to limit
8640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param limitSize the maximum number of elements in the returned iterable
8650888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @throws IllegalArgumentException if {@code limitSize} is negative
8660888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 3.0
8670888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
8680888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> limit(
8690888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<T> iterable, final int limitSize) {
8700888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
8710888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkArgument(limitSize >= 0, "limit is negative");
8720888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
8730888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
8740888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
8750888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.limit(iterable.iterator(), limitSize);
8760888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
8770888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
8780888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
8790888a09821a98ac0680fad765217302858e70fa4Paul Duffin
8800888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
8810888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns a view of the supplied iterable that wraps each generated
8820888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@link Iterator} through {@link Iterators#consumingIterator(Iterator)}.
8830888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
8840888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Note: If {@code iterable} is a {@link Queue}, the returned iterable will
8850888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * get entries from {@link Queue#remove()} since {@link Queue}'s iteration
8860888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * order is undefined.  Calling {@link Iterator#hasNext()} on a generated
8870888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * iterator from the returned iterable may cause an item to be immediately
8880888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * dequeued for return on a subsequent call to {@link Iterator#next()}.
8890888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
8900888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @param iterable the iterable to wrap
8910888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return a view of the supplied iterable that wraps each generated iterator
8920888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     through {@link Iterators#consumingIterator(Iterator)}; for queues,
8930888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     an iterable that generates iterators that return and consume the
8940888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *     queue's elements in queue order
8950888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
8960888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @see Iterators#consumingIterator(Iterator)
8970888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 2.0
8980888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
8990888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> consumingIterable(final Iterable<T> iterable) {
9000888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof Queue) {
9010888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return new FluentIterable<T>() {
9020888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
9030888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public Iterator<T> iterator() {
9040888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return new ConsumingQueueIterator<T>((Queue<T>) iterable);
9050888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
9060888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9070888a09821a98ac0680fad765217302858e70fa4Paul Duffin        @Override
9080888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public String toString() {
9090888a09821a98ac0680fad765217302858e70fa4Paul Duffin          return "Iterables.consumingIterable(...)";
9100888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
9110888a09821a98ac0680fad765217302858e70fa4Paul Duffin      };
9120888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
9130888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9140888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterable);
9150888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9160888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new FluentIterable<T>() {
9170888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9180888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
9190888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.consumingIterator(iterable.iterator());
9200888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
9210888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9220888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9230888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public String toString() {
9240888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return "Iterables.consumingIterable(...)";
9250888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
9260888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
9270888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
9280888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9290888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static class ConsumingQueueIterator<T> extends AbstractIterator<T> {
9300888a09821a98ac0680fad765217302858e70fa4Paul Duffin    private final Queue<T> queue;
9310888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9320888a09821a98ac0680fad765217302858e70fa4Paul Duffin    private ConsumingQueueIterator(Queue<T> queue) {
9330888a09821a98ac0680fad765217302858e70fa4Paul Duffin      this.queue = queue;
9340888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
9350888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9360888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override public T computeNext() {
9370888a09821a98ac0680fad765217302858e70fa4Paul Duffin      try {
9380888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return queue.remove();
9390888a09821a98ac0680fad765217302858e70fa4Paul Duffin      } catch (NoSuchElementException e) {
9400888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return endOfData();
9410888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
9420888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
9430888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
9440888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9450888a09821a98ac0680fad765217302858e70fa4Paul Duffin  // Methods only in Iterables, not in Iterators
9460888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9470888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
9480888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Determines if the given iterable contains no elements.
9490888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
9500888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>There is no precise {@link Iterator} equivalent to this method, since
9510888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * one can only ask an iterator whether it has any elements <i>remaining</i>
9520888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * (which one does using {@link Iterator#hasNext}).
9530888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
9540888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @return {@code true} if the iterable contains no elements
9550888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
9560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static boolean isEmpty(Iterable<?> iterable) {
9570888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (iterable instanceof Collection) {
9580888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return ((Collection<?>) iterable).isEmpty();
9590888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
9600888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return !iterable.iterator().hasNext();
9610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
9620888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9630888a09821a98ac0680fad765217302858e70fa4Paul Duffin  /**
9640888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * Returns an iterable over the merged contents of all given
9650888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * {@code iterables}. Equivalent entries will not be de-duplicated.
9660888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
9670888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>Callers must ensure that the source {@code iterables} are in
9680888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * non-descending order as this method does not sort its input.
9690888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
9700888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * <p>For any equivalent elements across all {@code iterables}, it is
9710888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * undefined which element is returned first.
9720888a09821a98ac0680fad765217302858e70fa4Paul Duffin   *
9730888a09821a98ac0680fad765217302858e70fa4Paul Duffin   * @since 11.0
9740888a09821a98ac0680fad765217302858e70fa4Paul Duffin   */
9750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Beta
9760888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public static <T> Iterable<T> mergeSorted(
9770888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Iterable<? extends Iterable<? extends T>> iterables,
9780888a09821a98ac0680fad765217302858e70fa4Paul Duffin      final Comparator<? super T> comparator) {
9790888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(iterables, "iterables");
9800888a09821a98ac0680fad765217302858e70fa4Paul Duffin    checkNotNull(comparator, "comparator");
9810888a09821a98ac0680fad765217302858e70fa4Paul Duffin    Iterable<T> iterable = new FluentIterable<T>() {
9820888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9830888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<T> iterator() {
9840888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return Iterators.mergeSorted(
9850888a09821a98ac0680fad765217302858e70fa4Paul Duffin            Iterables.transform(iterables, Iterables.<T>toIterator()),
9860888a09821a98ac0680fad765217302858e70fa4Paul Duffin            comparator);
9870888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
9880888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
9890888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new UnmodifiableIterable<T>(iterable);
9900888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
9910888a09821a98ac0680fad765217302858e70fa4Paul Duffin
9920888a09821a98ac0680fad765217302858e70fa4Paul Duffin  // TODO(user): Is this the best place for this? Move to fluent functions?
9930888a09821a98ac0680fad765217302858e70fa4Paul Duffin  // Useful as a public method?
9940888a09821a98ac0680fad765217302858e70fa4Paul Duffin  private static <T> Function<Iterable<? extends T>, Iterator<? extends T>>
9950888a09821a98ac0680fad765217302858e70fa4Paul Duffin      toIterator() {
9960888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new Function<Iterable<? extends T>, Iterator<? extends T>>() {
9970888a09821a98ac0680fad765217302858e70fa4Paul Duffin      @Override
9980888a09821a98ac0680fad765217302858e70fa4Paul Duffin      public Iterator<? extends T> apply(Iterable<? extends T> iterable) {
9990888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return iterable.iterator();
10000888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
10010888a09821a98ac0680fad765217302858e70fa4Paul Duffin    };
10020888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
10030888a09821a98ac0680fad765217302858e70fa4Paul Duffin}
1004