com.google.inject.internal
Class Iterators

java.lang.Object
  extended by com.google.inject.internal.Iterators

public final class Iterators
extends java.lang.Object

This class contains static utility methods that operate on or return objects of type Iterator. Also see the parallel implementations in Iterables.

Author:
Kevin Bourrillion, Scott Bonneau

Method Summary
static
<T> java.util.Enumeration<T>
asEnumeration(java.util.Iterator<T> iterator)
          Adapts an Iterator to the Enumeration interface.
static
<T> java.util.Iterator<T>
concat(java.util.Iterator<? extends java.util.Iterator<? extends T>> inputs)
          Combines multiple iterators into a single iterator.
static
<T> UnmodifiableIterator<T>
emptyIterator()
          Returns the empty Iterator.
static
<T> java.util.ListIterator<T>
emptyListIterator()
          Returns the empty ListIterator.
static
<T> UnmodifiableIterator<T>
forArray(T... array)
          Returns an iterator containing the elements of array in order.
static
<T> UnmodifiableIterator<T>
forArray(T[] array, int offset, int length)
          Returns an iterator containing the elements in the specified range of array in order.
static
<T> T
getOnlyElement(java.util.Iterator<T> iterator)
          Returns the single element contained in iterator.
static
<T> UnmodifiableIterator<T>
singletonIterator(T value)
          Returns an iterator containing only value.
static java.lang.String toString(java.util.Iterator<?> iterator)
          Returns a string representation of iterator, with the format [e1, e2, ..., en].
static
<F,T> java.util.Iterator<T>
transform(java.util.Iterator<F> fromIterator, Function<? super F,? extends T> function)
          Returns an iterator that applies function to each element of fromIterator.
static
<T> UnmodifiableIterator<T>
unmodifiableIterator(java.util.Iterator<T> iterator)
          Returns an unmodifiable view of iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

emptyIterator

public static <T> UnmodifiableIterator<T> emptyIterator()
Returns the empty Iterator.


emptyListIterator

public static <T> java.util.ListIterator<T> emptyListIterator()
Returns the empty ListIterator.


unmodifiableIterator

public static <T> UnmodifiableIterator<T> unmodifiableIterator(java.util.Iterator<T> iterator)
Returns an unmodifiable view of iterator.


toString

public static java.lang.String toString(java.util.Iterator<?> iterator)
Returns a string representation of iterator, with the format [e1, e2, ..., en]. The iterator will be left exhausted: its hasNext() method will return false.


getOnlyElement

public static <T> T getOnlyElement(java.util.Iterator<T> iterator)
Returns the single element contained in iterator.

Throws:
java.util.NoSuchElementException - if the iterator is empty
java.lang.IllegalArgumentException - if the iterator contains multiple elements. The state of the iterator is unspecified.

concat

public static <T> java.util.Iterator<T> concat(java.util.Iterator<? extends java.util.Iterator<? extends T>> inputs)
Combines multiple iterators into a single iterator. The returned iterator iterates across the elements of each iterator in inputs. The input iterators are not polled until necessary.

The returned iterator supports remove() when the corresponding input iterator supports it. The methods of the returned iterator may throw NullPointerException if any of the input iterators are null.


transform

public static <F,T> java.util.Iterator<T> transform(java.util.Iterator<F> fromIterator,
                                                    Function<? super F,? extends T> function)
Returns an iterator that applies function to each element of fromIterator.

The returned iterator supports remove() if the provided iterator does. After a successful remove() call, fromIterator no longer contains the corresponding element.


forArray

public static <T> UnmodifiableIterator<T> forArray(T... array)
Returns an iterator containing the elements of array in 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.


forArray

public static <T> UnmodifiableIterator<T> forArray(T[] array,
                                                   int offset,
                                                   int length)
Returns an iterator containing the elements in the specified range of array in order. The returned iterator is a view of the array; subsequent changes to the array will be reflected in the iterator.

Parameters:
array - array to read elements out of
offset - index of first array element to retrieve
length - number of elements in iteration
Throws:
java.lang.IndexOutOfBoundsException - if offset is negative, length is negative, or offset + length > array.length

singletonIterator

public static <T> UnmodifiableIterator<T> singletonIterator(@Nullable
                                                            T value)
Returns an iterator containing only value.


asEnumeration

public static <T> java.util.Enumeration<T> asEnumeration(java.util.Iterator<T> iterator)
Adapts an Iterator to the Enumeration interface.

See Also:
Collections.enumeration(Collection)


Copyright © 2011. All Rights Reserved.