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.annotations.GwtIncompatible; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional; 28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions; 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections; 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.HashSet; 36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator; 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List; 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException; 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue; 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.RandomAccess; 41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Set; 42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.SortedSet; 43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable; 45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** 47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * This class contains static utility methods that operate on or return objects 48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of type {@code Iterable}. Except as noted, each method has a corresponding 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterator}-based method in the {@link Iterators} class. 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterables 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that their iterators 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * only advance the backing iteration when absolutely necessary. 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion 56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library) 58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Iterables { 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Iterables() {} 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns an unmodifiable view of {@code iterable}. */ 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> unmodifiableIterable( 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<T> iterable) { 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof UnmodifiableIterable || 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterable instanceof ImmutableCollection) { 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable; 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterable<T>(iterable); 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Simply returns its argument. 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated no need to use this 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated public static <E> Iterable<E> unmodifiableIterable( 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableCollection<E> iterable) { 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return checkNotNull(iterable); 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final class UnmodifiableIterable<T> implements Iterable<T> { 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Iterable<T> iterable; 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private UnmodifiableIterable(Iterable<T> iterable) { 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.iterable = iterable; 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.unmodifiableIterator(iterable.iterator()); 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public String toString() { 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable.toString(); 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // no equals and hashCode; it would break the contract! 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements in {@code iterable}. 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int size(Iterable<?> iterable) { 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (iterable instanceof Collection) 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) iterable).size() 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.size(iterable.iterator()); 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if {@code iterable} contains {@code element}; that is, 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * any object for which {@code equals(element)} is true. 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean contains(Iterable<?> iterable, @Nullable Object element) 118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson { 119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof Collection) { 120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<?> collection = (Collection<?>) iterable; 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson try { 122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return collection.contains(element); 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (NullPointerException e) { 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } catch (ClassCastException e) { 126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.contains(iterable.iterator(), element); 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that belongs to the provided 134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection. 135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method calls {@link Collection#removeAll} if {@code iterable} is a 137090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection, and {@link Iterators#removeAll} otherwise. 138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRemove the elements to remove 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterable} 142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean removeAll( 144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> removeFrom, Collection<?> elementsToRemove) { 145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (removeFrom instanceof Collection) 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) removeFrom).removeAll(checkNotNull(elementsToRemove)) 147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.removeAll(removeFrom.iterator(), elementsToRemove); 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that does not belong to the 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided collection. 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method calls {@link Collection#retainAll} if {@code iterable} is a 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection, and {@link Iterators#retainAll} otherwise. 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRetain the elements to retain 1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterable} 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean retainAll( 162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> removeFrom, Collection<?> elementsToRetain) { 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (removeFrom instanceof Collection) 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson ? ((Collection<?>) removeFrom).retainAll(checkNotNull(elementsToRetain)) 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson : Iterators.retainAll(removeFrom.iterator(), elementsToRetain); 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes, from an iterable, every element that satisfies the provided 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate. 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterable to (potentially) remove elements from 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param predicate a predicate that determines whether an element should 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * be removed 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if any elements were removed from the iterable 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException if the iterable does not support 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove()}. 1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 181bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> boolean removeIf( 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> removeFrom, Predicate<? super T> predicate) { 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (removeFrom instanceof RandomAccess && removeFrom instanceof List) { 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return removeIfFromRandomAccessList( 185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson (List<T>) removeFrom, checkNotNull(predicate)); 186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.removeIf(removeFrom.iterator(), predicate); 188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static <T> boolean removeIfFromRandomAccessList( 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list, Predicate<? super T> predicate) { 1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Note: Not all random access lists support set() so we need to deal with 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // those that don't and attempt the slower remove() based solution. 194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int from = 0; 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int to = 0; 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (; from < list.size(); from++) { 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T element = list.get(from); 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!predicate.apply(element)) { 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (from > to) { 2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.set(to, element); 2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (UnsupportedOperationException e) { 2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert slowRemoveIfForRemainingElements(list, predicate, to, from); 2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return true; 2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson to++; 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Clear the tail of any remaining items 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.subList(to, list.size()).clear(); 214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return from != to; 215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> void slowRemoveIfForRemainingElements(List<T> list, 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate, int to, int from) { 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Here we know that: 2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * (to < from) and that both are valid indices. 2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (index < to) should be kept. 2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (to <= index < from) should be removed. 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * The element with (index == from) should be kept. 2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // * Everything with (index > from) has not been checked yet. 2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Check from the end of the list backwards (minimize expected cost of 2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // moving elements when remove() is called). Stop before 'from' because 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // we already know that should be kept. 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int n = list.size() - 1; n > from; n--) { 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (predicate.apply(list.get(n))) { 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.remove(n); 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // And now remove everything in the range [to, from) (going backwards). 2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int n = from - 1; n >= to; n--) { 2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.remove(n); 2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Determines whether two iterables contain equal elements in the same order. 242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * More specifically, this method returns {@code true} if {@code iterable1} 243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * and {@code iterable2} contain the same number of elements and every element 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of {@code iterable1} is equal to the corresponding element of 245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterable2}. 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean elementsEqual( 248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<?> iterable1, Iterable<?> iterable2) { 249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.elementsEqual(iterable1.iterator(), iterable2.iterator()); 250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a string representation of {@code iterable}, with the format 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [e1, e2, ..., en]}. 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static String toString(Iterable<?> iterable) { 257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.toString(iterable.iterator()); 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterable}. 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if the iterable is empty 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterable contains multiple 265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements 266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement(Iterable<T> iterable) { 268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getOnlyElement(iterable.iterator()); 269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterable}, or {@code 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * defaultValue} if the iterable is empty. 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterator contains multiple 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement( 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, @Nullable T defaultValue) { 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getOnlyElement(iterable.iterator(), defaultValue); 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copies an iterable's elements into an array. 285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to copy 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param type the type of the elements 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a newly-allocated array into which all the elements of the iterable 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * have been copied 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @GwtIncompatible("Array.newInstance(Class, int)") 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collection<? extends T> collection = toCollection(iterable); 294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T[] array = ObjectArrays.newArray(type, collection.size()); 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return collection.toArray(array); 296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copies an iterable's elements into an array. 3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param iterable the iterable to copy 3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return a newly-allocated array into which all the elements of the iterable 3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * have been copied 3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert static Object[] toArray(Iterable<?> iterable) { 3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return toCollection(iterable).toArray(); 3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Converts an iterable into a collection. If the iterable is already a 3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * collection, it is returned. Otherwise, an {@link java.util.ArrayList} is 3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * created with the contents of the iterable in the same iteration order. 3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <E> Collection<E> toCollection(Iterable<E> iterable) { 3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (iterable instanceof Collection) 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? (Collection<E>) iterable 3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : Lists.newArrayList(iterable.iterator()); 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds all elements in {@code iterable} to {@code collection}. 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if {@code collection} was modified as a result of this 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * operation. 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean addAll( 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<T> addTo, Iterable<? extends T> elementsToAdd) { 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (elementsToAdd instanceof Collection) { 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collection<? extends T> c = Collections2.cast(elementsToAdd); 330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return addTo.addAll(c); 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.addAll(addTo, elementsToAdd.iterator()); 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 336090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements in the specified iterable that equal the 3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * specified object. This implementation avoids a full iteration when the 3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable is a {@link Multiset} or {@link Set}. 339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @see Collections#frequency 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int frequency(Iterable<?> iterable, @Nullable Object element) { 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if ((iterable instanceof Multiset)) { 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((Multiset<?>) iterable).count(element); 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if ((iterable instanceof Set)) { 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((Set<?>) iterable).contains(element) ? 1 : 0; 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.frequency(iterable.iterator(), element); 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable whose iterators cycle indefinitely over the elements of 354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterable}. 355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>That iterator supports {@code remove()} if {@code iterable.iterator()} 357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * does. After {@code remove()} is called, subsequent cycles omit the removed 358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element, which is no longer in {@code iterable}. The iterator's 359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method returns {@code true} until {@code iterable} is 360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * empty. 361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>To cycle over the iterable {@code n} times, use the following: 367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Iterables.concat(Collections.nCopies(n, iterable))} 368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> cycle(final Iterable<T> iterable) { 370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Iterable<T>() { 3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.cycle(iterable); 375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterable.toString() + " (cycled)"; 378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable whose iterators cycle indefinitely over the provided 384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements. 385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>After {@code remove} is invoked on a generated iterator, the removed 387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element will no longer appear in either that iterator or any other iterator 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * created from the same source iterable. That is, this method behaves exactly 389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * as {@code Iterables.cycle(Lists.newArrayList(elements))}. The iterator's 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext} method returns {@code true} until all of the original 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements have been removed. 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>To cycle over the elements {@code n} times, use the following: 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Iterables.concat(Collections.nCopies(n, Arrays.asList(elements)))} 399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> cycle(T... elements) { 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return cycle(Lists.newArrayList(elements)); 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines two iterables into a single iterable. The returned iterable has an 406090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator that traverses the elements in {@code a}, followed by the elements 407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in {@code b}. The source iterators are not polled until necessary. 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 413090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat( 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> a, Iterable<? extends T> b) { 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b)); 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 421090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines three 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}. The source 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterators are not polled until necessary. 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T> a, 431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> b, Iterable<? extends T> c) { 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c)); 436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines four iterables into a single iterable. The returned iterable has 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterator that traverses the elements in {@code a}, followed by the 441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements in {@code b}, followed by the elements in {@code c}, followed by 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the elements in {@code d}. The source iterators are not polled until 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * necessary. 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T> a, 450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> b, Iterable<? extends T> c, 451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<? extends T> d) { 452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(d); 456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c, d)); 457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterables into a single iterable. The returned iterable 461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * has an iterator that traverses the elements of each iterable in 462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code inputs}. The input iterators are not polled until necessary. 463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. 466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any of the provided iterables is null 468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat(Iterable<? extends T>... inputs) { 4701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return concat(ImmutableList.copyOf(inputs)); 471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterables into a single iterable. The returned iterable 475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * has an iterator that traverses the elements of each iterable in 476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code inputs}. The input iterators are not polled until necessary. 477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} when the 479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * corresponding input iterator supports it. The methods of the returned 480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable may throw {@code NullPointerException} if any of the input 4811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterators is null. 482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> concat( 4841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<? extends Iterable<? extends T>> inputs) { 4851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(inputs); 486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.concat(iterators(inputs)); 4901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 4921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 4951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterator over the iterators of the given iterables. 4961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 4971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> UnmodifiableIterator<Iterator<? extends T>> iterators( 4981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<? extends Iterable<? extends T>> iterables) { 4991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<? extends Iterable<? extends T>> iterableIterator = 5001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterables.iterator(); 5011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterator<Iterator<? extends T>>() { 5021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 5031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public boolean hasNext() { 5041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterableIterator.hasNext(); 5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 5071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<? extends T> next() { 5081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterableIterator.next().iterator(); 509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 513090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterable into unmodifiable sublists of the given size (the final 515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable may be smaller). For example, partitioning an iterable containing 516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code 517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * [[a, b, c], [d, e]]} -- an outer iterable containing two inner lists of 518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * three and two elements, all in the original order. 519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Iterators returned by the returned iterable do not support the {@link 521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterator#remove()} method. The returned lists implement {@link 522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * RandomAccess}, whether or not the input list does. 523090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Note:</b> if {@code iterable} is a {@link List}, use {@link 525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Lists#partition(List, int)} instead. 526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to return a partitioned view of 528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition (the last may be smaller) 529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable of unmodifiable lists containing the elements of {@code 530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable} divided into partitions 531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 532090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<List<T>> partition( 534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> iterable, final int size) { 535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(size > 0); 537090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<List<T>>() { 5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 539090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<List<T>> iterator() { 540090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.partition(iterable.iterator(), size); 541090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 542090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 543090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterable into unmodifiable sublists of the given size, padding 547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the final iterable with null values if necessary. For example, partitioning 548090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterable containing {@code [a, b, c, d, e]} with a partition size of 3 549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterable containing 550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * two inner lists of three elements each, all in the original order. 551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Iterators returned by the returned iterable do not support the {@link 553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterator#remove()} method. 554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable to return a partitioned view of 556090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition 557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable of unmodifiable lists containing the elements of {@code 558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable} divided into partitions (the final iterable may have 559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * trailing null elements) 560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<List<T>> paddedPartition( 563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> iterable, final int size) { 564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(size > 0); 566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<List<T>>() { 5671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<List<T>> iterator() { 569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.paddedPartition(iterable.iterator(), size); 570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the elements of {@code unfiltered} that satisfy a predicate. The 576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * resulting iterable's iterator does not support {@code remove()}. 577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> filter( 579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<T> unfiltered, final Predicate<? super T> predicate) { 580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(unfiltered); 581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 5831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.filter(unfiltered.iterator(), predicate); 586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns all instances of class {@code type} in {@code unfiltered}. The 592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * returned iterable has elements whose class is {@code type} or a subclass of 593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code type}. The returned iterable's iterator does not support 594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove()}. 595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param unfiltered an iterable containing objects of any type 597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param type the type of elements desired 598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an unmodifiable iterable containing all elements of the original 599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable that were of the requested type 600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @GwtIncompatible("Class.isInstance") 602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> filter( 603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterable<?> unfiltered, final Class<T> type) { 604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(unfiltered); 605090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(type); 606090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 6071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 609090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.filter(unfiltered.iterator(), type); 610090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 611090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 612090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 613090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 615090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if one or more elements in {@code iterable} satisfy 616090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the predicate. 617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean any( 619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, Predicate<? super T> predicate) { 620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.any(iterable.iterator(), predicate); 621090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 622090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 623090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 624090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if every element in {@code iterable} satisfies the 625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * predicate. If {@code iterable} is empty, {@code true} is returned. 626090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 627090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean all( 628090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterable<T> iterable, Predicate<? super T> predicate) { 629090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.all(iterable.iterator(), predicate); 630090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 631090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 632090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 633090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the first element in {@code iterable} that satisfies the given 6341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate; use this method only when such an element is known to exist. If 6351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * it is possible that <i>no</i> element will match, use {@link 6361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #tryFind)} or {@link #find(Iterable, Predicate, T)} instead. 637090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 638090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if no element in {@code iterable} matches 639090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the given predicate 640090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 641090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T find(Iterable<T> iterable, 642090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Predicate<? super T> predicate) { 643090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.find(iterable.iterator(), predicate); 644090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 645090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 646090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 6471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the first element in {@code iterable} that satisfies the given 6481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate, or {@code defaultValue} if none found. Note that this can 6491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * usually be handled more naturally using {@code 6501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * tryFind(iterable, predicate).or(defaultValue)}. 6511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 6531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 6541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T find(Iterable<T> iterable, 6551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate, @Nullable T defaultValue) { 6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.find(iterable.iterator(), predicate, defaultValue); 6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 6601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an {@link Optional} containing the first element in {@code 6611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable} that satisfies the given predicate, if such an element exists. 6621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code 6641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * null}. If {@code null} is matched in {@code iterable}, a 6651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * NullPointerException will be thrown. 6661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 6681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 6691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Optional<T> tryFind(Iterable<T> iterable, 6701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Predicate<? super T> predicate) { 6711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.tryFind(iterable.iterator(), predicate); 6721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 675bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the index in {@code iterable} of the first element that satisfies 676bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * the provided {@code predicate}, or {@code -1} if the Iterable has no such 677bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * elements. 678bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 679bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>More formally, returns the lowest index {@code i} such that 6801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code predicate.apply(Iterables.get(iterable, i))} returns {@code true}, 6811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or {@code -1} if there is no such index. 682bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 6831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 684bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 685bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> int indexOf( 686bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor Iterable<T> iterable, Predicate<? super T> predicate) { 687bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Iterators.indexOf(iterable.iterator(), predicate); 688bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 689bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 690bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 691090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterable that applies {@code function} to each element of {@code 692090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * fromIterable}. 693090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 694090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterable's iterator supports {@code remove()} if the 695090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided iterator does. After a successful {@code remove()} call, 696090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code fromIterable} no longer contains the corresponding element. 6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>If the input {@code Iterable} is known to be a {@code List} or other 6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code Collection}, consider {@link Lists#transform} and {@link 7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Collections2#transform}. 701090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 702090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <F, T> Iterable<T> transform(final Iterable<F> fromIterable, 703090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Function<? super F, ? extends T> function) { 704090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(fromIterable); 705090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(function); 706090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new IterableWithToString<T>() { 7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 708090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Iterator<T> iterator() { 709090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.transform(fromIterable.iterator(), function); 710090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 711090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 712090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 713090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 714090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 715090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the element at the specified position in an iterable. 716090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 717090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param position position of the element to return 718090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the element at the specified position in {@code iterable} 719090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IndexOutOfBoundsException if {@code position} is negative or 720090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * greater than or equal to the size of {@code iterable} 721090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 722090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T get(Iterable<T> iterable, int position) { 723090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 724090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof List) { 725090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return ((List<T>) iterable).get(position); 726090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 727090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 728090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof Collection) { 729090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Can check both ends 730090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<T> collection = (Collection<T>) iterable; 731090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Preconditions.checkElementIndex(position, collection.size()); 732090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } else { 733090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Can only check the lower end 7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegativeIndex(position); 735090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 736090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.get(iterable.iterator(), position); 737090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 738090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static void checkNonnegativeIndex(int position) { 7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (position < 0) { 7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IndexOutOfBoundsException( 7421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "position cannot be negative: " + position); 7431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 7471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the element at the specified position in an iterable or a default 7481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value otherwise. 7491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param position position of the element to return 7511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if {@code position} is 7521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * greater than or equal to the size of the iterable 7531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the element at the specified position in {@code iterable} or 7541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code defaultValue} if {@code iterable} contains fewer than 7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code position + 1} elements. 7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IndexOutOfBoundsException if {@code position} is negative 7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 4.0 7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T get(Iterable<T> iterable, int position, 7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Nullable T defaultValue) { 7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 7621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegativeIndex(position); 7631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 7651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return get(iterable, position); 7661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IndexOutOfBoundsException e) { 7671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return defaultValue; 7681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 7721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the first element in {@code iterable} or {@code defaultValue} if 7731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iterable is empty. The {@link Iterators} analog to this method is 7741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link Iterators#getNext}. 7751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if the iterable is empty 7771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the first element of {@code iterable} or the default value 7781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 7791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getFirst(Iterable<T> iterable, @Nullable T defaultValue) { 7811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.getNext(iterable.iterator(), defaultValue); 7821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 784090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 785090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the last element of {@code iterable}. 786090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 787090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the last element of {@code iterable} 7881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws NoSuchElementException if the iterable is empty 789090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 790090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getLast(Iterable<T> iterable) { 7911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): Support a concurrently modified collection? 792090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof List) { 793090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list = (List<T>) iterable; 794090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (list.isEmpty()) { 795090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 796090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 7971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return getLastInNonemptyList(list); 798090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 799090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 8001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 8011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * TODO(kevinb): consider whether this "optimization" is worthwhile. Users 8021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * with SortedSets tend to know they are SortedSets and probably would not 8031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * call this method. 8041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 805090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterable instanceof SortedSet) { 806090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson SortedSet<T> sortedSet = (SortedSet<T>) iterable; 807090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return sortedSet.last(); 808090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 809090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 810090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterators.getLast(iterable.iterator()); 811090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 812090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 813bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the last element of {@code iterable} or {@code defaultValue} if 8151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iterable is empty. 8161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the value to return if {@code iterable} is empty 8181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the last element of {@code iterable} or the default value 8191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 8201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getLast(Iterable<T> iterable, @Nullable T defaultValue) { 8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Collection) { 8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Collection<T> collection = (Collection<T>) iterable; 8241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (collection.isEmpty()) { 8251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return defaultValue; 8261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof List) { 8301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert List<T> list = (List<T>) iterable; 8311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return getLastInNonemptyList(list); 8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 8351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * TODO(kevinb): consider whether this "optimization" is worthwhile. Users 8361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * with SortedSets tend to know they are SortedSets and probably would not 8371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * call this method. 8381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof SortedSet) { 8401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert SortedSet<T> sortedSet = (SortedSet<T>) iterable; 8411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return sortedSet.last(); 8421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.getLast(iterable.iterator(), defaultValue); 8451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> T getLastInNonemptyList(List<T> list) { 8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return list.get(list.size() - 1); 8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a view of {@code iterable} that skips its first 8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code numberToSkip} elements. If {@code iterable} contains fewer than 8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code numberToSkip} elements, the returned iterable skips all of its 8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * elements. 8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Modifications to the underlying {@link Iterable} before a call to 8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code iterator()} are reflected in the returned iterator. That is, the 8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator skips the first {@code numberToSkip} elements that exist when the 8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code Iterator} is created, not when {@code skip()} is called. 8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The returned iterable's iterator supports {@code remove()} if the 8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator of the underlying iterable supports it. Note that it is 8641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <i>not</i> possible to delete the last skipped element by immediately 8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * calling {@code remove()} on that iterator, as the {@code Iterator} 8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * contract states that a call to {@code remove()} before a call to 8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code next()} will throw an {@link IllegalStateException}. 8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> skip(final Iterable<T> iterable, 8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final int numberToSkip) { 8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(numberToSkip >= 0, "number to skip cannot be negative"); 8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof List) { 8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final List<T> list = (List<T>) iterable; 8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): Support a concurrently modified collection? 8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (numberToSkip >= list.size()) 8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? Iterators.<T>emptyIterator() 8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : list.subList(numberToSkip, list.size()).iterator(); 8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<T> iterator = iterable.iterator(); 8931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterators.skip(iterator, numberToSkip); 8951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * We can't just return the iterator because an immediate call to its 8981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * remove() method would remove one of the skipped elements instead of 8991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * throwing an IllegalStateException. 9001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Iterator<T>() { 9021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert boolean atStart = true; 9031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public boolean hasNext() { 9061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.hasNext(); 9071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public T next() { 9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!hasNext()) { 9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new NoSuchElementException(); 9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.next(); 9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } finally { 9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert atStart = false; 9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void remove() { 9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (atStart) { 9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IllegalStateException(); 9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.remove(); 9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates an iterable with the first {@code limitSize} elements of the given 9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterable. If the original iterable does not contain that many elements, the 9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned iterator will have the same behavior as the original iterable. The 9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned iterable's iterator supports {@code remove()} if the original 9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator does. 9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param iterable the iterable to limit 9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param limitSize the maximum number of elements in the returned iterator 9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IllegalArgumentException if {@code limitSize} is negative 9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 9451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> limit( 9471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<T> iterable, final int limitSize) { 9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterable); 9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(limitSize >= 0, "limit is negative"); 9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new IterableWithToString<T>() { 9511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 9531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.limit(iterable.iterator(), limitSize); 9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 959bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns a view of the supplied iterable that wraps each generated 960bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@link Iterator} through {@link Iterators#consumingIterator(Iterator)}. 961bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 9621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Note: If {@code iterable} is a {@link Queue}, the returned iterable will 9631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * get entries from {@link Queue#remove()} since {@link Queue}'s iteration 9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * order is undefined. Calling {@link Iterator#hasNext()} on a generated 9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator from the returned iterable may cause an item to be immediately 9661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * dequeued for return on a subsequent call to {@link Iterator#next()}. 9671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 968bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param iterable the iterable to wrap 969bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return a view of the supplied iterable that wraps each generated iterator 9701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * through {@link Iterators#consumingIterator(Iterator)}; for queues, 9711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * an iterable that generates iterators that return and consume the 9721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * queue's elements in queue order 973bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 974bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @see Iterators#consumingIterator(Iterator) 9751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 976bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 977bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> Iterable<T> consumingIterable(final Iterable<T> iterable) { 9781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Queue) { 9791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Iterable<T>() { 9801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 9821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new ConsumingQueueIterator<T>((Queue<T>) iterable); 9831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 987bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkNotNull(iterable); 9881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 989bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return new Iterable<T>() { 9901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 991bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public Iterator<T> iterator() { 992bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Iterators.consumingIterator(iterable.iterator()); 993bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 994bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor }; 995bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 996bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 9971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class ConsumingQueueIterator<T> extends AbstractIterator<T> { 9981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private final Queue<T> queue; 9991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private ConsumingQueueIterator(Queue<T> queue) { 10011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.queue = queue; 10021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public T computeNext() { 10051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 10061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return queue.remove(); 10071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NoSuchElementException e) { 10081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return endOfData(); 10091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1013090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Methods only in Iterables, not in Iterators 1014090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1015090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1016090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adapts a list to an iterable with reversed iteration order. It is 10171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * especially useful in foreach-style loops: <pre> {@code 1018090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1019090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * List<String> mylist = ... 1020090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * for (String str : Iterables.reverse(mylist)) { 1021090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * ... 1022090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * }}</pre> 1023090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1024090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * There is no corresponding method in {@link Iterators}, since {@link 1025090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterable#iterator} can simply be invoked on the result of calling this 1026090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * method. 1027090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1028090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterable with the same elements as the list, in reverse 10291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated use {@link Lists#reverse(List)} or {@link 10311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * ImmutableList#reverse()}. <b>This method is scheduled for deletion in 10321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * July 2012.</b> 1033090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 10341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated 1035090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterable<T> reverse(final List<T> list) { 10361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Lists.reverse(list); 1037090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1038090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1039090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1040090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Determines if the given iterable contains no elements. 1041090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1042090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>There is no precise {@link Iterator} equivalent to this method, since 1043090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * one can only ask an iterator whether it has any elements <i>remaining</i> 1044090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * (which one does using {@link Iterator#hasNext}). 1045090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1046090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if the iterable contains no elements 1047090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 10481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static boolean isEmpty(Iterable<?> iterable) { 10491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterable instanceof Collection) { 10501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ((Collection<?>) iterable).isEmpty(); 10511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1052090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return !iterable.iterator().hasNext(); 1053090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1054090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1055bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor // Non-public 1056bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 1057090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1058090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes the specified element from the specified iterable. 1059090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1060090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method iterates over the iterable, checking each element returned 1061090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * by the iterator in turn to see if it equals the object {@code o}. If they 1062090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are equal, it is removed from the iterable with the iterator's 1063090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove} method. At most one element is removed, even if the iterable 1064090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * contains multiple members that equal {@code o}. 1065090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> Do not use this method for a collection, such as a 1067090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link HashSet}, that has a fast {@code remove} method. 1068090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1069090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterable the iterable from which to remove 1070090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param o an element to remove from the collection 1071090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if the iterable changed as a result 1072090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws UnsupportedOperationException if the iterator does not support the 1073090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code remove} method and the iterable contains the object 1074090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1075090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static boolean remove(Iterable<?> iterable, @Nullable Object o) { 1076090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> i = iterable.iterator(); 1077090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (i.hasNext()) { 1078090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (Objects.equal(i.next(), o)) { 1079090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson i.remove(); 1080090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 1081090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1082090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1083090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 1084090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1085090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1086090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson abstract static class IterableWithToString<E> implements Iterable<E> { 1087090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override public String toString() { 1088090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterables.toString(this); 1089090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1090090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 10911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 10931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterable over the merged contents of all given 10941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code iterables}. Equivalent entries will not be de-duplicated. 10951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Callers must ensure that the source {@code iterables} are in 10971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * non-descending order as this method does not sort its input. 10981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 10991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>For any equivalent elements across all {@code iterables}, it is 11001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * undefined which element is returned first. 11011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 11021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 11031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 11041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 11051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterable<T> mergeSorted( 11061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterable<? extends Iterable<? extends T>> iterables, 11071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Comparator<? super T> comparator) { 11081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterables, "iterables"); 11091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(comparator, "comparator"); 11101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<T> iterable = new Iterable<T>() { 11111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 11121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<T> iterator() { 11131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Iterators.mergeSorted( 11141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterables.transform(iterables, Iterables.<T>toIterator()), 11151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert comparator); 11161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 11171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 11181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new UnmodifiableIterable<T>(iterable); 11191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 11201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 11211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(user): Is this the best place for this? Move to fluent functions? 11221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Useful as a public method? 11231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <T> Function<Iterable<? extends T>, Iterator<? extends T>> 11241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert toIterator() { 11251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Function<Iterable<? extends T>, Iterator<? extends T>>() { 11261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 11271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<? extends T> apply(Iterable<? extends T> iterable) { 11281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterable.iterator(); 11291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 11301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 11311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 1133