1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License. 6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at 7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and 14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License. 15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect; 18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkArgument; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkState; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible; 25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtIncompatible; 26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function; 27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Objects; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Optional; 29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions; 30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate; 31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicates; 32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Arrays; 34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection; 35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collections; 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Comparator; 37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Enumeration; 38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator; 39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List; 40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.NoSuchElementException; 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.PriorityQueue; 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Queue; 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 {@link Iterator}. Except as noted, each method has a corresponding 49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterable}-based method in the {@link Iterables} class. 50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><i>Performance notes:</i> Unless otherwise noted, all of the iterators 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * produced in this class are <i>lazy</i>, which means that they only advance 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 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 Iterators { 61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private Iterators() {} 62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static final UnmodifiableIterator<Object> EMPTY_ITERATOR 64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson = new UnmodifiableIterator<Object>() { 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public Object next() { 71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the empty iterator. 77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The {@link Iterable} equivalent of this method is {@link 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * ImmutableSet#of()}. 80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Casting to any type is safe since there are no actual elements. 82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> emptyIterator() { 84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (UnmodifiableIterator<T>) EMPTY_ITERATOR; 85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static final Iterator<Object> EMPTY_MODIFIABLE_ITERATOR = 88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson new Iterator<Object>() { 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public boolean hasNext() { 90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 91090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 92090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public Object next() { 94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 95090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 96090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public void remove() { 98090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new IllegalStateException(); 99090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the empty {@code Iterator} that throws 104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link IllegalStateException} instead of 105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link UnsupportedOperationException} on a call to 106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@link Iterator#remove()}. 107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Casting to any type is safe since there are no actual elements. 109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static <T> Iterator<T> emptyModifiableIterator() { 111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (Iterator<T>) EMPTY_MODIFIABLE_ITERATOR; 112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** Returns an unmodifiable view of {@code iterator}. */ 115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> unmodifiableIterator( 116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterator<T> iterator) { 117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterator); 1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterator instanceof UnmodifiableIterator) { 1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (UnmodifiableIterator<T>) iterator; 1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<T>() { 1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.hasNext(); 125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.next(); 129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Simply returns its argument. 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated no need to use this 1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated public static <T> UnmodifiableIterator<T> unmodifiableIterator( 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert UnmodifiableIterator<T> iterator) { 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return checkNotNull(iterator); 1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements remaining in {@code iterator}. The iterator 146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * will be left exhausted: its {@code hasNext()} method will return 147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code false}. 148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int size(Iterator<?> iterator) { 150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int count = 0; 151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson iterator.next(); 153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson count++; 154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return count; 156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if {@code iterator} contains {@code element}. 160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean contains(Iterator<?> iterator, @Nullable Object element) 162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson { 163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (element == null) { 164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterator.next() == null) { 166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } else { 170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (element.equals(iterator.next())) { 172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Traverses an iterator and removes every element that belongs to the 181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided collection. The iterator will be left exhausted: its 182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method will return {@code false}. 183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterator to (potentially) remove elements from 185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRemove the elements to remove 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterator} 187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean removeAll( 189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> removeFrom, Collection<?> elementsToRemove) { 190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(elementsToRemove); 191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean modified = false; 192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (removeFrom.hasNext()) { 193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (elementsToRemove.contains(removeFrom.next())) { 194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom.remove(); 195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson modified = true; 196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return modified; 199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Removes every element that satisfies the provided predicate from the 203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator. The iterator will be left exhausted: its {@code hasNext()} 204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * method will return {@code false}. 205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterator to (potentially) remove elements from 207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param predicate a predicate that determines whether an element should 208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * be removed 209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if any elements were removed from the iterator 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 212bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> boolean removeIf( 213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> removeFrom, Predicate<? super T> predicate) { 214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean modified = false; 216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (removeFrom.hasNext()) { 217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (predicate.apply(removeFrom.next())) { 218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom.remove(); 219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson modified = true; 220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return modified; 223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Traverses an iterator and removes every element that does not belong to the 227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * provided collection. The iterator will be left exhausted: its 228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method will return {@code false}. 229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param removeFrom the iterator to (potentially) remove elements from 231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param elementsToRetain the elements to retain 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return {@code true} if any element was removed from {@code iterator} 233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean retainAll( 235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> removeFrom, Collection<?> elementsToRetain) { 236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(elementsToRetain); 237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean modified = false; 238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (removeFrom.hasNext()) { 239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!elementsToRetain.contains(removeFrom.next())) { 240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom.remove(); 241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson modified = true; 242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return modified; 245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Determines whether two iterators contain equal elements in the same order. 249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * More specifically, this method returns {@code true} if {@code iterator1} 250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * and {@code iterator2} contain the same number of elements and every element 251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * of {@code iterator1} is equal to the corresponding element of 252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterator2}. 253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Note that this will modify the supplied iterators, since they will have 255090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * been advanced some number of elements forward. 256090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 257090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static boolean elementsEqual( 258090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> iterator1, Iterator<?> iterator2) { 259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator1.hasNext()) { 260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!iterator2.hasNext()) { 261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Object o1 = iterator1.next(); 264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Object o2 = iterator2.next(); 265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!Objects.equal(o1, o2)) { 266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return !iterator2.hasNext(); 270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a string representation of {@code iterator}, with the format 274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [e1, e2, ..., en]}. The iterator will be left exhausted: its 275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method will return {@code false}. 276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static String toString(Iterator<?> iterator) { 278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!iterator.hasNext()) { 279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return "[]"; 280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson StringBuilder builder = new StringBuilder(); 282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.append('[').append(iterator.next()); 283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson builder.append(", ").append(iterator.next()); 285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return builder.append(']').toString(); 287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterator}. 291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 292090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if the iterator is empty 293090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterator contains multiple 294090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements. The state of the iterator is unspecified. 295090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 296090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement(Iterator<T> iterator) { 297090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T first = iterator.next(); 298090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!iterator.hasNext()) { 299090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return first; 300090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 301090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 302090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson StringBuilder sb = new StringBuilder(); 303090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson sb.append("expected one element but was: <" + first); 304090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (int i = 0; i < 4 && iterator.hasNext(); i++) { 305090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson sb.append(", " + iterator.next()); 306090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 307090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterator.hasNext()) { 308090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson sb.append(", ..."); 309090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert sb.append('>'); 311090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new IllegalArgumentException(sb.toString()); 313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 314090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 315090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 316090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the single element contained in {@code iterator}, or {@code 317090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * defaultValue} if the iterator is empty. 318090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 319090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if the iterator contains multiple 320090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements. The state of the iterator is unspecified. 321090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 322090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getOnlyElement( 323090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator, @Nullable T defaultValue) { 324090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.hasNext() ? getOnlyElement(iterator) : defaultValue; 325090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 326090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 327090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 328090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Copies an iterator's elements into an array. The iterator will be left 329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * exhausted: its {@code hasNext()} method will return {@code false}. 330090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 331090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterator the iterator to copy 332090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param type the type of the elements 333090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a newly-allocated array into which all the elements of the iterator 334090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * have been copied 335090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Array.newInstance(Class, int)") 337090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T[] toArray( 338090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> iterator, Class<T> type) { 339090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list = Lists.newArrayList(iterator); 340090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return Iterables.toArray(list, type); 341090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 342090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 343090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 344090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adds all elements in {@code iterator} to {@code collection}. The iterator 345090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * will be left exhausted: its {@code hasNext()} method will return 346090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code false}. 347090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return {@code true} if {@code collection} was modified as a result of this 349090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * operation 350090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 351090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean addAll( 352090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Collection<T> addTo, Iterator<? extends T> iterator) { 353090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(addTo); 354090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean wasModified = false; 355090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 356090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson wasModified |= addTo.add(iterator.next()); 357090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 358090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return wasModified; 359090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 360090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 361090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 362090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the number of elements in the specified iterator that equal the 363090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * specified object. The iterator will be left exhausted: its 364090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method will return {@code false}. 365090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 366090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @see Collections#frequency 367090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 368090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static int frequency(Iterator<?> iterator, @Nullable Object element) { 369090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int result = 0; 370090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (element == null) { 371090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 372090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterator.next() == null) { 373090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson result++; 374090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 375090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 376090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } else { 377090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 378090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (element.equals(iterator.next())) { 379090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson result++; 380090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 381090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 382090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 383090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return result; 384090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 385090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 386090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 387090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator that cycles indefinitely over the elements of {@code 388090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterable}. 389090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 390090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} if the provided iterator 391090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * does. After {@code remove()} is called, subsequent cycles omit the removed 392090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element, which is no longer in {@code iterable}. The iterator's 393090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method returns {@code true} until {@code iterable} is 394090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * empty. 395090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 396090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 397090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 398090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 399090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 400090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> cycle(final Iterable<T> iterable) { 401090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterable); 402090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Iterator<T>() { 403090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator = emptyIterator(); 404090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> removeFrom; 405090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 4061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 407090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 408090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!iterator.hasNext()) { 409090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson iterator = iterable.iterator(); 410090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 411090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.hasNext(); 412090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 4131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 414090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 415090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!hasNext()) { 416090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 417090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 418090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom = iterator; 419090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.next(); 420090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 4211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 422090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void remove() { 423090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkState(removeFrom != null, 424090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson "no calls to next() since last call to remove()"); 425090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom.remove(); 426090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom = null; 427090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 428090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 429090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 430090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 431090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 432090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator that cycles indefinitely over the provided elements. 433090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 434090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} if the provided iterator 435090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * does. After {@code remove()} is called, subsequent cycles omit the removed 436090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * element, but {@code elements} does not change. The iterator's 437090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code hasNext()} method returns {@code true} until all of the original 438090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * elements have been removed. 439090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 440090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an 441090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * infinite loop. You should use an explicit {@code break} or be certain that 442090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you will eventually remove all the elements. 443090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 444090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> cycle(T... elements) { 445090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return cycle(Lists.newArrayList(elements)); 446090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 447090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 448090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 449090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines two iterators into a single iterator. The returned iterator 450090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterates across the elements in {@code a}, followed by the elements in 451090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code b}. The source iterators are not polled until necessary. 452090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 453090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} when the corresponding 454090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * input iterator supports it. 455090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 456090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 457090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> concat(Iterator<? extends T> a, 458090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> b) { 459090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 460090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 461090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b).iterator()); 462090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 463090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 464090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 465090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines three iterators into a single iterator. The returned iterator 466090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterates across the elements in {@code a}, followed by the elements in 467090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code b}, followed by the elements in {@code c}. The source iterators 468090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * are not polled until necessary. 469090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 470090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} when the corresponding 471090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * input iterator supports it. 472090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 473090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 474090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> concat(Iterator<? extends T> a, 475090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> b, Iterator<? extends T> c) { 476090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 477090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 478090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 479090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c).iterator()); 480090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 481090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 482090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 483090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines four iterators into a single iterator. The returned iterator 484090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterates across the elements in {@code a}, followed by the elements in 485090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code b}, followed by the elements in {@code c}, followed by the elements 486090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * in {@code d}. The source iterators are not polled until necessary. 487090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 488090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} when the corresponding 489090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * input iterator supports it. 490090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 491090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 492090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> concat(Iterator<? extends T> a, 493090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> b, Iterator<? extends T> c, 494090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> d) { 495090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(a); 496090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(b); 497090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(c); 498090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(d); 499090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return concat(Arrays.asList(a, b, c, d).iterator()); 500090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 501090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 502090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 503090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterators into a single iterator. The returned iterator 504090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterates across the elements of each iterator in {@code inputs}. The input 505090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterators are not polled until necessary. 506090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 507090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} when the corresponding 508090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * input iterator supports it. 509090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 510090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NullPointerException if any of the provided iterators is null 511090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 512090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> concat(Iterator<? extends T>... inputs) { 5131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return concat(ImmutableList.copyOf(inputs).iterator()); 514090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 515090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 516090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 517090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Combines multiple iterators into a single iterator. The returned iterator 518090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterates across the elements of each iterator in {@code inputs}. The input 519090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterators are not polled until necessary. 520090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 521090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} when the corresponding 522090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * input iterator supports it. The methods of the returned iterator may throw 5231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code NullPointerException} if any of the input iterators is null. 524090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 525090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Iterator<T> concat( 526090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterator<? extends Iterator<? extends T>> inputs) { 527090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(inputs); 528090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Iterator<T>() { 529090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> current = emptyIterator(); 530090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> removeFrom; 531090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 5321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 533090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 534090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // http://code.google.com/p/google-collections/issues/detail?id=151 535090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // current.hasNext() might be relatively expensive, worth minimizing. 536090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean currentHasNext; 5371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // checkNotNull eager for GWT 5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // note: it must be here & not where 'current' is assigned, 5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // because otherwise we'll have called inputs.next() before throwing 5401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // the first NPE, and the next time around we'll call inputs.next() 5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // again, incorrectly moving beyond the error. 5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert while (!(currentHasNext = checkNotNull(current).hasNext()) 5431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert && inputs.hasNext()) { 544090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson current = inputs.next(); 545090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 546090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return currentHasNext; 547090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 5481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 549090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 550090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!hasNext()) { 551090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 552090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 553090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom = current; 554090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return current.next(); 555090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 5561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 557090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void remove() { 558090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkState(removeFrom != null, 559090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson "no calls to next() since last call to remove()"); 560090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom.remove(); 561090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson removeFrom = null; 562090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 563090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 564090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 565090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 566090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 567090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterator into unmodifiable sublists of the given size (the final 568090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * list may be smaller). For example, partitioning an iterator containing 569090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code [a, b, c, d, e]} with a partition size of 3 yields {@code 570090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * [[a, b, c], [d, e]]} -- an outer iterator containing two inner lists of 571090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * three and two elements, all in the original order. 572090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 573090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned lists implement {@link java.util.RandomAccess}. 574090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 575090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterator the iterator to return a partitioned view of 576090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition (the last may be smaller) 577090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterator of immutable lists containing the elements of {@code 578090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator} divided into partitions 579090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 580090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 581090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<List<T>> partition( 582090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator, int size) { 583090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return partitionImpl(iterator, size, false); 584090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 585090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 586090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 587090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Divides an iterator into unmodifiable sublists of the given size, padding 588090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the final iterator with null values if necessary. For example, partitioning 589090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * an iterator containing {@code [a, b, c, d, e]} with a partition size of 3 590090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * yields {@code [[a, b, c], [d, e, null]]} -- an outer iterator containing 591090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * two inner lists of three elements each, all in the original order. 592090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 593090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned lists implement {@link java.util.RandomAccess}. 594090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 595090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterator the iterator to return a partitioned view of 596090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param size the desired size of each partition 597090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an iterator of immutable lists containing the elements of {@code 598090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator} divided into partitions (the final iterable may have 599090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * trailing null elements) 600090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IllegalArgumentException if {@code size} is nonpositive 601090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 602090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<List<T>> paddedPartition( 603090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator, int size) { 604090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return partitionImpl(iterator, size, true); 605090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 606090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 607090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static <T> UnmodifiableIterator<List<T>> partitionImpl( 608090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterator<T> iterator, final int size, final boolean pad) { 609090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterator); 610090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(size > 0); 611090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<List<T>>() { 6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 613090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 614090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.hasNext(); 615090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 617090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public List<T> next() { 618090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!hasNext()) { 619090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 620090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 621090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Object[] array = new Object[size]; 622090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int count = 0; 623090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson for (; count < size && iterator.hasNext(); count++) { 624090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson array[count] = iterator.next(); 625090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 6261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (int i = count; i < size; i++) { 6271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert array[i] = null; // for GWT 6281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 629090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 630090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // we only put Ts in it 631090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson List<T> list = Collections.unmodifiableList( 632090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson (List<T>) Arrays.asList(array)); 6331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (pad || count == size) ? list : list.subList(0, count); 634090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 635090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 636090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 637090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 638090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 639090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the elements of {@code unfiltered} that satisfy a predicate. 640090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 641090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> filter( 642090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Iterator<T> unfiltered, final Predicate<? super T> predicate) { 643090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(unfiltered); 644090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 645090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new AbstractIterator<T>() { 646090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Override protected T computeNext() { 647090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (unfiltered.hasNext()) { 648090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T element = unfiltered.next(); 649090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (predicate.apply(element)) { 650090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return element; 651090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 652090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 653090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return endOfData(); 654090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 655090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 656090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 657090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 658090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 659090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns all instances of class {@code type} in {@code unfiltered}. The 660090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * returned iterator has elements whose class is {@code type} or a subclass of 661090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code type}. 662090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 663090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param unfiltered an iterator containing objects of any type 664090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param type the type of elements desired 665090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return an unmodifiable iterator containing all elements of the original 666090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * iterator that were of the requested type 667090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 668090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") // can cast to <T> because non-Ts are removed 669090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @GwtIncompatible("Class.isInstance") 670090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> filter( 671090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<?> unfiltered, Class<T> type) { 672090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return (UnmodifiableIterator<T>) 673090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson filter(unfiltered, Predicates.instanceOf(type)); 674090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 675090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 676090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 677090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if one or more elements returned by {@code iterator} 678090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * satisfy the given predicate. 679090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 680090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean any( 681090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator, Predicate<? super T> predicate) { 682090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 683090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 684090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T element = iterator.next(); 685090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (predicate.apply(element)) { 686090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 687090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 688090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 689090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 690090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 691090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 692090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 693090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns {@code true} if every element returned by {@code iterator} 694090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * satisfies the given predicate. If {@code iterator} is empty, {@code true} 695090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * is returned. 696090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 697090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> boolean all( 698090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<T> iterator, Predicate<? super T> predicate) { 699090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(predicate); 700090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 701090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T element = iterator.next(); 702090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!predicate.apply(element)) { 703090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return false; 704090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 705090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 706090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return true; 707090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 708090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 709090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 710090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns the first element in {@code iterator} that satisfies the given 7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate; use this method only when such an element is known to exist. If 7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * no such element is found, the iterator will be left exhausted: its {@code 7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * hasNext()} method will return {@code false}. If it is possible that 7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <i>no</i> element will match, use {@link #tryFind)} or {@link 7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #find(Iterator, Predicate, T)} instead. 716090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 717090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws NoSuchElementException if no element in {@code iterator} matches 718090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * the given predicate 719090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T find( 7211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterator<T> iterator, Predicate<? super T> predicate) { 722090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return filter(iterator, predicate).next(); 723090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 724090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 725090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 7261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the first element in {@code iterator} that satisfies the given 7271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * predicate. If no such element is found, {@code defaultValue} will be 7281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned from this method and the iterator will be left exhausted: its 7291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code hasNext()} method will return {@code false}. Note that this can 7301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * usually be handled more naturally using {@code 7311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * tryFind(iterator, predicate).or(defaultValue)}. 7321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 7341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T find(Iterator<T> iterator, Predicate<? super T> predicate, 7361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Nullable T defaultValue) { 7371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate); 7381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return filteredIterator.hasNext() ? filteredIterator.next() : defaultValue; 7391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 7421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an {@link Optional} containing the first element in {@code 7431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator} that satisfies the given predicate, if such an element exists. If 7441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * no such element is found, an empty {@link Optional} will be returned from 7451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * this method and the the iterator will be left exhausted: its {@code 7461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * hasNext()} method will return {@code false}. 7471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code 7491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * null}. If {@code null} is matched in {@code iterator}, a 7501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * NullPointerException will be thrown. 7511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 7521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 7531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 7541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Optional<T> tryFind( 7551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterator<T> iterator, Predicate<? super T> predicate) { 7561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert UnmodifiableIterator<T> filteredIterator = filter(iterator, predicate); 7571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return filteredIterator.hasNext() 7581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ? Optional.of(filteredIterator.next()) 7591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert : Optional.<T>absent(); 7601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 763bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the index in {@code iterator} of the first element that satisfies 764bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * the provided {@code predicate}, or {@code -1} if the Iterator has no such 765bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * elements. 766bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 767bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>More formally, returns the lowest index {@code i} such that 7681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code predicate.apply(Iterators.get(iterator, i))} returns {@code true}, 7691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or {@code -1} if there is no such index. 770bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 771bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>If -1 is returned, the iterator will be left exhausted: its 772bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@code hasNext()} method will return {@code false}. Otherwise, 773bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * the iterator will be set to the element which satisfies the 774bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@code predicate}. 775bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 7761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 777bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 778bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> int indexOf( 779bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor Iterator<T> iterator, Predicate<? super T> predicate) { 7801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(predicate, "predicate"); 781bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor int i = 0; 782bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor while (iterator.hasNext()) { 783bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor T current = iterator.next(); 784bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (predicate.apply(current)) { 785bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return i; 786bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 787bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor i++; 788bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 789bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return -1; 790bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 791bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 792bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 793090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator that applies {@code function} to each element of {@code 794090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * fromIterator}. 795090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 796090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator supports {@code remove()} if the provided iterator 797090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * does. After a successful {@code remove()} call, {@code fromIterator} no 798090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * longer contains the corresponding element. 799090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 800090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <F, T> Iterator<T> transform(final Iterator<F> fromIterator, 801090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Function<? super F, ? extends T> function) { 802090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(fromIterator); 803090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(function); 804090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Iterator<T>() { 8051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 806090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 807090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return fromIterator.hasNext(); 808090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 8091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 810090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 811090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson F from = fromIterator.next(); 812090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return function.apply(from); 813090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 8141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 815090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void remove() { 816090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson fromIterator.remove(); 817090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 818090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 819090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 820090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 821090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 8221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Advances {@code iterator} {@code position + 1} times, returning the 8231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * element at the {@code position}th position. 824090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 825090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param position position of the element to return 826090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the element at the specified position in {@code iterator} 827090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @throws IndexOutOfBoundsException if {@code position} is negative or 828090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * greater than or equal to the number of elements remaining in 829090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code iterator} 830090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 831090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T get(Iterator<T> iterator, int position) { 8321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegative(position); 833090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 834090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson int skipped = 0; 835090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (iterator.hasNext()) { 836090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T t = iterator.next(); 837090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (skipped++ == position) { 838090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return t; 839090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 840090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 841090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 842090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new IndexOutOfBoundsException("position (" + position 843090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson + ") must be less than the number of elements that remained (" 844090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson + skipped + ")"); 845090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 846090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 8471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static void checkNonnegative(int position) { 8481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (position < 0) { 8491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IndexOutOfBoundsException("position (" + position 8501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + ") must not be negative"); 8511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 8551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Advances {@code iterator} {@code position + 1} times, returning the 8561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * element at the {@code position}th position or {@code defaultValue} 8571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * otherwise. 8581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param position position of the element to return 8601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if the iterator is empty 8611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or if {@code position} is greater than the number of elements 8621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * remaining in {@code iterator} 8631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the element at the specified position in {@code iterator} or 8641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code defaultValue} if {@code iterator} produces fewer than 8651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code position + 1} elements. 8661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IndexOutOfBoundsException if {@code position} is negative 8671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 4.0 8681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T get(Iterator<T> iterator, int position, 8701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Nullable T defaultValue) { 8711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNonnegative(position); 8721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 8741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return get(iterator, position); 8751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IndexOutOfBoundsException e) { 8761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return defaultValue; 8771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 8801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 8811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the next element in {@code iterator} or {@code defaultValue} if 8821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the iterator is empty. The {@link Iterables} analog to this method is 8831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link Iterables#getFirst}. 8841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 8851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if the iterator is empty 8861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the next element of {@code iterator} or the default value 8871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 7.0 8881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 8891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getNext(Iterator<T> iterator, @Nullable T defaultValue) { 8901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.hasNext() ? iterator.next() : defaultValue; 8911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 8921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 893090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 894090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Advances {@code iterator} to the end, returning the last element. 895090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 896090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return the last element of {@code iterator} 8971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws NoSuchElementException if the iterator is empty 898090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 899090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> T getLast(Iterator<T> iterator) { 900090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson while (true) { 901090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson T current = iterator.next(); 902090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!iterator.hasNext()) { 903090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return current; 904090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 905090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 906090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 907090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 908bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 9091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Advances {@code iterator} to the end, returning the last element or 9101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code defaultValue} if the iterator is empty. 9111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 9121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param defaultValue the default value to return if the iterator is empty 9131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the last element of {@code iterator} 9141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 9151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> T getLast(Iterator<T> iterator, @Nullable T defaultValue) { 9171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.hasNext() ? getLast(iterator) : defaultValue; 9181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 9211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Calls {@code next()} on {@code iterator}, either {@code numberToSkip} times 9221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or until {@code hasNext()} returns {@code false}, whichever comes first. 9231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 9241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the number of elements skipped 9251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 9261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 9281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> int skip(Iterator<T> iterator, int numberToSkip) { 9291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterator); 9301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(numberToSkip >= 0, "number to skip cannot be negative"); 9311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert int i; 9331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (i = 0; i < numberToSkip && iterator.hasNext(); i++) { 9341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.next(); 9351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return i; 9371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 9401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Creates an iterator returning the first {@code limitSize} elements of the 9411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * given iterator. If the original iterator does not contain that many 9421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * elements, the returned iterator will have the same behavior as the original 9431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator. The returned iterator supports {@code remove()} if the original 9441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator does. 9451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 9461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param iterator the iterator to limit 9471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param limitSize the maximum number of elements in the returned iterator 9481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IllegalArgumentException if {@code limitSize} is negative 9491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 3.0 9501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 9511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> Iterator<T> limit( 9521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Iterator<T> iterator, final int limitSize) { 9531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterator); 9541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkArgument(limitSize >= 0, "limit is negative"); 9551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new Iterator<T>() { 9561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private int count; 9571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public boolean hasNext() { 9601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return count < limitSize && iterator.hasNext(); 9611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public T next() { 9651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (!hasNext()) { 9661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new NoSuchElementException(); 9671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert count++; 9691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator.next(); 9701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 9731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void remove() { 9741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.remove(); 9751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 9771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 9781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 9791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 980bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns a view of the supplied {@code iterator} that removes each element 981bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * from the supplied {@code iterator} as it is returned. 982bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 983bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>The provided iterator must support {@link Iterator#remove()} or 984bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * else the returned iterator will fail on the first call to {@code 985bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * next}. 986bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 987bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param iterator the iterator to remove and return elements from 988bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return an iterator that removes and returns elements from the 989bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * supplied iterator 9901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 991bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 992bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static <T> Iterator<T> consumingIterator(final Iterator<T> iterator) { 993bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkNotNull(iterator); 994bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return new UnmodifiableIterator<T>() { 9951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 996bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public boolean hasNext() { 997bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return iterator.hasNext(); 998bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 999bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 10001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1001bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public T next() { 1002bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor T next = iterator.next(); 1003bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor iterator.remove(); 1004bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return next; 1005bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 1006bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor }; 1007bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 1008bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 1009090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Methods only in Iterators, not in Iterables 1010090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1011090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 10121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Clears the iterator using its remove method. 10131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 10141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert static void clear(Iterator<?> iterator) { 10151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterator); 10161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert while (iterator.hasNext()) { 10171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.next(); 10181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.remove(); 10191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 10211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 10221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 1023090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator containing the elements of {@code array} in order. The 1024090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * returned iterator is a view of the array; subsequent changes to the array 1025090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * will be reflected in the iterator. 1026090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1027090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p><b>Note:</b> It is often preferable to represent your data using a 1028090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * collection type, for example using {@link Arrays#asList(Object[])}, making 1029090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * this method unnecessary. 1030090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1031090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The {@code Iterable} equivalent of this method is either {@link 10321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Arrays#asList(Object[])}, {@link ImmutableList#copyOf(Object[])}}, 10331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * or {@link ImmutableList#of}. 1034090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1035090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> forArray(final T... array) { 10361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): compare performance with Arrays.asList(array).iterator(). 1037bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkNotNull(array); // eager for GWT. 10381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new AbstractIndexedListIterator<T>(array.length) { 10391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override protected T get(int index) { 10401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return array[index]; 1041090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1042090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 1043090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1044090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1045090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1046090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator containing the elements in the specified range of 1047090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code array} in order. The returned iterator is a view of the array; 1048090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * subsequent changes to the array will be reflected in the iterator. 1049090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1050090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The {@code Iterable} equivalent of this method is {@code 1051090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Arrays.asList(array).subList(offset, offset + length)}. 1052090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1053090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param array array to read elements out of 1054090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param offset index of first array element to retrieve 1055090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param length number of elements in iteration 10561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws IndexOutOfBoundsException if {@code offset} is negative, {@code 10571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * length} is negative, or {@code offset + length > array.length} 1058090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1059090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson static <T> UnmodifiableIterator<T> forArray( 1060090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final T[] array, final int offset, int length) { 1061090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkArgument(length >= 0); 10621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert int end = offset + length; 1063090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1064090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Technically we should give a slightly more descriptive error on overflow 1065090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Preconditions.checkPositionIndexes(offset, end, array.length); 1066090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 10671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /* 10681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * We can't use call the two-arg constructor with arguments (offset, end) 10691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * because the returned Iterator is a ListIterator that may be moved back 10701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * past the beginning of the iteration. 10711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 10721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new AbstractIndexedListIterator<T>(length) { 10731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override protected T get(int index) { 10741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return array[offset + index]; 1075090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1076090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 1077090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1078090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1079090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1080090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns an iterator containing only {@code value}. 1081090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1082090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The {@link Iterable} equivalent of this method is {@link 1083090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Collections#singleton}. 1084090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1085090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> singletonIterator( 1086090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @Nullable final T value) { 1087090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<T>() { 1088090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson boolean done; 10891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1090090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 1091090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return !done; 1092090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 10931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1094090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 1095090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (done) { 1096090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson throw new NoSuchElementException(); 1097090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1098090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson done = true; 1099090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return value; 1100090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1101090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 1102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adapts an {@code Enumeration} to the {@code Iterator} interface. 1106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>This method has no equivalent in {@link Iterables} because viewing an 1108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Enumeration} as an {@code Iterable} is impossible. However, the 1109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * contents can be <i>copied</i> into a collection using {@link 1110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Collections#list}. 1111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> UnmodifiableIterator<T> forEnumeration( 1113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson final Enumeration<T> enumeration) { 1114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(enumeration); 1115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new UnmodifiableIterator<T>() { 11161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 1118090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return enumeration.hasMoreElements(); 1119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 11201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T next() { 1122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return enumeration.nextElement(); 1123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1124090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 1125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Adapts an {@code Iterator} to the {@code Enumeration} interface. 1129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The {@code Iterable} equivalent of this method is either {@link 1131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Collections#enumeration} (if you have a {@link Collection}), or 1132090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code Iterators.asEnumeration(collection.iterator())}. 1133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> Enumeration<T> asEnumeration(final Iterator<T> iterator) { 1135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkNotNull(iterator); 1136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new Enumeration<T>() { 11371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasMoreElements() { 1139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.hasNext(); 1140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 11411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public T nextElement() { 1143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.next(); 1144090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1145090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson }; 1146090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1147090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1148090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1149090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Implementation of PeekingIterator that avoids peeking unless necessary. 1150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private static class PeekingImpl<E> implements PeekingIterator<E> { 1152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private final Iterator<? extends E> iterator; 1154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private boolean hasPeeked; 1155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson private E peekedElement; 1156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public PeekingImpl(Iterator<? extends E> iterator) { 1158090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson this.iterator = checkNotNull(iterator); 1159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 11611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public boolean hasNext() { 1163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return hasPeeked || iterator.hasNext(); 1164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 11661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public E next() { 1168090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!hasPeeked) { 1169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return iterator.next(); 1170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson E result = peekedElement; 1172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson hasPeeked = false; 1173090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson peekedElement = null; 1174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return result; 1175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 11771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1178090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public void remove() { 1179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson checkState(!hasPeeked, "Can't remove after you've peeked at next"); 1180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson iterator.remove(); 1181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 11831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 1184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public E peek() { 1185090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (!hasPeeked) { 1186090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson peekedElement = iterator.next(); 1187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson hasPeeked = true; 1188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return peekedElement; 1190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson 1193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson /** 1194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Returns a {@code PeekingIterator} backed by the given iterator. 1195090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Calls to the {@code peek} method with no intervening calls to {@code 1197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * next} do not affect the iteration, and hence return the same object each 1198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * time. A subsequent call to {@code next} is guaranteed to return the same 1199090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * object again. For example: <pre> {@code 1200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * PeekingIterator<String> peekingIterator = 1202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Iterators.peekingIterator(Iterators.forArray("a", "b")); 1203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * String a1 = peekingIterator.peek(); // returns "a" 1204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * String a2 = peekingIterator.peek(); // also returns "a" 1205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * String a3 = peekingIterator.next(); // also returns "a"}</pre> 1206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1207090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Any structural changes to the underlying iteration (aside from those 1208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * performed by the iterator's own {@link PeekingIterator#remove()} method) 1209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * will leave the iterator in an undefined state. 1210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1211090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>The returned iterator does not support removal after peeking, as 1212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * explained by {@link PeekingIterator#remove()}. 1213090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>Note: If the given iterator is already a {@code PeekingIterator}, 1215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * it <i>might</i> be returned to the caller, although this is neither 1216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * guaranteed to occur nor required to be consistent. For example, this 1217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * method <i>might</i> choose to pass through recognized implementations of 1218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * {@code PeekingIterator} when the behavior of the implementation is 1219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * known to meet the contract guaranteed by this method. 1220090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * <p>There is no {@link Iterable} equivalent to this method, so use this 1222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * method to wrap each individual iterator as it is generated. 1223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * 1224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @param iterator the backing iterator. The {@link PeekingIterator} assumes 1225090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * ownership of this iterator, so users should cease making direct calls 1226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * to it after calling this method. 1227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @return a peeking iterator backed by that iterator. Apart from the 1228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * additional {@link PeekingIterator#peek()} method, this iterator behaves 1229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * exactly the same as {@code iterator}. 1230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */ 1231090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson public static <T> PeekingIterator<T> peekingIterator( 1232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson Iterator<? extends T> iterator) { 1233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson if (iterator instanceof PeekingImpl) { 1234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // Safe to cast <? extends T> to <T> because PeekingImpl only uses T 1235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson // covariantly (and cannot be subclassed to add non-covariant uses). 1236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson @SuppressWarnings("unchecked") 1237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson PeekingImpl<T> peeking = (PeekingImpl<T>) iterator; 1238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return peeking; 1239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 1240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson return new PeekingImpl<T>(iterator); 1241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson } 12421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 12441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Simply returns its argument. 12451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 12461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @deprecated no need to use this 12471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 12481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 12491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Deprecated public static <T> PeekingIterator<T> peekingIterator( 12501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert PeekingIterator<T> iterator) { 12511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return checkNotNull(iterator); 12521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 12531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 12551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterator over the merged contents of all given 12561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code iterators}, traversing every element of the input iterators. 12571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Equivalent entries will not be de-duplicated. 12581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 12591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Callers must ensure that the source {@code iterators} are in 12601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * non-descending order as this method does not sort its input. 12611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 12621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>For any equivalent elements across all {@code iterators}, it is 12631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * undefined which element is returned first. 12641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 12651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 11.0 12661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 12671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Beta 12681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <T> UnmodifiableIterator<T> mergeSorted( 12691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<? extends Iterator<? extends T>> iterators, 12701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super T> comparator) { 12711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(iterators, "iterators"); 12721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert checkNotNull(comparator, "comparator"); 12731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new MergingIterator<T>(iterators, comparator); 12751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 12761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 12781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * An iterator that performs a lazy N-way merge, calculating the next value 12791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * each time the iterator is polled. This amortizes the sorting cost over the 12801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iteration and requires less memory than sorting all elements at once. 12811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 12821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Retrieving a single element takes approximately O(log(M)) time, where M 12831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * is the number of iterators. (Retrieving all elements takes approximately 12841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * O(N*log(M)) time, where N is the total number of elements.) 12851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 12861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static class MergingIterator<T> extends AbstractIterator<T> { 12871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Queue<PeekingIterator<T>> queue; 12881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert final Comparator<? super T> comparator; 12891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public MergingIterator(Iterable<? extends Iterator<? extends T>> iterators, 12911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<? super T> itemComparator) { 12921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.comparator = itemComparator; 12931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 12941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // A comparator that's used by the heap, allowing the heap 12951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // to be sorted based on the top of each iterator. 12961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Comparator<PeekingIterator<T>> heapComparator = 12971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert new Comparator<PeekingIterator<T>>() { 12981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 12991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public int compare(PeekingIterator<T> o1, PeekingIterator<T> o2) { 13001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return comparator.compare(o1.peek(), o2.peek()); 13011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 13031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert queue = new PriorityQueue<PeekingIterator<T>>(2, heapComparator); 13051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert for (Iterator<? extends T> iterator : iterators) { 13071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterator.hasNext()) { 13081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert queue.add(Iterators.peekingIterator(iterator)); 13091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 13141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected T computeNext() { 13151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (queue.isEmpty()) { 13161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return endOfData(); 13171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert PeekingIterator<T> nextIter = queue.poll(); 13201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert T next = nextIter.next(); 13211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (nextIter.hasNext()) { 13231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert queue.add(nextIter); 13241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 13261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return next; 13271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 13281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1329090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson} 1330