org.apache.commons.collections

Class IteratorUtils


public class IteratorUtils
extends java.lang.Object

Provides static utility methods and decorators for Iterator instances. The implementations are provided in the org.apache.commons.collections.iterators subpackage.
Version:
$Id: IteratorUtils.java,v 1.4.2.2 2004/05/22 12:14:01 scolebourne Exp $
Author:
Stephen Colebourne
Since:
2.1

Field Summary

static Iterator
EMPTY_ITERATOR
Deprecated. Use EmptyIterator.INSTANCE
static ListIterator
EMPTY_LIST_ITERATOR
Deprecated. Use EmptyListIterator.INSTANCE

Method Summary

static Iterator
arrayIterator(Object[] array)
Deprecated. Use new ArrayIterator(array)
static Iterator
arrayIterator(Object[] array, int start)
Deprecated. Use new ArrayIterator(array,start)
static Iterator
arrayIterator(Object[] array, int start, int end)
Deprecated. Use new ArrayIterator(array,start,end)
static Enumeration
asEnumeration(Iterator iterator)
Gets an enumeration that wraps an iterator.
static Iterator
asIterator(Enumeration enumeration)
Gets an iterator that provides an iterator view of the given enumeration.
static Iterator
asIterator(Enumeration enumeration, Collection removeCollection)
Gets an iterator that provides an iterator view of the given enumeration that will remove elements from the specified collection.
static Iterator
chainedIterator(Collection iterators)
Gets an iterator that iterates through a collections of Iterators one after another.
static Iterator
chainedIterator(Iterator iterator1, Iterator iterator2)
Gets an iterator that iterates through two Iterators one after another.
static Iterator
chainedIterator(Iterator[] iterators)
Gets an iterator that iterates through an array of Iterators one after another.
static Iterator
collatedIterator(Comparator comparator, Collection iterators)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of Iterators.
static Iterator
collatedIterator(Comparator comparator, Iterator iterator1, Iterator iterator2)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of ordered Iterators.
static Iterator
collatedIterator(Comparator comparator, Iterator[] iterators)
Gets an iterator that provides an ordered iteration over the elements contained in an array of Iterators.
static Iterator
emptyIterator()
Deprecated. Use EmptyIterator.INSTANCE
static ListIterator
emptyListIterator()
Deprecated. Use EmptyListIterator.INSTANCE
static Iterator
filteredIterator(Iterator iterator, Predicate predicate)
Gets an iterator that filters another iterator.
static ListIterator
filteredListIterator(ListIterator listIterator, Predicate predicate)
Gets a list iterator that filters another list iterator.
static Iterator
getIterator(Object obj)
Gets a suitable Iterator for the given object.
static Iterator
singletonIterator(Object object)
Deprecated. Use new SingletonIterator(object)
static ListIterator
singletonListIterator(Object object)
Gets a singleton list iterator.
static Object[]
toArray(Iterator iterator)
Gets an array based on an iterator.
static Object[]
toArray(Iterator iterator, Class arrayClass)
Gets an array based on an iterator.
static List
toList(Iterator iterator)
Gets a list based on an iterator.
static List
toList(Iterator iterator, int estimatedSize)
Gets a list based on an iterator.
static ListIterator
toListIterator(Iterator iterator)
Gets a list iterator based on a simple iterator.
static Iterator
transformedIterator(Iterator iterator, Transformer transform)
Gets an iterator that transforms the elements of another iterator.

Field Details

EMPTY_ITERATOR

public static final Iterator EMPTY_ITERATOR

Deprecated. Use EmptyIterator.INSTANCE

An iterator over no elements.

EMPTY_LIST_ITERATOR

public static final ListIterator EMPTY_LIST_ITERATOR

Deprecated. Use EmptyListIterator.INSTANCE

A list iterator over no elements

Method Details

arrayIterator

public static Iterator arrayIterator(Object[] array)

Deprecated. Use new ArrayIterator(array)

Gets an iterator over an array.
Parameters:
array - the array over which to iterate
Returns:
an iterator over the array

arrayIterator

public static Iterator arrayIterator(Object[] array,
                                     int start)

Deprecated. Use new ArrayIterator(array,start)

Gets an iterator over the end part of an array.
Parameters:
array - the array over which to iterate
start - the index to start iterating at
Returns:
an iterator over part of the array

arrayIterator

public static Iterator arrayIterator(Object[] array,
                                     int start,
                                     int end)

Deprecated. Use new ArrayIterator(array,start,end)

Gets an iterator over part of an array.
Parameters:
array - the array over which to iterate
start - the index to start iterating at
end - the index to finish iterating at
Returns:
an iterator over part of the array

asEnumeration

public static Enumeration asEnumeration(Iterator iterator)
Gets an enumeration that wraps an iterator.
Parameters:
iterator - the iterator to use, not null

asIterator

public static Iterator asIterator(Enumeration enumeration)
Gets an iterator that provides an iterator view of the given enumeration.
Parameters:
enumeration - the enumeration to use

asIterator

public static Iterator asIterator(Enumeration enumeration,
                                  Collection removeCollection)
Gets an iterator that provides an iterator view of the given enumeration that will remove elements from the specified collection.
Parameters:
enumeration - the enumeration to use

chainedIterator

public static Iterator chainedIterator(Collection iterators)
Gets an iterator that iterates through a collections of Iterators one after another.
Parameters:
iterators - the iterators to use, not null or empty or contain nulls
Returns:
a combination iterator over the iterators

chainedIterator

public static Iterator chainedIterator(Iterator iterator1,
                                       Iterator iterator2)
Gets an iterator that iterates through two Iterators one after another.
Parameters:
iterator1 - the first iterators to use, not null
iterator2 - the first iterators to use, not null
Returns:
a combination iterator over the iterators

chainedIterator

public static Iterator chainedIterator(Iterator[] iterators)
Gets an iterator that iterates through an array of Iterators one after another.
Parameters:
iterators - the iterators to use, not null or empty or contain nulls
Returns:
a combination iterator over the iterators

collatedIterator

public static Iterator collatedIterator(Comparator comparator,
                                        Collection iterators)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of Iterators.

Given two ordered Iterators A and B, the Iterator.next() method will return the lesser of A.next() and B.next() and so on.

The comparator is optional. If null is specified then natural order is used.

Parameters:
comparator - the comparator to use, may be null for natural order
iterators - the iterators to use, not null or empty or contain nulls
Returns:
a combination iterator over the iterators

collatedIterator

public static Iterator collatedIterator(Comparator comparator,
                                        Iterator iterator1,
                                        Iterator iterator2)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of ordered Iterators.

Given two ordered Iterators A and B, the Iterator.next() method will return the lesser of A.next() and B.next().

The comparator is optional. If null is specified then natural order is used.

Parameters:
comparator - the comparator to use, may be null for natural order
iterator1 - the first iterators to use, not null
iterator2 - the first iterators to use, not null
Returns:
a combination iterator over the iterators

collatedIterator

public static Iterator collatedIterator(Comparator comparator,
                                        Iterator[] iterators)
Gets an iterator that provides an ordered iteration over the elements contained in an array of Iterators.

Given two ordered Iterators A and B, the Iterator.next() method will return the lesser of A.next() and B.next() and so on.

The comparator is optional. If null is specified then natural order is used.

Parameters:
comparator - the comparator to use, may be null for natural order
iterators - the iterators to use, not null or empty or contain nulls
Returns:
a combination iterator over the iterators

emptyIterator

public static Iterator emptyIterator()

Deprecated. Use EmptyIterator.INSTANCE

Gets an empty iterator.

This iterator is a valid iterator object that will iterate over nothing.

Returns:
an iterator over nothing

emptyListIterator

public static ListIterator emptyListIterator()

Deprecated. Use EmptyListIterator.INSTANCE

Gets an empty list iterator.

This iterator is a valid list iterator object that will iterate over nothing.

Returns:
a list iterator over nothing

filteredIterator

public static Iterator filteredIterator(Iterator iterator,
                                        Predicate predicate)
Gets an iterator that filters another iterator.

The returned iterator will only return objects that match the specified filtering predicate.

Parameters:
iterator - the iterator to use, not null
predicate - the predicate to use as a filter, not null

filteredListIterator

public static ListIterator filteredListIterator(ListIterator listIterator,
                                                Predicate predicate)
Gets a list iterator that filters another list iterator.

The returned iterator will only return objects that match the specified filtering predicate.

Parameters:
listIterator - the list iterator to use, not null
predicate - the predicate to use as a filter, not null

getIterator

public static Iterator getIterator(Object obj)
Gets a suitable Iterator for the given object.

This method can handles objects as follows

  • null - empty iterator
  • Iterator - returned directly
  • Enumeration - wrapped
  • Collection - iterator from collection returned
  • Map - values iterator returned
  • Dictionary - values (elements) enumeration returned as iterator
  • array - iterator over array returned
  • object with iterator() public method accessed by reflection
  • object - singleton iterator
Parameters:
obj - the object to convert to an iterator
Returns:
a suitable iterator, never null

singletonIterator

public static Iterator singletonIterator(Object object)

Deprecated. Use new SingletonIterator(object)

Gets a singleton iterator.

This iterator is a valid iterator object that will iterate over the specified object.

Parameters:
object - the single object over which to iterate
Returns:
a singleton iterator over the object

singletonListIterator

public static ListIterator singletonListIterator(Object object)
Gets a singleton list iterator.

This iterator is a valid list iterator object that will iterate over the specified object.

Parameters:
object - the single object over which to iterate
Returns:
a singleton list iterator over the object

toArray

public static Object[] toArray(Iterator iterator)
Gets an array based on an iterator.

As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, this is converted to an array.

Parameters:
iterator - the iterator to use, not null

toArray

public static Object[] toArray(Iterator iterator,
                               Class arrayClass)
Gets an array based on an iterator.

As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, this is converted to an array.

Parameters:
iterator - the iterator to use, not null
arrayClass - the class of array to create

toList

public static List toList(Iterator iterator)
Gets a list based on an iterator.

As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, the list is returned.

Parameters:
iterator - the iterator to use, not null

toList

public static List toList(Iterator iterator,
                          int estimatedSize)
Gets a list based on an iterator.

As the wrapped Iterator is traversed, an ArrayList of its values is created. At the end, the list is returned.

Parameters:
iterator - the iterator to use, not null
estimatedSize - the initial size of the ArrayList

toListIterator

public static ListIterator toListIterator(Iterator iterator)
Gets a list iterator based on a simple iterator.

As the wrapped Iterator is traversed, a LinkedList of its values is cached, permitting all required operations of ListIterator.

Parameters:
iterator - the iterator to use, not null

transformedIterator

public static Iterator transformedIterator(Iterator iterator,
                                           Transformer transform)
Gets an iterator that transforms the elements of another iterator.

The transformation occurs during the next() method and the underlying iterator is unaffected by the transformation.

Parameters:
iterator - the iterator to use, not null
transform - the transform to use, not null

Copyright © 2001-2004 Apache Software Foundation. Documenation generated ${TODAY}.