Author: bayard
Date: Tue Sep 15 05:55:33 2009
New Revision: 815061
URL: http://svn.apache.org/viewvc?rev=815061&view=rev
Log:
Merging from -r468106:814127 of collections_jdk5_branch - namely where this
code was generified; mostly in r738956.
Also see the following revisions:
------------------------------------------------------------------------
r753392 | mbenson | 2009-03-13 13:39:53 -0700 (Fri, 13 Mar 2009) | 1 line
revert IteratorUtils stuff; toList() works fine for this--duh
------------------------------------------------------------------------
r751850 | mbenson | 2009-03-09 14:34:44 -0700 (Mon, 09 Mar 2009) | 1 line
add iterable(Iterator)
------------------------------------------------------------------------
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/IteratorUtils.java
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/IteratorUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/IteratorUtils.java?rev=815061&r1=815060&r2=815061&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/java/org/apache/commons/collections/IteratorUtils.java
(original)
+++
commons/proper/collections/trunk/src/java/org/apache/commons/collections/IteratorUtils.java
Tue Sep 15 05:55:33 2009
@@ -55,7 +55,7 @@
import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
/**
- * Provides static utility methods and decorators for {...@link Iterator}
+ * Provides static utility methods and decorators for {...@link Iterator}
* instances. The implementations are provided in the iterators subpackage.
* <p>
* WARNING: Due to human error certain binary incompatabilities were introduced
@@ -81,26 +81,30 @@
* WARNING: This constant is binary incompatible with Commons Collections
2.1 and 2.1.1.
* Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons
Collections 2.1.1.
*/
- public static final ResettableIterator EMPTY_ITERATOR =
EmptyIterator.RESETTABLE_INSTANCE;
+ public static final ResettableIterator<Object> EMPTY_ITERATOR =
EmptyIterator.RESETTABLE_INSTANCE;
+
/**
* A list iterator over no elements.
* <p>
* WARNING: This constant is binary incompatible with Commons Collections
2.1 and 2.1.1.
* Use <code>EmptyListIterator.INSTANCE</code> for compatability with
Commons Collections 2.1.1.
*/
- public static final ResettableListIterator EMPTY_LIST_ITERATOR =
EmptyListIterator.RESETTABLE_INSTANCE;
+ public static final ResettableListIterator<Object> EMPTY_LIST_ITERATOR =
EmptyListIterator.RESETTABLE_INSTANCE;
+
/**
* An ordered iterator over no elements.
- */
- public static final OrderedIterator EMPTY_ORDERED_ITERATOR =
EmptyOrderedIterator.INSTANCE;
+ */
+ public static final OrderedIterator<Object> EMPTY_ORDERED_ITERATOR =
EmptyOrderedIterator.INSTANCE;
+
/**
* A map iterator over no elements.
- */
- public static final MapIterator EMPTY_MAP_ITERATOR =
EmptyMapIterator.INSTANCE;
+ */
+ public static final MapIterator<Object, Object> EMPTY_MAP_ITERATOR =
EmptyMapIterator.INSTANCE;
+
/**
* An ordered map iterator over no elements.
- */
- public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR =
EmptyOrderedMapIterator.INSTANCE;
+ */
+ public static final OrderedMapIterator<Object, Object>
EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE;
/**
* IteratorUtils is not normally instantiated.
@@ -121,14 +125,14 @@
*
* @return an iterator over nothing
*/
- public static ResettableIterator emptyIterator() {
- return EMPTY_ITERATOR;
+ public static <E> ResettableIterator<E> emptyIterator() {
+ return EmptyIterator.<E>getResettableInstance();
}
/**
* Gets an empty list iterator.
* <p>
- * This iterator is a valid list iterator object that will iterate
+ * This iterator is a valid list iterator object that will iterate
* over nothing.
* <p>
* WARNING: This method is binary incompatible with Commons Collections
2.1 and 2.1.1.
@@ -136,44 +140,44 @@
*
* @return a list iterator over nothing
*/
- public static ResettableListIterator emptyListIterator() {
- return EMPTY_LIST_ITERATOR;
+ public static <E> ResettableListIterator<E> emptyListIterator() {
+ return EmptyListIterator.<E>getResettableInstance();
}
/**
* Gets an empty ordered iterator.
* <p>
- * This iterator is a valid iterator object that will iterate
+ * This iterator is a valid iterator object that will iterate
* over nothing.
*
* @return an ordered iterator over nothing
*/
- public static OrderedIterator emptyOrderedIterator() {
- return EMPTY_ORDERED_ITERATOR;
+ public static <E> OrderedIterator<E> emptyOrderedIterator() {
+ return EmptyOrderedIterator.<E>getInstance();
}
/**
* Gets an empty map iterator.
* <p>
- * This iterator is a valid map iterator object that will iterate
+ * This iterator is a valid map iterator object that will iterate
* over nothing.
*
* @return a map iterator over nothing
*/
- public static MapIterator emptyMapIterator() {
- return EMPTY_MAP_ITERATOR;
+ public static <K, V> MapIterator<K, V> emptyMapIterator() {
+ return EmptyMapIterator.<K, V>getInstance();
}
/**
* Gets an empty ordered map iterator.
* <p>
- * This iterator is a valid map iterator object that will iterate
+ * This iterator is a valid map iterator object that will iterate
* over nothing.
*
* @return a map iterator over nothing
*/
- public static OrderedMapIterator emptyOrderedMapIterator() {
- return EMPTY_ORDERED_MAP_ITERATOR;
+ public static <K, V> OrderedMapIterator<K, V> emptyOrderedMapIterator() {
+ return EmptyOrderedMapIterator.<K, V>getInstance();
}
// Singleton
@@ -190,8 +194,8 @@
* @param object the single object over which to iterate
* @return a singleton iterator over the object
*/
- public static ResettableIterator singletonIterator(Object object) {
- return new SingletonIterator(object);
+ public static <E> ResettableIterator<E> singletonIterator(E object) {
+ return new SingletonIterator<E>(object);
}
/**
@@ -203,8 +207,8 @@
* @param object the single object over which to iterate
* @return a singleton list iterator over the object
*/
- public static ListIterator singletonListIterator(Object object) {
- return new SingletonListIterator(object);
+ public static <E> ListIterator<E> singletonListIterator(E object) {
+ return new SingletonListIterator<E>(object);
}
// Arrays
@@ -219,8 +223,8 @@
* @return an iterator over the array
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object[] array) {
- return new ObjectArrayIterator(array);
+ public static <E> ResettableIterator<E> arrayIterator(E[] array) {
+ return new ObjectArrayIterator<E>(array);
}
/**
@@ -234,8 +238,8 @@
* @throws IllegalArgumentException if the array is not an array
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object array) {
- return new ArrayIterator(array);
+ public static <E> ResettableIterator<E> arrayIterator(Object array) {
+ return new ArrayIterator<E>(array);
}
/**
@@ -251,8 +255,8 @@
* than the length of the array
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object[] array, int start) {
- return new ObjectArrayIterator(array, start);
+ public static <E> ResettableIterator<E> arrayIterator(E[] array, int
start) {
+ return new ObjectArrayIterator<E>(array, start);
}
/**
@@ -269,8 +273,8 @@
* than the length of the array
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object array, int start) {
- return new ArrayIterator(array, start);
+ public static <E> ResettableIterator<E> arrayIterator(Object array, int
start) {
+ return new ArrayIterator<E>(array, start);
}
/**
@@ -287,8 +291,8 @@
* @throws IllegalArgumentException if end is before start
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object[] array, int start,
int end) {
- return new ObjectArrayIterator(array, start, end);
+ public static <E> ResettableIterator<E> arrayIterator(E[] array, int
start, int end) {
+ return new ObjectArrayIterator<E>(array, start, end);
}
/**
@@ -306,8 +310,8 @@
* @throws IllegalArgumentException if end is before start
* @throws NullPointerException if array is null
*/
- public static ResettableIterator arrayIterator(Object array, int start,
int end) {
- return new ArrayIterator(array, start, end);
+ public static <E> ResettableIterator<E> arrayIterator(Object array, int
start, int end) {
+ return new ArrayIterator<E>(array, start, end);
}
//-----------------------------------------------------------------------
@@ -318,8 +322,8 @@
* @return a list iterator over the array
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object[] array) {
- return new ObjectArrayListIterator(array);
+ public static <E> ResettableListIterator<E> arrayListIterator(E[] array) {
+ return new ObjectArrayListIterator<E>(array);
}
/**
@@ -333,8 +337,8 @@
* @throws IllegalArgumentException if the array is not an array
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object array) {
- return new ArrayListIterator(array);
+ public static <E> ResettableListIterator<E> arrayListIterator(Object
array) {
+ return new ArrayListIterator<E>(array);
}
/**
@@ -346,8 +350,8 @@
* @throws IndexOutOfBoundsException if start is less than zero
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object[] array, int
start) {
- return new ObjectArrayListIterator(array, start);
+ public static <E> ResettableListIterator<E> arrayListIterator(E[] array,
int start) {
+ return new ObjectArrayListIterator<E>(array, start);
}
/**
@@ -363,8 +367,8 @@
* @throws IndexOutOfBoundsException if start is less than zero
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object array, int
start) {
- return new ArrayListIterator(array, start);
+ public static <E> ResettableListIterator<E> arrayListIterator(Object
array, int start) {
+ return new ArrayListIterator<E>(array, start);
}
/**
@@ -378,10 +382,10 @@
* @throws IllegalArgumentException if end is before start
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object[] array, int
start, int end) {
- return new ObjectArrayListIterator(array, start, end);
+ public static <E> ResettableListIterator<E> arrayListIterator(E[] array,
int start, int end) {
+ return new ObjectArrayListIterator<E>(array, start, end);
}
-
+
/**
* Gets a list iterator over part of an object or primitive array.
* <p>
@@ -397,10 +401,10 @@
* @throws IllegalArgumentException if end is before start
* @throws NullPointerException if array is null
*/
- public static ResettableListIterator arrayListIterator(Object array, int
start, int end) {
- return new ArrayListIterator(array, start, end);
+ public static <E> ResettableListIterator<E> arrayListIterator(Object
array, int start, int end) {
+ return new ArrayListIterator<E>(array, start, end);
}
-
+
// Unmodifiable
//-----------------------------------------------------------------------
/**
@@ -411,10 +415,10 @@
* @param iterator the iterator to make immutable
* @return an immutable version of the iterator
*/
- public static Iterator unmodifiableIterator(Iterator iterator) {
+ public static <E> Iterator<E> unmodifiableIterator(Iterator<E> iterator) {
return UnmodifiableIterator.decorate(iterator);
}
-
+
/**
* Gets an immutable version of a {...@link ListIterator}. The returned
object
* will always throw an {...@link UnsupportedOperationException} for
@@ -424,7 +428,7 @@
* @param listIterator the iterator to make immutable
* @return an immutable version of the iterator
*/
- public static ListIterator unmodifiableListIterator(ListIterator
listIterator) {
+ public static <E> ListIterator<E> unmodifiableListIterator(ListIterator<E>
listIterator) {
return UnmodifiableListIterator.decorate(listIterator);
}
@@ -436,14 +440,14 @@
* @param mapIterator the iterator to make immutable
* @return an immutable version of the iterator
*/
- public static MapIterator unmodifiableMapIterator(MapIterator mapIterator)
{
+ public static <K, V> MapIterator<K, V>
unmodifiableMapIterator(MapIterator<K, V> mapIterator) {
return UnmodifiableMapIterator.decorate(mapIterator);
}
// Chained
//-----------------------------------------------------------------------
/**
- * Gets an iterator that iterates through two {...@link Iterator}s
+ * Gets an iterator that iterates through two {...@link Iterator}s
* one after another.
*
* @param iterator1 the first iterators to use, not null
@@ -451,24 +455,24 @@
* @return a combination iterator over the iterators
* @throws NullPointerException if either iterator is null
*/
- public static Iterator chainedIterator(Iterator iterator1, Iterator
iterator2) {
- return new IteratorChain(iterator1, iterator2);
+ public static <E> Iterator<E> chainedIterator(Iterator<? extends E>
iterator1, Iterator<? extends E> iterator2) {
+ return new IteratorChain<E>(iterator1, iterator2);
}
/**
- * Gets an iterator that iterates through an array of {...@link Iterator}s
+ * Gets an iterator that iterates through an array of {...@link Iterator}s
* one after another.
*
* @param iterators the iterators to use, not null or empty or contain
nulls
* @return a combination iterator over the iterators
* @throws NullPointerException if iterators array is null or contains a
null
*/
- public static Iterator chainedIterator(Iterator[] iterators) {
- return new IteratorChain(iterators);
+ public static <E> Iterator<E> chainedIterator(Iterator<? extends E>[]
iterators) {
+ return new IteratorChain<E>(iterators);
}
/**
- * Gets an iterator that iterates through a collections of {...@link
Iterator}s
+ * Gets an iterator that iterates through a collections of {...@link
Iterator}s
* one after another.
*
* @param iterators the iterators to use, not null or empty or contain
nulls
@@ -476,8 +480,8 @@
* @throws NullPointerException if iterators collection is null or
contains a null
* @throws ClassCastException if the iterators collection contains the
wrong object type
*/
- public static Iterator chainedIterator(Collection iterators) {
- return new IteratorChain(iterators);
+ public static <E> Iterator<E> chainedIterator(Collection<Iterator<?
extends E>> iterators) {
+ return new IteratorChain<E>(iterators);
}
// Collated
@@ -487,7 +491,7 @@
* contained in a collection of ordered {...@link Iterator}s.
* <p>
* Given two ordered {...@link Iterator}s <code>A</code> and
<code>B</code>,
- * the {...@link Iterator#next()} method will return the lesser of
+ * the {...@link Iterator#next()} method will return the lesser of
* <code>A.next()</code> and <code>B.next()</code>.
* <p>
* The comparator is optional. If null is specified then natural order is
used.
@@ -498,8 +502,8 @@
* @return a combination iterator over the iterators
* @throws NullPointerException if either iterator is null
*/
- public static Iterator collatedIterator(Comparator comparator, Iterator
iterator1, Iterator iterator2) {
- return new CollatingIterator(comparator, iterator1, iterator2);
+ public static <E> Iterator<E> collatedIterator(Comparator<? super E>
comparator, Iterator<? extends E> iterator1, Iterator<? extends E> iterator2) {
+ return new CollatingIterator<E>(comparator, iterator1, iterator2);
}
/**
@@ -507,7 +511,7 @@
* contained in an array of {...@link Iterator}s.
* <p>
* Given two ordered {...@link Iterator}s <code>A</code> and
<code>B</code>,
- * the {...@link Iterator#next()} method will return the lesser of
+ * the {...@link Iterator#next()} method will return the lesser of
* <code>A.next()</code> and <code>B.next()</code> and so on.
* <p>
* The comparator is optional. If null is specified then natural order is
used.
@@ -517,8 +521,8 @@
* @return a combination iterator over the iterators
* @throws NullPointerException if iterators array is null or contains a
null
*/
- public static Iterator collatedIterator(Comparator comparator, Iterator[]
iterators) {
- return new CollatingIterator(comparator, iterators);
+ public static <E> Iterator<E> collatedIterator(Comparator<? super E>
comparator, Iterator<? extends E>[] iterators) {
+ return new CollatingIterator<E>(comparator, iterators);
}
/**
@@ -526,7 +530,7 @@
* contained in a collection of {...@link Iterator}s.
* <p>
* Given two ordered {...@link Iterator}s <code>A</code> and
<code>B</code>,
- * the {...@link Iterator#next()} method will return the lesser of
+ * the {...@link Iterator#next()} method will return the lesser of
* <code>A.next()</code> and <code>B.next()</code> and so on.
* <p>
* The comparator is optional. If null is specified then natural order is
used.
@@ -537,10 +541,11 @@
* @throws NullPointerException if iterators collection is null or
contains a null
* @throws ClassCastException if the iterators collection contains the
wrong object type
*/
- public static Iterator collatedIterator(Comparator comparator, Collection
iterators) {
- return new CollatingIterator(comparator, iterators);
+ public static <E> Iterator<E> collatedIterator(Comparator<? super E>
comparator,
+ Collection<Iterator<? extends E>> iterators) {
+ return new CollatingIterator<E>(comparator, iterators);
}
-
+
// Object Graph
//-----------------------------------------------------------------------
/**
@@ -590,16 +595,16 @@
* <p>
* Under many circumstances, linking Iterators together in this manner is
* more efficient (and convenient) than using nested for loops to extract
a list.
- *
+ *
* @param root the root object to start iterating from, null results in
an empty iterator
* @param transformer the transformer to use, see above, null uses no
effect transformer
* @return a new object graph iterator
* @since Commons Collections 3.1
*/
- public static Iterator objectGraphIterator(Object root, Transformer
transformer) {
- return new ObjectGraphIterator(root, transformer);
+ public static <E> Iterator<E> objectGraphIterator(E root, Transformer<?
super E, ? extends E> transformer) {
+ return new ObjectGraphIterator<E>(root, transformer);
}
-
+
// Transformed
//-----------------------------------------------------------------------
/**
@@ -613,16 +618,16 @@
* @return a new transforming iterator
* @throws NullPointerException if either parameter is null
*/
- public static Iterator transformedIterator(Iterator iterator, Transformer
transform) {
+ public static <I, O> Iterator<O> transformedIterator(Iterator<? extends I>
iterator, Transformer<? super I, ? extends O> transform) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (transform == null) {
throw new NullPointerException("Transformer must not be null");
}
- return new TransformIterator(iterator, transform);
+ return new TransformIterator<I, O>(iterator, transform);
}
-
+
// Filtered
//-----------------------------------------------------------------------
/**
@@ -636,16 +641,16 @@
* @return a new filtered iterator
* @throws NullPointerException if either parameter is null
*/
- public static Iterator filteredIterator(Iterator iterator, Predicate
predicate) {
+ public static <E> Iterator<E> filteredIterator(Iterator<? extends E>
iterator, Predicate<? super E> predicate) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (predicate == null) {
throw new NullPointerException("Predicate must not be null");
}
- return new FilterIterator(iterator, predicate);
+ return new FilterIterator<E>(iterator, predicate);
}
-
+
/**
* Gets a list iterator that filters another list iterator.
* <p>
@@ -657,16 +662,16 @@
* @return a new filtered iterator
* @throws NullPointerException if either parameter is null
*/
- public static ListIterator filteredListIterator(ListIterator listIterator,
Predicate predicate) {
+ public static <E> ListIterator<E> filteredListIterator(ListIterator<?
extends E> listIterator, Predicate<? super E> predicate) {
if (listIterator == null) {
throw new NullPointerException("ListIterator must not be null");
}
if (predicate == null) {
throw new NullPointerException("Predicate must not be null");
}
- return new FilterListIterator(listIterator, predicate);
+ return new FilterListIterator<E>(listIterator, predicate);
}
-
+
// Looping
//-----------------------------------------------------------------------
/**
@@ -680,13 +685,13 @@
* @return a new looping iterator
* @throws NullPointerException if the collection is null
*/
- public static ResettableIterator loopingIterator(Collection coll) {
+ public static <E> ResettableIterator<E> loopingIterator(Collection<?
extends E> coll) {
if (coll == null) {
throw new NullPointerException("Collection must not be null");
}
- return new LoopingIterator(coll);
+ return new LoopingIterator<E>(coll);
}
-
+
/**
* Gets an iterator that loops continuously over the supplied list.
* <p>
@@ -698,13 +703,13 @@
* @throws NullPointerException if the list is null
* @since Commons Collections 3.2
*/
- public static ResettableListIterator loopingListIterator(List list) {
+ public static <E> ResettableListIterator<E> loopingListIterator(List<E>
list) {
if (list == null) {
throw new NullPointerException("List must not be null");
}
- return new LoopingListIterator(list);
+ return new LoopingListIterator<E>(list);
}
-
+
// Views
//-----------------------------------------------------------------------
/**
@@ -713,31 +718,31 @@
* @param enumeration the enumeration to use
* @return a new iterator
*/
- public static Iterator asIterator(Enumeration enumeration) {
+ public static <E> Iterator<E> asIterator(Enumeration<? extends E>
enumeration) {
if (enumeration == null) {
throw new NullPointerException("Enumeration must not be null");
}
- return new EnumerationIterator(enumeration);
+ return new EnumerationIterator<E>(enumeration);
}
/**
- * Gets an iterator that provides an iterator view of the given
enumeration
+ * Gets an iterator that provides an iterator view of the given enumeration
* that will remove elements from the specified collection.
*
* @param enumeration the enumeration to use
* @param removeCollection the collection to remove elements from
* @return a new iterator
*/
- public static Iterator asIterator(Enumeration enumeration, Collection
removeCollection) {
+ public static <E> Iterator<E> asIterator(Enumeration<? extends E>
enumeration, Collection<? super E> removeCollection) {
if (enumeration == null) {
throw new NullPointerException("Enumeration must not be null");
}
if (removeCollection == null) {
throw new NullPointerException("Collection must not be null");
}
- return new EnumerationIterator(enumeration, removeCollection);
+ return new EnumerationIterator<E>(enumeration, removeCollection);
}
-
+
/**
* Gets an enumeration that wraps an iterator.
*
@@ -745,13 +750,13 @@
* @return a new enumeration
* @throws NullPointerException if iterator is null
*/
- public static Enumeration asEnumeration(Iterator iterator) {
+ public static <E> Enumeration<E> asEnumeration(Iterator<? extends E>
iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
- return new IteratorEnumeration(iterator);
+ return new IteratorEnumeration<E>(iterator);
}
-
+
/**
* Gets a list iterator based on a simple iterator.
* <p>
@@ -762,13 +767,13 @@
* @return a new iterator
* @throws NullPointerException if iterator parameter is null
*/
- public static ListIterator toListIterator(Iterator iterator) {
+ public static <E> ListIterator<E> toListIterator(Iterator<? extends E>
iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
- return new ListIteratorWrapper(iterator);
+ return new ListIteratorWrapper<E>(iterator);
}
-
+
/**
* Gets an array based on an iterator.
* <p>
@@ -779,14 +784,14 @@
* @return an array of the iterator contents
* @throws NullPointerException if iterator parameter is null
*/
- public static Object[] toArray(Iterator iterator) {
+ public static Object[] toArray(Iterator<?> iterator) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
- List list = toList(iterator, 100);
+ List<?> list = toList(iterator, 100);
return list.toArray();
}
-
+
/**
* Gets an array based on an iterator.
* <p>
@@ -800,17 +805,18 @@
* @throws NullPointerException if arrayClass is null
* @throws ClassCastException if the arrayClass is invalid
*/
- public static Object[] toArray(Iterator iterator, Class arrayClass) {
+ @SuppressWarnings("unchecked")
+ public static <E> E[] toArray(Iterator<? extends E> iterator, Class<E>
arrayClass) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (arrayClass == null) {
throw new NullPointerException("Array class must not be null");
}
- List list = toList(iterator, 100);
- return list.toArray((Object[]) Array.newInstance(arrayClass,
list.size()));
+ List<E> list = toList(iterator, 100);
+ return list.toArray((E[]) Array.newInstance(arrayClass, list.size()));
}
-
+
/**
* Gets a list based on an iterator.
* <p>
@@ -821,10 +827,10 @@
* @return a list of the iterator contents
* @throws NullPointerException if iterator parameter is null
*/
- public static List toList(Iterator iterator) {
+ public static <E> List<E> toList(Iterator<? extends E> iterator) {
return toList(iterator, 10);
}
-
+
/**
* Gets a list based on an iterator.
* <p>
@@ -837,24 +843,24 @@
* @throws NullPointerException if iterator parameter is null
* @throws IllegalArgumentException if the size is less than 1
*/
- public static List toList(Iterator iterator, int estimatedSize) {
+ public static <E> List<E> toList(Iterator<? extends E> iterator, int
estimatedSize) {
if (iterator == null) {
throw new NullPointerException("Iterator must not be null");
}
if (estimatedSize < 1) {
throw new IllegalArgumentException("Estimated size must be greater
than 0");
}
- List list = new ArrayList(estimatedSize);
+ List<E> list = new ArrayList<E>(estimatedSize);
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
-
- /**
+
+ /**
* Gets a suitable Iterator for the given object.
* <p>
- * This method can handles objects as follows
+ * This method can handle objects as follows
* <ul>
* <li>null - empty iterator
* <li>Iterator - returned directly
@@ -866,49 +872,47 @@
* <li>object with iterator() public method accessed by reflection
* <li>object - singleton iterator
* </ul>
- *
+ *
* @param obj the object to convert to an iterator
* @return a suitable iterator, never null
*/
- public static Iterator getIterator(Object obj) {
+ @SuppressWarnings("unchecked")
+ public static Iterator<?> getIterator(Object obj) {
if (obj == null) {
return emptyIterator();
-
- } else if (obj instanceof Iterator) {
+ }
+ if (obj instanceof Iterator) {
return (Iterator) obj;
-
- } else if (obj instanceof Collection) {
+ }
+ if (obj instanceof Collection) {
return ((Collection) obj).iterator();
-
- } else if (obj instanceof Object[]) {
+ }
+ if (obj instanceof Object[]) {
return new ObjectArrayIterator((Object[]) obj);
-
- } else if (obj instanceof Enumeration) {
+ }
+ if (obj instanceof Enumeration) {
return new EnumerationIterator((Enumeration) obj);
-
- } else if (obj instanceof Map) {
+ }
+ if (obj instanceof Map) {
return ((Map) obj).values().iterator();
-
- } else if (obj instanceof Dictionary) {
+ }
+ if (obj instanceof Dictionary) {
return new EnumerationIterator(((Dictionary) obj).elements());
-
} else if (obj.getClass().isArray()) {
return new ArrayIterator(obj);
-
- } else {
- try {
- Method method = obj.getClass().getMethod("iterator", (Class[])
null);
- if (Iterator.class.isAssignableFrom(method.getReturnType())) {
- Iterator it = (Iterator) method.invoke(obj, (Object[])
null);
- if (it != null) {
- return it;
- }
+ }
+ try {
+ Method method = obj.getClass().getMethod("iterator", (Class[])
null);
+ if (Iterator.class.isAssignableFrom(method.getReturnType())) {
+ Iterator it = (Iterator) method.invoke(obj, (Object[]) null);
+ if (it != null) {
+ return it;
}
- } catch (Exception ex) {
- // ignore
}
- return singletonIterator(obj);
+ } catch (Exception ex) {
+ // ignore
}
+ return singletonIterator(obj);
}
}