|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.IteratorUtils
public class IteratorUtils
Provides static utility methods and decorators for Iterator
instances. The implementations are provided in the iterators subpackage.
Field Summary | |
---|---|
static ResettableIterator |
EMPTY_ITERATOR
An iterator over no elements. |
static ResettableListIterator |
EMPTY_LIST_ITERATOR
A list iterator over no elements. |
static MapIterator |
EMPTY_MAP_ITERATOR
A map iterator over no elements. |
static OrderedIterator |
EMPTY_ORDERED_ITERATOR
An ordered iterator over no elements. |
static OrderedMapIterator |
EMPTY_ORDERED_MAP_ITERATOR
An ordered map iterator over no elements. |
Constructor Summary | |
---|---|
IteratorUtils()
IteratorUtils is not normally instantiated. |
Method Summary | ||
---|---|---|
static
|
arrayIterator(E[] array)
Gets an iterator over an object array. |
|
static
|
arrayIterator(E[] array,
int start)
Gets an iterator over the end part of an object array. |
|
static
|
arrayIterator(E[] array,
int start,
int end)
Gets an iterator over part of an object array. |
|
static
|
arrayIterator(Object array)
Gets an iterator over an object or primitive array. |
|
static
|
arrayIterator(Object array,
int start)
Gets an iterator over the end part of an object or primitive array. |
|
static
|
arrayIterator(Object array,
int start,
int end)
Gets an iterator over part of an object or primitive array. |
|
static
|
arrayListIterator(E[] array)
Gets a list iterator over an object array. |
|
static
|
arrayListIterator(E[] array,
int start)
Gets a list iterator over the end part of an object array. |
|
static
|
arrayListIterator(E[] array,
int start,
int end)
Gets a list iterator over part of an object array. |
|
static
|
arrayListIterator(Object array)
Gets a list iterator over an object or primitive array. |
|
static
|
arrayListIterator(Object array,
int start)
Gets a list iterator over the end part of an object or primitive array. |
|
static
|
arrayListIterator(Object array,
int start,
int end)
Gets a list iterator over part of an object or primitive array. |
|
static
|
asEnumeration(Iterator<E> iterator)
Gets an enumeration that wraps an iterator. |
|
static
|
asIterator(Enumeration<E> enumeration)
Gets an iterator that provides an iterator view of the given enumeration. |
|
static
|
asIterator(Enumeration<E> enumeration,
Collection<E> removeCollection)
Gets an iterator that provides an iterator view of the given enumeration that will remove elements from the specified collection. |
|
static
|
chainedIterator(Collection<Iterator<? extends E>> iterators)
Gets an iterator that iterates through a collections of Iterator s
one after another. |
|
static
|
chainedIterator(Iterator<E>[] iterators)
Gets an iterator that iterates through an array of Iterator s
one after another. |
|
static
|
chainedIterator(Iterator<E> iterator1,
Iterator<E> iterator2)
Gets an iterator that iterates through two Iterator s
one after another. |
|
static
|
collatedIterator(Comparator<? super E> comparator,
Collection<Iterator<? extends E>> iterators)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of Iterator s. |
|
static
|
collatedIterator(Comparator<? super E> comparator,
Iterator<? extends E>[] iterators)
Gets an iterator that provides an ordered iteration over the elements contained in an array of Iterator s. |
|
static
|
collatedIterator(Comparator<? super E> comparator,
Iterator<? extends E> iterator1,
Iterator<? extends E> iterator2)
Gets an iterator that provides an ordered iteration over the elements contained in a collection of ordered Iterator s. |
|
static ResettableIterator |
emptyIterator()
Gets an empty iterator. |
|
static ResettableListIterator |
emptyListIterator()
Gets an empty list iterator. |
|
static MapIterator |
emptyMapIterator()
Gets an empty map iterator. |
|
static OrderedIterator |
emptyOrderedIterator()
Gets an empty ordered iterator. |
|
static OrderedMapIterator |
emptyOrderedMapIterator()
Gets an empty ordered map iterator. |
|
static
|
filteredIterator(Iterator<E> iterator,
Predicate<? super E> predicate)
Gets an iterator that filters another iterator. |
|
static
|
filteredListIterator(ListIterator<E> listIterator,
Predicate<? super E> predicate)
Gets a list iterator that filters another list iterator. |
|
static Iterator |
getIterator(Object obj)
Gets a suitable Iterator for the given object. |
|
static
|
loopingIterator(Collection<E> coll)
Gets an iterator that loops continuously over the supplied collection. |
|
static Iterator |
objectGraphIterator(Object root,
Transformer transformer)
Gets an iterator that operates over an object graph. |
|
static
|
singletonIterator(E object)
Gets a singleton iterator. |
|
static
|
singletonListIterator(E object)
Gets a singleton list iterator. |
|
static
|
toArray(Iterator<E> iterator)
Gets an array based on an iterator. |
|
static
|
toArray(Iterator<E> iterator,
Class<E> arrayClass)
Gets an array based on an iterator. |
|
static
|
toList(Iterator<E> iterator)
Gets a list based on an iterator. |
|
static
|
toList(Iterator<E> iterator,
int estimatedSize)
Gets a list based on an iterator. |
|
static
|
toListIterator(Iterator<E> iterator)
Gets a list iterator based on a simple iterator. |
|
static
|
transformedIterator(Iterator<I> iterator,
Transformer<I,O> transform)
Gets an iterator that transforms the elements of another iterator. |
|
static
|
unmodifiableIterator(Iterator<E> iterator)
Gets an immutable version of an Iterator . |
|
static
|
unmodifiableListIterator(ListIterator<E> listIterator)
Gets an immutable version of a ListIterator . |
|
static
|
unmodifiableMapIterator(MapIterator<K,V> mapIterator)
Gets an immutable version of a MapIterator . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final ResettableIterator EMPTY_ITERATOR
EmptyIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
public static final ResettableListIterator EMPTY_LIST_ITERATOR
EmptyListIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
public static final OrderedIterator EMPTY_ORDERED_ITERATOR
public static final MapIterator EMPTY_MAP_ITERATOR
public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR
Constructor Detail |
---|
public IteratorUtils()
Method Detail |
---|
public static ResettableIterator emptyIterator()
EmptyIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
public static ResettableListIterator emptyListIterator()
EmptyListIterator.INSTANCE
for compatability with Commons Collections 2.1.1.
public static OrderedIterator emptyOrderedIterator()
public static MapIterator emptyMapIterator()
public static OrderedMapIterator emptyOrderedMapIterator()
public static <E> ResettableIterator<E> singletonIterator(E object)
new SingletonIterator(object)
for compatability.
object
- the single object over which to iterate
public static <E> ListIterator<E> singletonListIterator(E object)
object
- the single object over which to iterate
public static <E> ResettableIterator<E> arrayIterator(E[] array)
new ArrayIterator(array)
for compatability.
array
- the array over which to iterate
NullPointerException
- if array is nullpublic static <E> ResettableIterator<E> arrayIterator(Object array)
array
- the array over which to iterate
IllegalArgumentException
- if the array is not an array
NullPointerException
- if array is nullpublic static <E> ResettableIterator<E> arrayIterator(E[] array, int start)
new ArrayIterator(array,start)
for compatability.
array
- the array over which to iteratestart
- the index to start iterating at
IndexOutOfBoundsException
- if start is less than zero or greater
than the length of the array
NullPointerException
- if array is nullpublic static <E> ResettableIterator<E> arrayIterator(Object array, int start)
array
- the array over which to iteratestart
- the index to start iterating at
IllegalArgumentException
- if the array is not an array
IndexOutOfBoundsException
- if start is less than zero or greater
than the length of the array
NullPointerException
- if array is nullpublic static <E> ResettableIterator<E> arrayIterator(E[] array, int start, int end)
new ArrayIterator(array,start,end)
for compatability.
array
- the array over which to iteratestart
- the index to start iterating atend
- the index to finish iterating at
IndexOutOfBoundsException
- if array bounds are invalid
IllegalArgumentException
- if end is before start
NullPointerException
- if array is nullpublic static <E> ResettableIterator<E> arrayIterator(Object array, int start, int end)
array
- the array over which to iteratestart
- the index to start iterating atend
- the index to finish iterating at
IllegalArgumentException
- if the array is not an array
IndexOutOfBoundsException
- if array bounds are invalid
IllegalArgumentException
- if end is before start
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(E[] array)
array
- the array over which to iterate
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(Object array)
array
- the array over which to iterate
IllegalArgumentException
- if the array is not an array
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start)
array
- the array over which to iteratestart
- the index to start iterating at
IndexOutOfBoundsException
- if start is less than zero
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(Object array, int start)
array
- the array over which to iteratestart
- the index to start iterating at
IllegalArgumentException
- if the array is not an array
IndexOutOfBoundsException
- if start is less than zero
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start, int end)
array
- the array over which to iteratestart
- the index to start iterating atend
- the index to finish iterating at
IndexOutOfBoundsException
- if array bounds are invalid
IllegalArgumentException
- if end is before start
NullPointerException
- if array is nullpublic static <E> ResettableListIterator<E> arrayListIterator(Object array, int start, int end)
array
- the array over which to iteratestart
- the index to start iterating atend
- the index to finish iterating at
IllegalArgumentException
- if the array is not an array
IndexOutOfBoundsException
- if array bounds are invalid
IllegalArgumentException
- if end is before start
NullPointerException
- if array is nullpublic static <E> Iterator<E> unmodifiableIterator(Iterator<E> iterator)
Iterator
. The returned object
will always throw an UnsupportedOperationException
for
the Iterator.remove()
method.
iterator
- the iterator to make immutable
public static <E> ListIterator<E> unmodifiableListIterator(ListIterator<E> listIterator)
ListIterator
. The returned object
will always throw an UnsupportedOperationException
for
the Iterator.remove()
, ListIterator.add(E)
and
ListIterator.set(E)
methods.
listIterator
- the iterator to make immutable
public static <K,V> MapIterator<K,V> unmodifiableMapIterator(MapIterator<K,V> mapIterator)
MapIterator
. The returned object
will always throw an UnsupportedOperationException
for
the Iterator.remove()
, MapIterator.setValue(Object)
methods.
mapIterator
- the iterator to make immutable
public static <E> Iterator<E> chainedIterator(Iterator<E> iterator1, Iterator<E> iterator2)
Iterator
s
one after another.
iterator1
- the first iterators to use, not nulliterator2
- the first iterators to use, not null
NullPointerException
- if either iterator is nullpublic static <E> Iterator<E> chainedIterator(Iterator<E>[] iterators)
Iterator
s
one after another.
iterators
- the iterators to use, not null or empty or contain nulls
NullPointerException
- if iterators array is null or contains a nullpublic static <E> Iterator<E> chainedIterator(Collection<Iterator<? extends E>> iterators)
Iterator
s
one after another.
iterators
- the iterators to use, not null or empty or contain nulls
NullPointerException
- if iterators collection is null or contains a null
ClassCastException
- if the iterators collection contains the wrong object typepublic static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E> iterator1, Iterator<? extends E> iterator2)
Iterator
s.
Given two ordered Iterator
s 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.
comparator
- the comparator to use, may be null for natural orderiterator1
- the first iterators to use, not nulliterator2
- the first iterators to use, not null
NullPointerException
- if either iterator is nullpublic static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E>[] iterators)
Iterator
s.
Given two ordered Iterator
s 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.
comparator
- the comparator to use, may be null for natural orderiterators
- the iterators to use, not null or empty or contain nulls
NullPointerException
- if iterators array is null or contains a nullpublic static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Collection<Iterator<? extends E>> iterators)
Iterator
s.
Given two ordered Iterator
s 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.
comparator
- the comparator to use, may be null for natural orderiterators
- the iterators to use, not null or empty or contain nulls
NullPointerException
- if iterators collection is null or contains a null
ClassCastException
- if the iterators collection contains the wrong object typepublic static Iterator objectGraphIterator(Object root, Transformer transformer)
Transformer
to extract the iterators and elements.
Its main benefit is that no intermediate List
is created.
For example, consider an object graph:
|- Branch -- Leaf | \- Leaf |- Tree | /- Leaf | |- Branch -- Leaf Forest | \- Leaf | |- Branch -- Leaf | | \- Leaf |- Tree | /- Leaf |- Branch -- Leaf |- Branch -- LeafThe following
Transformer
, used in this class, will extract all
the Leaf objects without creating a combined intermediate list:
public Object transform(Object input) { if (input instanceof Forest) { return ((Forest) input).treeIterator(); } if (input instanceof Tree) { return ((Tree) input).branchIterator(); } if (input instanceof Branch) { return ((Branch) input).leafIterator(); } if (input instanceof Leaf) { return input; } throw new ClassCastException(); }Internally, iteration starts from the root object. When next is called, the transformer is called to examine the object. The transformer will return either an iterator or an object. If the object is an Iterator, the next element from that iterator is obtained and the process repeats. If the element is an object it is returned. Under many circumstances, linking Iterators together in this manner is more efficient (and convenient) than using nested for loops to extract a list.
root
- the root object to start iterating from, null results in an empty iteratortransformer
- the transformer to use, see above, null uses no effect transformer
public static <I,O> Iterator<O> transformedIterator(Iterator<I> iterator, Transformer<I,O> transform)
iterator
- the iterator to use, not nulltransform
- the transform to use, not null
NullPointerException
- if either parameter is nullpublic static <E> Iterator<E> filteredIterator(Iterator<E> iterator, Predicate<? super E> predicate)
iterator
- the iterator to use, not nullpredicate
- the predicate to use as a filter, not null
NullPointerException
- if either parameter is nullpublic static <E> ListIterator<E> filteredListIterator(ListIterator<E> listIterator, Predicate<? super E> predicate)
listIterator
- the list iterator to use, not nullpredicate
- the predicate to use as a filter, not null
NullPointerException
- if either parameter is nullpublic static <E> ResettableIterator<E> loopingIterator(Collection<E> coll)
coll
- the collection to iterate over, not null
NullPointerException
- if the collection is nullpublic static <E> Iterator<E> asIterator(Enumeration<E> enumeration)
enumeration
- the enumeration to use
public static <E> Iterator<E> asIterator(Enumeration<E> enumeration, Collection<E> removeCollection)
enumeration
- the enumeration to useremoveCollection
- the collection to remove elements from
public static <E> Enumeration<E> asEnumeration(Iterator<E> iterator)
iterator
- the iterator to use, not null
NullPointerException
- if iterator is nullpublic static <E> ListIterator<E> toListIterator(Iterator<E> iterator)
iterator
- the iterator to use, not null
NullPointerException
- if iterator parameter is nullpublic static <E> E[] toArray(Iterator<E> iterator)
iterator
- the iterator to use, not null
NullPointerException
- if iterator parameter is nullpublic static <E> E[] toArray(Iterator<E> iterator, Class<E> arrayClass)
iterator
- the iterator to use, not nullarrayClass
- the class of array to create
NullPointerException
- if iterator parameter is null
NullPointerException
- if arrayClass is null
ClassCastException
- if the arrayClass is invalidpublic static <E> List<E> toList(Iterator<E> iterator)
iterator
- the iterator to use, not null
NullPointerException
- if iterator parameter is nullpublic static <E> List<E> toList(Iterator<E> iterator, int estimatedSize)
iterator
- the iterator to use, not nullestimatedSize
- the initial size of the ArrayList
NullPointerException
- if iterator parameter is null
IllegalArgumentException
- if the size is less than 1public static Iterator getIterator(Object obj)
obj
- the object to convert to an iterator
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |