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 Bringert 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions; 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate; 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator; 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List; 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException; 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue; 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.RandomAccess; 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Set; 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.SortedSet; 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class contains static utility methods that operate on or return objects 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of type {@code Iterable}. Except as noted, each method has a corresponding 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterator}-based method in the {@link Iterators} class. 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterables 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that their iterators 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * only advance the backing iteration when absolutely necessary. 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion 53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Iterables { 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Iterables() {} 59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns an unmodifiable view of {@code iterable}. */ 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> unmodifiableIterable( 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<T> iterable) { 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof UnmodifiableIterable || 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterable instanceof ImmutableCollection) { 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable; 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterable<T>(iterable); 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Simply returns its argument. 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated no need to use this 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated public static <E> Iterable<E> unmodifiableIterable( 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableCollection<E> iterable) { 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return checkNotNull(iterable); 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final class UnmodifiableIterable<T> implements Iterable<T> { 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Iterable<T> iterable; 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private UnmodifiableIterable(Iterable<T> iterable) { 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.iterable = iterable; 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.unmodifiableIterator(iterable.iterator()); 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public String toString() { 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable.toString(); 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // no equals and hashCode; it would break the contract! 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements in {@code iterable}. 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int size(Iterable<?> iterable) { 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (iterable instanceof Collection) 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) iterable).size() 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.size(iterable.iterator()); 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if {@code iterable} contains {@code element}; that is, 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * any object for which {@code equals(element)} is true. 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean contains(Iterable<?> iterable, @Nullable Object element) 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson { 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof Collection) { 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<?> collection = (Collection<?>) iterable; 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson try { 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return collection.contains(element); 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (NullPointerException e) { 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (ClassCastException e) { 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.contains(iterable.iterator(), element); 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that belongs to the provided 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection. 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method calls {@link Collection#removeAll} if {@code iterable} is a 134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection, and {@link Iterators#removeAll} otherwise. 135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRemove the elements to remove 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterable} 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean removeAll( 141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> removeFrom, Collection<?> elementsToRemove) { 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (removeFrom instanceof Collection) 143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) removeFrom).removeAll(checkNotNull(elementsToRemove)) 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.removeAll(removeFrom.iterator(), elementsToRemove); 145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that does not belong to the 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided collection. 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method calls {@link Collection#retainAll} if {@code iterable} is a 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection, and {@link Iterators#retainAll} otherwise. 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRetain the elements to retain 1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterable} 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean retainAll( 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> removeFrom, Collection<?> elementsToRetain) { 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (removeFrom instanceof Collection) 161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) removeFrom).retainAll(checkNotNull(elementsToRetain)) 162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.retainAll(removeFrom.iterator(), elementsToRetain); 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that satisfies the provided 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate. 168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param predicate a predicate that determines whether an element should 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * be removed 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if any elements were removed from the iterable 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException if the iterable does not support 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove()}. 1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 178bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> boolean removeIf( 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> removeFrom, Predicate<? super T> predicate) { 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (removeFrom instanceof RandomAccess && removeFrom instanceof List) { 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return removeIfFromRandomAccessList( 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson (List<T>) removeFrom, checkNotNull(predicate)); 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.removeIf(removeFrom.iterator(), predicate); 185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static <T> boolean removeIfFromRandomAccessList( 188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list, Predicate<? super T> predicate) { 1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Note: Not all random access lists support set() so we need to deal with 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // those that don't and attempt the slower remove() based solution. 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int from = 0; 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int to = 0; 193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (; from < list.size(); from++) { 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T element = list.get(from); 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!predicate.apply(element)) { 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (from > to) { 1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.set(to, element); 2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (UnsupportedOperationException e) { 2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert slowRemoveIfForRemainingElements(list, predicate, to, from); 2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return true; 2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson to++; 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Clear the tail of any remaining items 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.subList(to, list.size()).clear(); 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return from != to; 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> void slowRemoveIfForRemainingElements(List<T> list, 2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate, int to, int from) { 2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Here we know that: 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * (to < from) and that both are valid indices. 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (index < to) should be kept. 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (to <= index < from) should be removed. 2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * The element with (index == from) should be kept. 2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (index > from) has not been checked yet. 2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Check from the end of the list backwards (minimize expected cost of 2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // moving elements when remove() is called). Stop before 'from' because 2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // we already know that should be kept. 2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int n = list.size() - 1; n > from; n--) { 2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (predicate.apply(list.get(n))) { 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.remove(n); 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // And now remove everything in the range [to, from) (going backwards). 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int n = from - 1; n >= to; n--) { 2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.remove(n); 2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Determines whether two iterables contain equal elements in the same order. 239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * More specifically, this method returns {@code true} if {@code iterable1} 240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * and {@code iterable2} contain the same number of elements and every element 241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of {@code iterable1} is equal to the corresponding element of 242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterable2}. 243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean elementsEqual( 245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> iterable1, Iterable<?> iterable2) { 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.elementsEqual(iterable1.iterator(), iterable2.iterator()); 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a string representation of {@code iterable}, with the format 251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [e1, e2, ..., en]}. 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static String toString(Iterable<?> iterable) { 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.toString(iterable.iterator()); 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterable}. 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if the iterable is empty 261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterable contains multiple 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement(Iterable<T> iterable) { 265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getOnlyElement(iterable.iterator()); 266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterable}, or {@code 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * defaultValue} if the iterable is empty. 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterator contains multiple 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement( 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, @Nullable T defaultValue) { 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getOnlyElement(iterable.iterator(), defaultValue); 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copies an iterable's elements into an array. 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to copy 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a newly-allocated array into which all the elements of the iterable 285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * have been copied 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert static Object[] toArray(Iterable<?> iterable) { 2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return toCollection(iterable).toArray(); 2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Converts an iterable into a collection. If the iterable is already a 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * created with the contents of the iterable in the same iteration order. 2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <E> Collection<E> toCollection(Iterable<E> iterable) { 2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (iterable instanceof Collection) 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? (Collection<E>) iterable 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : Lists.newArrayList(iterable.iterator()); 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds all elements in {@code iterable} to {@code collection}. 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if {@code collection} was modified as a result of this 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * operation. 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean addAll( 309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<T> addTo, Iterable<? extends T> elementsToAdd) { 310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (elementsToAdd instanceof Collection) { 3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collection<? extends T> c = Collections2.cast(elementsToAdd); 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return addTo.addAll(c); 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.addAll(addTo, elementsToAdd.iterator()); 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements in the specified iterable that equal the 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * specified object. This implementation avoids a full iteration when the 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable is a {@link Multiset} or {@link Set}. 321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @see Collections#frequency 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int frequency(Iterable<?> iterable, @Nullable Object element) { 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if ((iterable instanceof Multiset)) { 326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((Multiset<?>) iterable).count(element); 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if ((iterable instanceof Set)) { 329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((Set<?>) iterable).contains(element) ? 1 : 0; 330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.frequency(iterable.iterator(), element); 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable whose iterators cycle indefinitely over the elements of 336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterable}. 337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>That iterator supports {@code remove()} if {@code iterable.iterator()} 339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * does. After {@code remove()} is called, subsequent cycles omit the removed 340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element, which is no longer in {@code iterable}. The iterator's 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method returns {@code true} until {@code iterable} is 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * empty. 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>To cycle over the iterable {@code n} times, use the following: 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Iterables.concat(Collections.nCopies(n, iterable))} 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> cycle(final Iterable<T> iterable) { 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Iterable<T>() { 3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.cycle(iterable); 357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterable.toString() + " (cycled)"; 360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable whose iterators cycle indefinitely over the provided 366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements. 367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>After {@code remove} is invoked on a generated iterator, the removed 369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element will no longer appear in either that iterator or any other iterator 370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * created from the same source iterable. That is, this method behaves exactly 371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * as {@code Iterables.cycle(Lists.newArrayList(elements))}. The iterator's 372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext} method returns {@code true} until all of the original 373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements have been removed. 374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>To cycle over the elements {@code n} times, use the following: 380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Iterables.concat(Collections.nCopies(n, Arrays.asList(elements)))} 381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> cycle(T... elements) { 383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return cycle(Lists.newArrayList(elements)); 384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines two iterables into a single iterable. The returned iterable has an 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator that traverses the elements in {@code a}, followed by the elements 389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in {@code b}. The source iterators are not polled until necessary. 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat( 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> a, Iterable<? extends T> b) { 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b)); 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines three iterables into a single iterable. The returned iterable has 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterator that traverses the elements in {@code a}, followed by the 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements in {@code b}, followed by the elements in {@code c}. The source 406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterators are not polled until necessary. 407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T> a, 413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> b, Iterable<? extends T> c) { 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c)); 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines four iterables into a single iterable. The returned iterable has 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterator that traverses the elements in {@code a}, followed by the 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements in {@code b}, followed by the elements in {@code c}, followed by 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the elements in {@code d}. The source iterators are not polled until 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * necessary. 426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T> a, 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> b, Iterable<? extends T> c, 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> d) { 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(d); 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c, d)); 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterables into a single iterable. The returned iterable 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * has an iterator that traverses the elements of each iterable in 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code inputs}. The input iterators are not polled until necessary. 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any of the provided iterables is null 450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T>... inputs) { 4521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return concat(ImmutableList.copyOf(inputs)); 453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterables into a single iterable. The returned iterable 457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * has an iterator that traverses the elements of each iterable in 458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code inputs}. The input iterators are not polled until necessary. 459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. The methods of the returned 462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable may throw {@code NullPointerException} if any of the input 4631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterators is null. 464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat( 4661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<? extends Iterable<? extends T>> inputs) { 4671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(inputs); 468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 4691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 4711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.concat(iterators(inputs)); 4721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 4741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 4771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterator over the iterators of the given iterables. 4781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 4791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> UnmodifiableIterator<Iterator<? extends T>> iterators( 4801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<? extends Iterable<? extends T>> iterables) { 4811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Iterable<? extends T>> iterableIterator = 4821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterables.iterator(); 4831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterator<Iterator<? extends T>>() { 4841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 4851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public boolean hasNext() { 4861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterableIterator.hasNext(); 4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<? extends T> next() { 4901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterableIterator.next().iterator(); 491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterable into unmodifiable sublists of the given size (the final 497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable may be smaller). For example, partitioning an iterable containing 498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code 499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * [[a, b, c], [d, e]]} -- an outer iterable containing two inner lists of 500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * three and two elements, all in the original order. 501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Iterators returned by the returned iterable do not support the {@link 503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterator#remove()} method. The returned lists implement {@link 504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * RandomAccess}, whether or not the input list does. 505090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Note:</b> if {@code iterable} is a {@link List}, use {@link 507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Lists#partition(List, int)} instead. 508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to return a partitioned view of 510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition (the last may be smaller) 511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable of unmodifiable lists containing the elements of {@code 512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable} divided into partitions 513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<List<T>> partition( 516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> iterable, final int size) { 517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(size > 0); 519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<List<T>>() { 5201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<List<T>> iterator() { 522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.partition(iterable.iterator(), size); 523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterable into unmodifiable sublists of the given size, padding 529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the final iterable with null values if necessary. For example, partitioning 530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterable containing {@code [a, b, c, d, e]} with a partition size of 3 531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterable containing 532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * two inner lists of three elements each, all in the original order. 533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Iterators returned by the returned iterable do not support the {@link 535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterator#remove()} method. 536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to return a partitioned view of 538090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition 539090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable of unmodifiable lists containing the elements of {@code 540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable} divided into partitions (the final iterable may have 541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * trailing null elements) 542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<List<T>> paddedPartition( 545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> iterable, final int size) { 546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(size > 0); 548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<List<T>>() { 5491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<List<T>> iterator() { 551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.paddedPartition(iterable.iterator(), size); 552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 556090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the elements of {@code unfiltered} that satisfy a predicate. The 558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * resulting iterable's iterator does not support {@code remove()}. 559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> filter( 561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> unfiltered, final Predicate<? super T> predicate) { 562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(unfiltered); 563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 5651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.filter(unfiltered.iterator(), predicate); 568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if one or more elements in {@code iterable} satisfy 574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the predicate. 575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean any( 577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, Predicate<? super T> predicate) { 578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.any(iterable.iterator(), predicate); 579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if every element in {@code iterable} satisfies the 583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate. If {@code iterable} is empty, {@code true} is returned. 584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean all( 586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, Predicate<? super T> predicate) { 587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.all(iterable.iterator(), predicate); 588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the first element in {@code iterable} that satisfies the given 5921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate; use this method only when such an element is known to exist. If 5931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * it is possible that <i>no</i> element will match, use {@link 5941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #tryFind)} or {@link #find(Iterable, Predicate, T)} instead. 595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if no element in {@code iterable} matches 597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the given predicate 598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T find(Iterable<T> iterable, 600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Predicate<? super T> predicate) { 601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.find(iterable.iterator(), predicate); 602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 6051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the first element in {@code iterable} that satisfies the given 6061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate, or {@code defaultValue} if none found. Note that this can 6071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * usually be handled more naturally using {@code 6081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * tryFind(iterable, predicate).or(defaultValue)}. 6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 6111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T find(Iterable<T> iterable, 6131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate, @Nullable T defaultValue) { 6141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.find(iterable.iterator(), predicate, defaultValue); 6151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 6181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an {@link Optional} containing the first element in {@code 6191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable} that satisfies the given predicate, if such an element exists. 6201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code 6221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * null}. If {@code null} is matched in {@code iterable}, a 6231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * NullPointerException will be thrown. 6241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 6261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 6271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Optional<T> tryFind(Iterable<T> iterable, 6281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate) { 6291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.tryFind(iterable.iterator(), predicate); 6301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 633bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the index in {@code iterable} of the first element that satisfies 634bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * the provided {@code predicate}, or {@code -1} if the Iterable has no such 635bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * elements. 636bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 637bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>More formally, returns the lowest index {@code i} such that 6381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code predicate.apply(Iterables.get(iterable, i))} returns {@code true}, 6391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or {@code -1} if there is no such index. 640bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 6411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 642bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 643bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> int indexOf( 644bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor Iterable<T> iterable, Predicate<? super T> predicate) { 645bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Iterators.indexOf(iterable.iterator(), predicate); 646bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 647bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 648bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 649090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable that applies {@code function} to each element of {@code 650090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * fromIterable}. 651090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 652090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} if the 653090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided iterator does. After a successful {@code remove()} call, 654090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code fromIterable} no longer contains the corresponding element. 6551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>If the input {@code Iterable} is known to be a {@code List} or other 6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code Collection}, consider {@link Lists#transform} and {@link 6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Collections2#transform}. 659090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 660090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <F, T> Iterable<T> transform(final Iterable<F> fromIterable, 661090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Function<? super F, ? extends T> function) { 662090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(fromIterable); 663090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(function); 664090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 6651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 666090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 667090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.transform(fromIterable.iterator(), function); 668090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 669090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 670090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 671090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 672090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 673090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the element at the specified position in an iterable. 674090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 675090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param position position of the element to return 676090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the element at the specified position in {@code iterable} 677090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IndexOutOfBoundsException if {@code position} is negative or 678090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * greater than or equal to the size of {@code iterable} 679090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 680090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T get(Iterable<T> iterable, int position) { 681090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 682090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof List) { 683090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((List<T>) iterable).get(position); 684090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 685090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 686090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof Collection) { 687090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Can check both ends 688090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<T> collection = (Collection<T>) iterable; 689090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Preconditions.checkElementIndex(position, collection.size()); 690090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } else { 691090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Can only check the lower end 6921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegativeIndex(position); 693090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 694090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.get(iterable.iterator(), position); 695090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 696090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static void checkNonnegativeIndex(int position) { 6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (position < 0) { 6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IndexOutOfBoundsException( 7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "position cannot be negative: " + position); 7011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 7051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the element at the specified position in an iterable or a default 7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value otherwise. 7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param position position of the element to return 7091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if {@code position} is 7101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * greater than or equal to the size of the iterable 7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the element at the specified position in {@code iterable} or 7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code defaultValue} if {@code iterable} contains fewer than 7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code position + 1} elements. 7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IndexOutOfBoundsException if {@code position} is negative 7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 4.0 7161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T get(Iterable<T> iterable, int position, 7181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Nullable T defaultValue) { 7191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegativeIndex(position); 7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 7231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return get(iterable, position); 7241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IndexOutOfBoundsException e) { 7251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return defaultValue; 7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 7301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the first element in {@code iterable} or {@code defaultValue} if 7311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iterable is empty. The {@link Iterators} analog to this method is 7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link Iterators#getNext}. 7331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if the iterable is empty 7351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the first element of {@code iterable} or the default value 7361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 7371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getFirst(Iterable<T> iterable, @Nullable T defaultValue) { 7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.getNext(iterable.iterator(), defaultValue); 7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 742090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 743090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the last element of {@code iterable}. 744090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 745090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the last element of {@code iterable} 7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws NoSuchElementException if the iterable is empty 747090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 748090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getLast(Iterable<T> iterable) { 7491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): Support a concurrently modified collection? 750090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof List) { 751090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list = (List<T>) iterable; 752090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (list.isEmpty()) { 753090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 754090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return getLastInNonemptyList(list); 756090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 757090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * TODO(kevinb): consider whether this "optimization" is worthwhile. Users 7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * with SortedSets tend to know they are SortedSets and probably would not 7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * call this method. 7621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 763090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof SortedSet) { 764090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson SortedSet<T> sortedSet = (SortedSet<T>) iterable; 765090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return sortedSet.last(); 766090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 767090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 768090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getLast(iterable.iterator()); 769090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 770090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 771bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 7721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the last element of {@code iterable} or {@code defaultValue} if 7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iterable is empty. 7741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the value to return if {@code iterable} is empty 7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the last element of {@code iterable} or the default value 7771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getLast(Iterable<T> iterable, @Nullable T defaultValue) { 7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Collection) { 7811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collection<T> collection = (Collection<T>) iterable; 7821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (collection.isEmpty()) { 7831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return defaultValue; 7841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof List) { 7881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert List<T> list = (List<T>) iterable; 7891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return getLastInNonemptyList(list); 7901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 7931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * TODO(kevinb): consider whether this "optimization" is worthwhile. Users 7941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * with SortedSets tend to know they are SortedSets and probably would not 7951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * call this method. 7961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof SortedSet) { 7981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SortedSet<T> sortedSet = (SortedSet<T>) iterable; 7991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return sortedSet.last(); 8001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.getLast(iterable.iterator(), defaultValue); 8031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> T getLastInNonemptyList(List<T> list) { 8061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return list.get(list.size() - 1); 8071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 8101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a view of {@code iterable} that skips its first 8111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code numberToSkip} elements. If {@code iterable} contains fewer than 8121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code numberToSkip} elements, the returned iterable skips all of its 8131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * elements. 8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Modifications to the underlying {@link Iterable} before a call to 8161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code iterator()} are reflected in the returned iterator. That is, the 8171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator skips the first {@code numberToSkip} elements that exist when the 8181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code Iterator} is created, not when {@code skip()} is called. 8191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The returned iterable's iterator supports {@code remove()} if the 8211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator of the underlying iterable supports it. Note that it is 8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <i>not</i> possible to delete the last skipped element by immediately 8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * calling {@code remove()} on that iterator, as the {@code Iterator} 8241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * contract states that a call to {@code remove()} before a call to 8251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code next()} will throw an {@link IllegalStateException}. 8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 8281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> skip(final Iterable<T> iterable, 8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final int numberToSkip) { 8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(numberToSkip >= 0, "number to skip cannot be negative"); 8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof List) { 8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final List<T> list = (List<T>) iterable; 8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): Support a concurrently modified collection? 8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (numberToSkip >= list.size()) 8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? Iterators.<T>emptyIterator() 8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : list.subList(numberToSkip, list.size()).iterator(); 8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<T> iterator = iterable.iterator(); 8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterators.skip(iterator, numberToSkip); 8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * We can't just return the iterator because an immediate call to its 8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * remove() method would remove one of the skipped elements instead of 8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * throwing an IllegalStateException. 8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Iterator<T>() { 8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert boolean atStart = true; 8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public boolean hasNext() { 8641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.hasNext(); 8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public T next() { 8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!hasNext()) { 8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new NoSuchElementException(); 8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.next(); 8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } finally { 8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert atStart = false; 8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void remove() { 8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (atStart) { 8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IllegalStateException(); 8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.remove(); 8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates an iterable with the first {@code limitSize} elements of the given 8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable. If the original iterable does not contain that many elements, the 8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned iterator will have the same behavior as the original iterable. The 8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned iterable's iterator supports {@code remove()} if the original 8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator does. 8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param iterable the iterable to limit 9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param limitSize the maximum number of elements in the returned iterator 9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IllegalArgumentException if {@code limitSize} is negative 9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 9031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> limit( 9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<T> iterable, final int limitSize) { 9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(limitSize >= 0, "limit is negative"); 9081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.limit(iterable.iterator(), limitSize); 9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 917bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns a view of the supplied iterable that wraps each generated 918bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link Iterator} through {@link Iterators#consumingIterator(Iterator)}. 919bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Note: If {@code iterable} is a {@link Queue}, the returned iterable will 9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * get entries from {@link Queue#remove()} since {@link Queue}'s iteration 9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * order is undefined. Calling {@link Iterator#hasNext()} on a generated 9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator from the returned iterable may cause an item to be immediately 9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * dequeued for return on a subsequent call to {@link Iterator#next()}. 9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 926bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param iterable the iterable to wrap 927bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return a view of the supplied iterable that wraps each generated iterator 9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * through {@link Iterators#consumingIterator(Iterator)}; for queues, 9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * an iterable that generates iterators that return and consume the 9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * queue's elements in queue order 931bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 932bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @see Iterators#consumingIterator(Iterator) 9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 934bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 935bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> Iterable<T> consumingIterable(final Iterable<T> iterable) { 9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Queue) { 9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Iterable<T>() { 9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new ConsumingQueueIterator<T>((Queue<T>) iterable); 9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 945bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkNotNull(iterable); 9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 947bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return new Iterable<T>() { 9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 949bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public Iterator<T> iterator() { 950bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Iterators.consumingIterator(iterable.iterator()); 951bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 952bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor }; 953bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 954bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 9551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ConsumingQueueIterator<T> extends AbstractIterator<T> { 9561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Queue<T> queue; 9571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private ConsumingQueueIterator(Queue<T> queue) { 9591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.queue = queue; 9601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public T computeNext() { 9631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return queue.remove(); 9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NoSuchElementException e) { 9661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return endOfData(); 9671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 971090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Methods only in Iterables, not in Iterators 972090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 973090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 974090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adapts a list to an iterable with reversed iteration order. It is 9751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * especially useful in foreach-style loops: <pre> {@code 976090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 977090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * List<String> mylist = ... 978090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * for (String str : Iterables.reverse(mylist)) { 979090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * ... 980090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * }}</pre> 981090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 982090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * There is no corresponding method in {@link Iterators}, since {@link 983090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterable#iterator} can simply be invoked on the result of calling this 984090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * method. 985090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 986090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable with the same elements as the list, in reverse 9871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 9881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated use {@link Lists#reverse(List)} or {@link 9891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * ImmutableList#reverse()}. <b>This method is scheduled for deletion in 9901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * July 2012.</b> 991090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 9921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated 993090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> reverse(final List<T> list) { 9941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Lists.reverse(list); 995090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 996090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 997090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 998090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Determines if the given iterable contains no elements. 999090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1000090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>There is no precise {@link Iterator} equivalent to this method, since 1001090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * one can only ask an iterator whether it has any elements <i>remaining</i> 1002090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * (which one does using {@link Iterator#hasNext}). 1003090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1004090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if the iterable contains no elements 1005090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 10061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static boolean isEmpty(Iterable<?> iterable) { 10071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Collection) { 10081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ((Collection<?>) iterable).isEmpty(); 10091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1010090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return !iterable.iterator().hasNext(); 1011090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1012090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1013bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor // Non-public 1014bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 1015090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1016090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes the specified element from the specified iterable. 1017090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1018090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method iterates over the iterable, checking each element returned 1019090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * by the iterator in turn to see if it equals the object {@code o}. If they 1020090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are equal, it is removed from the iterable with the iterator's 1021090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove} method. At most one element is removed, even if the iterable 1022090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * contains multiple members that equal {@code o}. 1023090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> Do not use this method for a collection, such as a 1025090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link HashSet}, that has a fast {@code remove} method. 1026090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1027090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable from which to remove 1028090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param o an element to remove from the collection 1029090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if the iterable changed as a result 1030090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException if the iterator does not support the 1031090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove} method and the iterable contains the object 1032090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1033090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static boolean remove(Iterable<?> iterable, @Nullable Object o) { 1034090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> i = iterable.iterator(); 1035090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (i.hasNext()) { 1036090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (Objects.equal(i.next(), o)) { 1037090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson i.remove(); 1038090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 1039090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1040090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1041090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 1042090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1043090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1044090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson abstract static class IterableWithToString<E> implements Iterable<E> { 1045090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 1046090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterables.toString(this); 1047090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1048090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 10491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 10511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterable over the merged contents of all given 10521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code iterables}. Equivalent entries will not be de-duplicated. 10531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Callers must ensure that the source {@code iterables} are in 10551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * non-descending order as this method does not sort its input. 10561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>For any equivalent elements across all {@code iterables}, it is 10581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * undefined which element is returned first. 10591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 10611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 10621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 10631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> mergeSorted( 10641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<? extends Iterable<? extends T>> iterables, 10651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Comparator<? super T> comparator) { 10661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterables, "iterables"); 10671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(comparator, "comparator"); 10681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<T> iterable = new Iterable<T>() { 10691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 10701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 10711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.mergeSorted( 10721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterables.transform(iterables, Iterables.<T>toIterator()), 10731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert comparator); 10741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 10761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterable<T>(iterable); 10771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(user): Is this the best place for this? Move to fluent functions? 10801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Useful as a public method? 10811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> Function<Iterable<? extends T>, Iterator<? extends T>> 10821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert toIterator() { 10831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Function<Iterable<? extends T>, Iterator<? extends T>>() { 10841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 10851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<? extends T> apply(Iterable<? extends T> iterable) { 10861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable.iterator(); 10871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 10891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1090090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 1091