Class Iterators
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.Iterators
-
public final class Iterators extends Object
This class contains static utility methods that operate on or return objects of typeIterator. Except as noted, each method has a correspondingIterable-based method in theIterablesclass.Performance notes: Unless otherwise noted, all of the iterators produced in this class are lazy, which means that they only advance the backing iteration when absolutely necessary.
See the Guava User Guide section on
Iterators.- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Kevin Bourrillion, Jared Levy
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> booleanaddAll(Collection<T> addTo, Iterator<? extends T> iterator)Adds all elements initeratortocollection.static <T> booleanall(Iterator<T> iterator, Predicate<? super T> predicate)Returnstrueif every element returned byiteratorsatisfies the given predicate.static booleanelementsEqual(Iterator<?> iterator1, Iterator<?> iterator2)Determines whether two iterators contain equal elements in the same order.static <T> UnmodifiableIterator<T>emptyIterator()Deprecated.UseImmutableSet.<T>of().iterator()instead; or for Java 7 or later,Collections.emptyIterator().static <T> UnmodifiableIterator<T>forArray(T... array)Returns an iterator containing the elements ofarrayin order.static <T> TgetNext(Iterator<? extends T> iterator, T defaultValue)Returns the next element initeratorordefaultValueif the iterator is empty.static <T> PeekingIterator<T>peekingIterator(Iterator<? extends T> iterator)Returns aPeekingIteratorbacked by the given iterator.static booleanremoveAll(Iterator<?> removeFrom, Collection<?> elementsToRemove)Traverses an iterator and removes every element that belongs to the provided collection.static <T> booleanremoveIf(Iterator<T> removeFrom, Predicate<? super T> predicate)Removes every element that satisfies the provided predicate from the iterator.static <T> UnmodifiableIterator<T>singletonIterator(T value)Returns an iterator containing onlyvalue.static intsize(Iterator<?> iterator)Returns the number of elements remaining initerator.static <F,T>
Iterator<T>transform(Iterator<F> fromIterator, Function<? super F,? extends T> function)Returns an iterator that appliesfunctionto each element offromIterator.static <T> UnmodifiableIterator<T>unmodifiableIterator(Iterator<T> iterator)Returns an unmodifiable view ofiterator.
-
-
-
Method Detail
-
emptyIterator
@Deprecated public static <T> UnmodifiableIterator<T> emptyIterator()
Deprecated.UseImmutableSet.<T>of().iterator()instead; or for Java 7 or later,Collections.emptyIterator(). This method is scheduled for removal in May 2016.Returns the empty iterator.The
Iterableequivalent of this method isImmutableSet#of().
-
unmodifiableIterator
public static <T> UnmodifiableIterator<T> unmodifiableIterator(Iterator<T> iterator)
Returns an unmodifiable view ofiterator.
-
size
public static int size(Iterator<?> iterator)
Returns the number of elements remaining initerator. The iterator will be left exhausted: itshasNext()method will returnfalse.
-
removeAll
public static boolean removeAll(Iterator<?> removeFrom, Collection<?> elementsToRemove)
Traverses an iterator and removes every element that belongs to the provided collection. The iterator will be left exhausted: itshasNext()method will returnfalse.- Parameters:
removeFrom- the iterator to (potentially) remove elements fromelementsToRemove- the elements to remove- Returns:
trueif any element was removed fromiterator
-
removeIf
public static <T> boolean removeIf(Iterator<T> removeFrom, Predicate<? super T> predicate)
Removes every element that satisfies the provided predicate from the iterator. The iterator will be left exhausted: itshasNext()method will returnfalse.- Parameters:
removeFrom- the iterator to (potentially) remove elements frompredicate- a predicate that determines whether an element should be removed- Returns:
trueif any elements were removed from the iterator- Since:
- 2.0
-
elementsEqual
public static boolean elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2)
Determines whether two iterators contain equal elements in the same order. More specifically, this method returnstrueifiterator1anditerator2contain the same number of elements and every element ofiterator1is equal to the corresponding element ofiterator2.Note that this will modify the supplied iterators, since they will have been advanced some number of elements forward.
-
addAll
public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator)
Adds all elements initeratortocollection. The iterator will be left exhausted: itshasNext()method will returnfalse.- Returns:
trueifcollectionwas modified as a result of this operation
-
all
public static <T> boolean all(Iterator<T> iterator, Predicate<? super T> predicate)
Returnstrueif every element returned byiteratorsatisfies the given predicate. Ifiteratoris empty,trueis returned.
-
transform
public static <F,T> Iterator<T> transform(Iterator<F> fromIterator, Function<? super F,? extends T> function)
Returns an iterator that appliesfunctionto each element offromIterator.The returned iterator supports
remove()if the provided iterator does. After a successfulremove()call,fromIteratorno longer contains the corresponding element.
-
getNext
public static <T> T getNext(Iterator<? extends T> iterator, T defaultValue)
Returns the next element initeratorordefaultValueif the iterator is empty. TheIterablesanalog to this method isIterables.getFirst(java.lang.Iterable<? extends T>, T).- Parameters:
defaultValue- the default value to return if the iterator is empty- Returns:
- the next element of
iteratoror the default value - Since:
- 7.0
-
forArray
public static <T> UnmodifiableIterator<T> forArray(T... array)
Returns an iterator containing the elements ofarrayin order. The returned iterator is a view of the array; subsequent changes to the array will be reflected in the iterator.Note: It is often preferable to represent your data using a collection type, for example using
Arrays.asList(Object[]), making this method unnecessary.The
Iterableequivalent of this method is eitherArrays.asList(Object[]),ImmutableList#copyOf(Object[])}, orImmutableList#of.
-
singletonIterator
public static <T> UnmodifiableIterator<T> singletonIterator(T value)
Returns an iterator containing onlyvalue.The
Iterableequivalent of this method isCollections.singleton(T).
-
peekingIterator
public static <T> PeekingIterator<T> peekingIterator(Iterator<? extends T> iterator)
Returns aPeekingIteratorbacked by the given iterator.Calls to the
peekmethod with no intervening calls tonextdo not affect the iteration, and hence return the same object each time. A subsequent call tonextis guaranteed to return the same object again. For example:<p> PeekingIterator<String> peekingIterator = Iterators.peekingIterator(Iterators.forArray("a", "b")); String a1 = peekingIterator.peek(); // returns "a" String a2 = peekingIterator.peek(); // also returns "a" String a3 = peekingIterator.next(); // also returns "a"Any structural changes to the underlying iteration (aside from those performed by the iterator's own
PeekingIterator.remove()method) will leave the iterator in an undefined state.The returned iterator does not support removal after peeking, as explained by
PeekingIterator.remove().Note: If the given iterator is already a
PeekingIterator, it might be returned to the caller, although this is neither guaranteed to occur nor required to be consistent. For example, this method might choose to pass through recognized implementations ofPeekingIteratorwhen the behavior of the implementation is known to meet the contract guaranteed by this method.There is no
Iterableequivalent to this method, so use this method to wrap each individual iterator as it is generated.- Parameters:
iterator- the backing iterator. ThePeekingIteratorassumes ownership of this iterator, so users should cease making direct calls to it after calling this method.- Returns:
- a peeking iterator backed by that iterator. Apart from the
additional
PeekingIterator.peek()method, this iterator behaves exactly the same asiterator.
-
-