KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > IteratorUtils


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.lang.reflect.Array JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Comparator JavaDoc;
23 import java.util.Dictionary JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.commons.collections.iterators.ArrayIterator;
31 import org.apache.commons.collections.iterators.ArrayListIterator;
32 import org.apache.commons.collections.iterators.CollatingIterator;
33 import org.apache.commons.collections.iterators.EmptyIterator;
34 import org.apache.commons.collections.iterators.EmptyListIterator;
35 import org.apache.commons.collections.iterators.EmptyMapIterator;
36 import org.apache.commons.collections.iterators.EmptyOrderedIterator;
37 import org.apache.commons.collections.iterators.EmptyOrderedMapIterator;
38 import org.apache.commons.collections.iterators.EnumerationIterator;
39 import org.apache.commons.collections.iterators.FilterIterator;
40 import org.apache.commons.collections.iterators.FilterListIterator;
41 import org.apache.commons.collections.iterators.IteratorChain;
42 import org.apache.commons.collections.iterators.IteratorEnumeration;
43 import org.apache.commons.collections.iterators.ListIteratorWrapper;
44 import org.apache.commons.collections.iterators.LoopingIterator;
45 import org.apache.commons.collections.iterators.ObjectArrayIterator;
46 import org.apache.commons.collections.iterators.ObjectArrayListIterator;
47 import org.apache.commons.collections.iterators.ObjectGraphIterator;
48 import org.apache.commons.collections.iterators.SingletonIterator;
49 import org.apache.commons.collections.iterators.SingletonListIterator;
50 import org.apache.commons.collections.iterators.TransformIterator;
51 import org.apache.commons.collections.iterators.UnmodifiableIterator;
52 import org.apache.commons.collections.iterators.UnmodifiableListIterator;
53 import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
54
55 /**
56  * Provides static utility methods and decorators for {@link Iterator}
57  * instances. The implementations are provided in the iterators subpackage.
58  * <p>
59  * WARNING: Due to human error certain binary incompatabilities were introduced
60  * between Commons Collections 2.1 and 3.0. The class remained source and test
61  * compatible, so if you can recompile all your classes and dependencies
62  * everything is OK. Those methods which are binary incompatible are marked as
63  * such, together with alternate solutions that are binary compatible
64  * against versions 2.1.1 and 3.1.
65  *
66  * @since Commons Collections 2.1
67  * @version $Revision: 1.27 $ $Date: 2004/05/26 21:53:46 $
68  *
69  * @author Stephen Colebourne
70  * @author Phil Steitz
71  */

72 public class IteratorUtils {
73     // validation is done in this class in certain cases because the
74
// public classes allow invalid states
75

76     /**
77      * An iterator over no elements.
78      * <p>
79      * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
80      * Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
81      */

82     public static final ResettableIterator EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE;
83     /**
84      * A list iterator over no elements.
85      * <p>
86      * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1.
87      * Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
88      */

89     public static final ResettableListIterator EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE;
90     /**
91      * An ordered iterator over no elements.
92      */

93     public static final OrderedIterator EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE;
94     /**
95      * A map iterator over no elements.
96      */

97     public static final MapIterator EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE;
98     /**
99      * An ordered map iterator over no elements.
100      */

101     public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE;
102
103     /**
104      * IteratorUtils is not normally instantiated.
105      */

106     public IteratorUtils() {
107     }
108
109     // Empty
110
//-----------------------------------------------------------------------
111
/**
112      * Gets an empty iterator.
113      * <p>
114      * This iterator is a valid iterator object that will iterate over
115      * nothing.
116      * <p>
117      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
118      * Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
119      *
120      * @return an iterator over nothing
121      */

122     public static ResettableIterator emptyIterator() {
123         return EMPTY_ITERATOR;
124     }
125
126     /**
127      * Gets an empty list iterator.
128      * <p>
129      * This iterator is a valid list iterator object that will iterate
130      * over nothing.
131      * <p>
132      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
133      * Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1.
134      *
135      * @return a list iterator over nothing
136      */

137     public static ResettableListIterator emptyListIterator() {
138         return EMPTY_LIST_ITERATOR;
139     }
140
141     /**
142      * Gets an empty ordered iterator.
143      * <p>
144      * This iterator is a valid iterator object that will iterate
145      * over nothing.
146      *
147      * @return an ordered iterator over nothing
148      */

149     public static OrderedIterator emptyOrderedIterator() {
150         return EMPTY_ORDERED_ITERATOR;
151     }
152
153     /**
154      * Gets an empty map iterator.
155      * <p>
156      * This iterator is a valid map iterator object that will iterate
157      * over nothing.
158      *
159      * @return a map iterator over nothing
160      */

161     public static MapIterator emptyMapIterator() {
162         return EMPTY_MAP_ITERATOR;
163     }
164
165     /**
166      * Gets an empty ordered map iterator.
167      * <p>
168      * This iterator is a valid map iterator object that will iterate
169      * over nothing.
170      *
171      * @return a map iterator over nothing
172      */

173     public static OrderedMapIterator emptyOrderedMapIterator() {
174         return EMPTY_ORDERED_MAP_ITERATOR;
175     }
176
177     // Singleton
178
//-----------------------------------------------------------------------
179
/**
180      * Gets a singleton iterator.
181      * <p>
182      * This iterator is a valid iterator object that will iterate over
183      * the specified object.
184      * <p>
185      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
186      * Use <code>new SingletonIterator(object)</code> for compatability.
187      *
188      * @param object the single object over which to iterate
189      * @return a singleton iterator over the object
190      */

191     public static ResettableIterator singletonIterator(Object JavaDoc object) {
192         return new SingletonIterator(object);
193     }
194
195     /**
196      * Gets a singleton list iterator.
197      * <p>
198      * This iterator is a valid list iterator object that will iterate over
199      * the specified object.
200      *
201      * @param object the single object over which to iterate
202      * @return a singleton list iterator over the object
203      */

204     public static ListIterator JavaDoc singletonListIterator(Object JavaDoc object) {
205         return new SingletonListIterator(object);
206     }
207
208     // Arrays
209
//-----------------------------------------------------------------------
210
/**
211      * Gets an iterator over an object array.
212      * <p>
213      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
214      * Use <code>new ArrayIterator(array)</code> for compatability.
215      *
216      * @param array the array over which to iterate
217      * @return an iterator over the array
218      * @throws NullPointerException if array is null
219      */

220     public static ResettableIterator arrayIterator(Object JavaDoc[] array) {
221         return new ObjectArrayIterator(array);
222     }
223
224     /**
225      * Gets an iterator over an object or primitive array.
226      * <p>
227      * This method will handle primitive arrays as well as object arrays.
228      * The primitives will be wrapped in the appropriate wrapper class.
229      *
230      * @param array the array over which to iterate
231      * @return an iterator over the array
232      * @throws IllegalArgumentException if the array is not an array
233      * @throws NullPointerException if array is null
234      */

235     public static ResettableIterator arrayIterator(Object JavaDoc array) {
236         return new ArrayIterator(array);
237     }
238
239     /**
240      * Gets an iterator over the end part of an object array.
241      * <p>
242      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
243      * Use <code>new ArrayIterator(array,start)</code> for compatability.
244      *
245      * @param array the array over which to iterate
246      * @param start the index to start iterating at
247      * @return an iterator over part of the array
248      * @throws IndexOutOfBoundsException if start is less than zero or greater
249      * than the length of the array
250      * @throws NullPointerException if array is null
251      */

252     public static ResettableIterator arrayIterator(Object JavaDoc[] array, int start) {
253         return new ObjectArrayIterator(array, start);
254     }
255
256     /**
257      * Gets an iterator over the end part of an object or primitive array.
258      * <p>
259      * This method will handle primitive arrays as well as object arrays.
260      * The primitives will be wrapped in the appropriate wrapper class.
261      *
262      * @param array the array over which to iterate
263      * @param start the index to start iterating at
264      * @return an iterator over part of the array
265      * @throws IllegalArgumentException if the array is not an array
266      * @throws IndexOutOfBoundsException if start is less than zero or greater
267      * than the length of the array
268      * @throws NullPointerException if array is null
269      */

270     public static ResettableIterator arrayIterator(Object JavaDoc array, int start) {
271         return new ArrayIterator(array, start);
272     }
273
274     /**
275      * Gets an iterator over part of an object array.
276      * <p>
277      * WARNING: This method is binary incompatible with Commons Collections 2.1 and 2.1.1.
278      * Use <code>new ArrayIterator(array,start,end)</code> for compatability.
279      *
280      * @param array the array over which to iterate
281      * @param start the index to start iterating at
282      * @param end the index to finish iterating at
283      * @return an iterator over part of the array
284      * @throws IndexOutOfBoundsException if array bounds are invalid
285      * @throws IllegalArgumentException if end is before start
286      * @throws NullPointerException if array is null
287      */

288     public static ResettableIterator arrayIterator(Object JavaDoc[] array, int start, int end) {
289         return new ObjectArrayIterator(array, start, end);
290     }
291
292     /**
293      * Gets an iterator over part of an object or primitive array.
294      * <p>
295      * This method will handle primitive arrays as well as object arrays.
296      * The primitives will be wrapped in the appropriate wrapper class.
297      *
298      * @param array the array over which to iterate
299      * @param start the index to start iterating at
300      * @param end the index to finish iterating at
301      * @return an iterator over part of the array
302      * @throws IllegalArgumentException if the array is not an array
303      * @throws IndexOutOfBoundsException if array bounds are invalid
304      * @throws IllegalArgumentException if end is before start
305      * @throws NullPointerException if array is null
306      */

307     public static ResettableIterator arrayIterator(Object JavaDoc array, int start, int end) {
308         return new ArrayIterator(array, start, end);
309     }
310
311     //-----------------------------------------------------------------------
312
/**
313      * Gets a list iterator over an object array.
314      *
315      * @param array the array over which to iterate
316      * @return a list iterator over the array
317      * @throws NullPointerException if array is null
318      */

319     public static ResettableListIterator arrayListIterator(Object JavaDoc[] array) {
320         return new ObjectArrayListIterator(array);
321     }
322
323     /**
324      * Gets a list iterator over an object or primitive array.
325      * <p>
326      * This method will handle primitive arrays as well as object arrays.
327      * The primitives will be wrapped in the appropriate wrapper class.
328      *
329      * @param array the array over which to iterate
330      * @return a list iterator over the array
331      * @throws IllegalArgumentException if the array is not an array
332      * @throws NullPointerException if array is null
333      */

334     public static ResettableListIterator arrayListIterator(Object JavaDoc array) {
335         return new ArrayListIterator(array);
336     }
337
338     /**
339      * Gets a list iterator over the end part of an object array.
340      *
341      * @param array the array over which to iterate
342      * @param start the index to start iterating at
343      * @return a list iterator over part of the array
344      * @throws IndexOutOfBoundsException if start is less than zero
345      * @throws NullPointerException if array is null
346      */

347     public static ResettableListIterator arrayListIterator(Object JavaDoc[] array, int start) {
348         return new ObjectArrayListIterator(array, start);
349     }
350
351     /**
352      * Gets a list iterator over the end part of an object or primitive array.
353      * <p>
354      * This method will handle primitive arrays as well as object arrays.
355      * The primitives will be wrapped in the appropriate wrapper class.
356      *
357      * @param array the array over which to iterate
358      * @param start the index to start iterating at
359      * @return a list iterator over part of the array
360      * @throws IllegalArgumentException if the array is not an array
361      * @throws IndexOutOfBoundsException if start is less than zero
362      * @throws NullPointerException if array is null
363      */

364     public static ResettableListIterator arrayListIterator(Object JavaDoc array, int start) {
365         return new ArrayListIterator(array, start);
366     }
367
368     /**
369      * Gets a list iterator over part of an object array.
370      *
371      * @param array the array over which to iterate
372      * @param start the index to start iterating at
373      * @param end the index to finish iterating at
374      * @return a list iterator over part of the array
375      * @throws IndexOutOfBoundsException if array bounds are invalid
376      * @throws IllegalArgumentException if end is before start
377      * @throws NullPointerException if array is null
378      */

379     public static ResettableListIterator arrayListIterator(Object JavaDoc[] array, int start, int end) {
380         return new ObjectArrayListIterator(array, start, end);
381     }
382     
383     /**
384      * Gets a list iterator over part of an object or primitive array.
385      * <p>
386      * This method will handle primitive arrays as well as object arrays.
387      * The primitives will be wrapped in the appropriate wrapper class.
388      *
389      * @param array the array over which to iterate
390      * @param start the index to start iterating at
391      * @param end the index to finish iterating at
392      * @return a list iterator over part of the array
393      * @throws IllegalArgumentException if the array is not an array
394      * @throws IndexOutOfBoundsException if array bounds are invalid
395      * @throws IllegalArgumentException if end is before start
396      * @throws NullPointerException if array is null
397      */

398     public static ResettableListIterator arrayListIterator(Object JavaDoc array, int start, int end) {
399         return new ArrayListIterator(array, start, end);
400     }
401     
402     // Unmodifiable
403
//-----------------------------------------------------------------------
404
/**
405      * Gets an immutable version of an {@link Iterator}. The returned object
406      * will always throw an {@link UnsupportedOperationException} for
407      * the {@link Iterator#remove} method.
408      *
409      * @param iterator the iterator to make immutable
410      * @return an immutable version of the iterator
411      */

412     public static Iterator JavaDoc unmodifiableIterator(Iterator JavaDoc iterator) {
413         return UnmodifiableIterator.decorate(iterator);
414     }
415     
416     /**
417      * Gets an immutable version of a {@link ListIterator}. The returned object
418      * will always throw an {@link UnsupportedOperationException} for
419      * the {@link Iterator#remove}, {@link ListIterator#add} and
420      * {@link ListIterator#set} methods.
421      *
422      * @param listIterator the iterator to make immutable
423      * @return an immutable version of the iterator
424      */

425     public static ListIterator JavaDoc unmodifiableListIterator(ListIterator JavaDoc listIterator) {
426         return UnmodifiableListIterator.decorate(listIterator);
427     }
428
429     /**
430      * Gets an immutable version of a {@link MapIterator}. The returned object
431      * will always throw an {@link UnsupportedOperationException} for
432      * the {@link Iterator#remove}, {@link MapIterator#setValue(Object)} methods.
433      *
434      * @param mapIterator the iterator to make immutable
435      * @return an immutable version of the iterator
436      */

437     public static MapIterator unmodifiableMapIterator(MapIterator mapIterator) {
438         return UnmodifiableMapIterator.decorate(mapIterator);
439     }
440
441     // Chained
442
//-----------------------------------------------------------------------
443
/**
444      * Gets an iterator that iterates through two {@link Iterator}s
445      * one after another.
446      *
447      * @param iterator1 the first iterators to use, not null
448      * @param iterator2 the first iterators to use, not null
449      * @return a combination iterator over the iterators
450      * @throws NullPointerException if either iterator is null
451      */

452     public static Iterator JavaDoc chainedIterator(Iterator JavaDoc iterator1, Iterator JavaDoc iterator2) {
453         return new IteratorChain(iterator1, iterator2);
454     }
455
456     /**
457      * Gets an iterator that iterates through an array of {@link Iterator}s
458      * one after another.
459      *
460      * @param iterators the iterators to use, not null or empty or contain nulls
461      * @return a combination iterator over the iterators
462      * @throws NullPointerException if iterators array is null or contains a null
463      */

464     public static Iterator JavaDoc chainedIterator(Iterator JavaDoc[] iterators) {
465         return new IteratorChain(iterators);
466     }
467
468     /**
469      * Gets an iterator that iterates through a collections of {@link Iterator}s
470      * one after another.
471      *
472      * @param iterators the iterators to use, not null or empty or contain nulls
473      * @return a combination iterator over the iterators
474      * @throws NullPointerException if iterators collection is null or contains a null
475      * @throws ClassCastException if the iterators collection contains the wrong object type
476      */

477     public static Iterator JavaDoc chainedIterator(Collection JavaDoc iterators) {
478         return new IteratorChain(iterators);
479     }
480
481     // Collated
482
//-----------------------------------------------------------------------
483
/**
484      * Gets an iterator that provides an ordered iteration over the elements
485      * contained in a collection of ordered {@link Iterator}s.
486      * <p>
487      * Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
488      * the {@link Iterator#next()} method will return the lesser of
489      * <code>A.next()</code> and <code>B.next()</code>.
490      * <p>
491      * The comparator is optional. If null is specified then natural order is used.
492      *
493      * @param comparator the comparator to use, may be null for natural order
494      * @param iterator1 the first iterators to use, not null
495      * @param iterator2 the first iterators to use, not null
496      * @return a combination iterator over the iterators
497      * @throws NullPointerException if either iterator is null
498      */

499     public static Iterator JavaDoc collatedIterator(Comparator JavaDoc comparator, Iterator JavaDoc iterator1, Iterator JavaDoc iterator2) {
500         return new CollatingIterator(comparator, iterator1, iterator2);
501     }
502
503     /**
504      * Gets an iterator that provides an ordered iteration over the elements
505      * contained in an array of {@link Iterator}s.
506      * <p>
507      * Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
508      * the {@link Iterator#next()} method will return the lesser of
509      * <code>A.next()</code> and <code>B.next()</code> and so on.
510      * <p>
511      * The comparator is optional. If null is specified then natural order is used.
512      *
513      * @param comparator the comparator to use, may be null for natural order
514      * @param iterators the iterators to use, not null or empty or contain nulls
515      * @return a combination iterator over the iterators
516      * @throws NullPointerException if iterators array is null or contains a null
517      */

518     public static Iterator JavaDoc collatedIterator(Comparator JavaDoc comparator, Iterator JavaDoc[] iterators) {
519         return new CollatingIterator(comparator, iterators);
520     }
521
522     /**
523      * Gets an iterator that provides an ordered iteration over the elements
524      * contained in a collection of {@link Iterator}s.
525      * <p>
526      * Given two ordered {@link Iterator}s <code>A</code> and <code>B</code>,
527      * the {@link Iterator#next()} method will return the lesser of
528      * <code>A.next()</code> and <code>B.next()</code> and so on.
529      * <p>
530      * The comparator is optional. If null is specified then natural order is used.
531      *
532      * @param comparator the comparator to use, may be null for natural order
533      * @param iterators the iterators to use, not null or empty or contain nulls
534      * @return a combination iterator over the iterators
535      * @throws NullPointerException if iterators collection is null or contains a null
536      * @throws ClassCastException if the iterators collection contains the wrong object type
537      */

538     public static Iterator JavaDoc collatedIterator(Comparator JavaDoc comparator, Collection JavaDoc iterators) {
539         return new CollatingIterator(comparator, iterators);
540     }
541     
542     // Object Graph
543
//-----------------------------------------------------------------------
544
/**
545      * Gets an iterator that operates over an object graph.
546      * <p>
547      * This iterator can extract multiple objects from a complex tree-like object graph.
548      * The iteration starts from a single root object.
549      * It uses a <code>Transformer</code> to extract the iterators and elements.
550      * Its main benefit is that no intermediate <code>List</code> is created.
551      * <p>
552      * For example, consider an object graph:
553      * <pre>
554      * |- Branch -- Leaf
555      * | \- Leaf
556      * |- Tree | /- Leaf
557      * | |- Branch -- Leaf
558      * Forest | \- Leaf
559      * | |- Branch -- Leaf
560      * | | \- Leaf
561      * |- Tree | /- Leaf
562      * |- Branch -- Leaf
563      * |- Branch -- Leaf</pre>
564      * The following <code>Transformer</code>, used in this class, will extract all
565      * the Leaf objects without creating a combined intermediate list:
566      * <pre>
567      * public Object transform(Object input) {
568      * if (input instanceof Forest) {
569      * return ((Forest) input).treeIterator();
570      * }
571      * if (input instanceof Tree) {
572      * return ((Tree) input).branchIterator();
573      * }
574      * if (input instanceof Branch) {
575      * return ((Branch) input).leafIterator();
576      * }
577      * if (input instanceof Leaf) {
578      * return input;
579      * }
580      * throw new ClassCastException();
581      * }</pre>
582      * <p>
583      * Internally, iteration starts from the root object. When next is called,
584      * the transformer is called to examine the object. The transformer will return
585      * either an iterator or an object. If the object is an Iterator, the next element
586      * from that iterator is obtained and the process repeats. If the element is an object
587      * it is returned.
588      * <p>
589      * Under many circumstances, linking Iterators together in this manner is
590      * more efficient (and convenient) than using nested for loops to extract a list.
591      *
592      * @param root the root object to start iterating from, null results in an empty iterator
593      * @param transformer the transformer to use, see above, null uses no effect transformer
594      * @return a new object graph iterator
595      * @since Commons Collections 3.1
596      */

597     public static Iterator JavaDoc objectGraphIterator(Object JavaDoc root, Transformer transformer) {
598         return new ObjectGraphIterator(root, transformer);
599     }
600     
601     // Transformed
602
//-----------------------------------------------------------------------
603
/**
604      * Gets an iterator that transforms the elements of another iterator.
605      * <p>
606      * The transformation occurs during the next() method and the underlying
607      * iterator is unaffected by the transformation.
608      *
609      * @param iterator the iterator to use, not null
610      * @param transform the transform to use, not null
611      * @return a new transforming iterator
612      * @throws NullPointerException if either parameter is null
613      */

614     public static Iterator JavaDoc transformedIterator(Iterator JavaDoc iterator, Transformer transform) {
615         if (iterator == null) {
616             throw new NullPointerException JavaDoc("Iterator must not be null");
617         }
618         if (transform == null) {
619             throw new NullPointerException JavaDoc("Transformer must not be null");
620         }
621         return new TransformIterator(iterator, transform);
622     }
623     
624     // Filtered
625
//-----------------------------------------------------------------------
626
/**
627      * Gets an iterator that filters another iterator.
628      * <p>
629      * The returned iterator will only return objects that match the specified
630      * filtering predicate.
631      *
632      * @param iterator the iterator to use, not null
633      * @param predicate the predicate to use as a filter, not null
634      * @return a new filtered iterator
635      * @throws NullPointerException if either parameter is null
636      */

637     public static Iterator JavaDoc filteredIterator(Iterator JavaDoc iterator, Predicate predicate) {
638         if (iterator == null) {
639             throw new NullPointerException JavaDoc("Iterator must not be null");
640         }
641         if (predicate == null) {
642             throw new NullPointerException JavaDoc("Predicate must not be null");
643         }
644         return new FilterIterator(iterator, predicate);
645     }
646     
647     /**
648      * Gets a list iterator that filters another list iterator.
649      * <p>
650      * The returned iterator will only return objects that match the specified
651      * filtering predicate.
652      *
653      * @param listIterator the list iterator to use, not null
654      * @param predicate the predicate to use as a filter, not null
655      * @return a new filtered iterator
656      * @throws NullPointerException if either parameter is null
657      */

658     public static ListIterator JavaDoc filteredListIterator(ListIterator JavaDoc listIterator, Predicate predicate) {
659         if (listIterator == null) {
660             throw new NullPointerException JavaDoc("ListIterator must not be null");
661         }
662         if (predicate == null) {
663             throw new NullPointerException JavaDoc("Predicate must not be null");
664         }
665         return new FilterListIterator(listIterator, predicate);
666     }
667     
668     // Looping
669
//-----------------------------------------------------------------------
670
/**
671      * Gets an iterator that loops continuously over the supplied collection.
672      * <p>
673      * The iterator will only stop looping if the remove method is called
674      * enough times to empty the collection, or if the collection is empty
675      * to start with.
676      *
677      * @param coll the collection to iterate over, not null
678      * @return a new looping iterator
679      * @throws NullPointerException if the collection is null
680      */

681     public static ResettableIterator loopingIterator(Collection JavaDoc coll) {
682         if (coll == null) {
683             throw new NullPointerException JavaDoc("Collection must not be null");
684         }
685         return new LoopingIterator(coll);
686     }
687     
688     // Views
689
//-----------------------------------------------------------------------
690
/**
691      * Gets an iterator that provides an iterator view of the given enumeration.
692      *
693      * @param enumeration the enumeration to use
694      * @return a new iterator
695      */

696     public static Iterator JavaDoc asIterator(Enumeration JavaDoc enumeration) {
697         if (enumeration == null) {
698             throw new NullPointerException JavaDoc("Enumeration must not be null");
699         }
700         return new EnumerationIterator(enumeration);
701     }
702
703     /**
704      * Gets an iterator that provides an iterator view of the given enumeration
705      * that will remove elements from the specified collection.
706      *
707      * @param enumeration the enumeration to use
708      * @param removeCollection the collection to remove elements from
709      * @return a new iterator
710      */

711     public static Iterator JavaDoc asIterator(Enumeration JavaDoc enumeration, Collection JavaDoc removeCollection) {
712         if (enumeration == null) {
713             throw new NullPointerException JavaDoc("Enumeration must not be null");
714         }
715         if (removeCollection == null) {
716             throw new NullPointerException JavaDoc("Collection must not be null");
717         }
718         return new EnumerationIterator(enumeration, removeCollection);
719     }
720     
721     /**
722      * Gets an enumeration that wraps an iterator.
723      *
724      * @param iterator the iterator to use, not null
725      * @return a new enumeration
726      * @throws NullPointerException if iterator is null
727      */

728     public static Enumeration JavaDoc asEnumeration(Iterator JavaDoc iterator) {
729         if (iterator == null) {
730             throw new NullPointerException JavaDoc("Iterator must not be null");
731         }
732         return new IteratorEnumeration(iterator);
733     }
734     
735     /**
736      * Gets a list iterator based on a simple iterator.
737      * <p>
738      * As the wrapped Iterator is traversed, a LinkedList of its values is
739      * cached, permitting all required operations of ListIterator.
740      *
741      * @param iterator the iterator to use, not null
742      * @return a new iterator
743      * @throws NullPointerException if iterator parameter is null
744      */

745     public static ListIterator JavaDoc toListIterator(Iterator JavaDoc iterator) {
746         if (iterator == null) {
747             throw new NullPointerException JavaDoc("Iterator must not be null");
748         }
749         return new ListIteratorWrapper(iterator);
750     }
751     
752     /**
753      * Gets an array based on an iterator.
754      * <p>
755      * As the wrapped Iterator is traversed, an ArrayList of its values is
756      * created. At the end, this is converted to an array.
757      *
758      * @param iterator the iterator to use, not null
759      * @return an array of the iterator contents
760      * @throws NullPointerException if iterator parameter is null
761      */

762     public static Object JavaDoc[] toArray(Iterator JavaDoc iterator) {
763         if (iterator == null) {
764             throw new NullPointerException JavaDoc("Iterator must not be null");
765         }
766         List JavaDoc list = toList(iterator, 100);
767         return list.toArray();
768     }
769     
770     /**
771      * Gets an array based on an iterator.
772      * <p>
773      * As the wrapped Iterator is traversed, an ArrayList of its values is
774      * created. At the end, this is converted to an array.
775      *
776      * @param iterator the iterator to use, not null
777      * @param arrayClass the class of array to create
778      * @return an array of the iterator contents
779      * @throws NullPointerException if iterator parameter is null
780      * @throws NullPointerException if arrayClass is null
781      * @throws ClassCastException if the arrayClass is invalid
782      */

783     public static Object JavaDoc[] toArray(Iterator JavaDoc iterator, Class JavaDoc arrayClass) {
784         if (iterator == null) {
785             throw new NullPointerException JavaDoc("Iterator must not be null");
786         }
787         if (arrayClass == null) {
788             throw new NullPointerException JavaDoc("Array class must not be null");
789         }
790         List JavaDoc list = toList(iterator, 100);
791         return list.toArray((Object JavaDoc[]) Array.newInstance(arrayClass, list.size()));
792     }
793     
794     /**
795      * Gets a list based on an iterator.
796      * <p>
797      * As the wrapped Iterator is traversed, an ArrayList of its values is
798      * created. At the end, the list is returned.
799      *
800      * @param iterator the iterator to use, not null
801      * @return a list of the iterator contents
802      * @throws NullPointerException if iterator parameter is null
803      */

804     public static List JavaDoc toList(Iterator JavaDoc iterator) {
805         return toList(iterator, 10);
806     }
807     
808     /**
809      * Gets a list based on an iterator.
810      * <p>
811      * As the wrapped Iterator is traversed, an ArrayList of its values is
812      * created. At the end, the list is returned.
813      *
814      * @param iterator the iterator to use, not null
815      * @param estimatedSize the initial size of the ArrayList
816      * @return a list of the iterator contents
817      * @throws NullPointerException if iterator parameter is null
818      * @throws IllegalArgumentException if the size is less than 1
819      */

820     public static List JavaDoc toList(Iterator JavaDoc iterator, int estimatedSize) {
821         if (iterator == null) {
822             throw new NullPointerException JavaDoc("Iterator must not be null");
823         }
824         if (estimatedSize < 1) {
825             throw new IllegalArgumentException JavaDoc("Estimated size must be greater than 0");
826         }
827         List JavaDoc list = new ArrayList JavaDoc(estimatedSize);
828         while (iterator.hasNext()) {
829             list.add(iterator.next());
830         }
831         return list;
832     }
833     
834     /**
835      * Gets a suitable Iterator for the given object.
836      * <p>
837      * This method can handles objects as follows
838      * <ul>
839      * <li>null - empty iterator
840      * <li>Iterator - returned directly
841      * <li>Enumeration - wrapped
842      * <li>Collection - iterator from collection returned
843      * <li>Map - values iterator returned
844      * <li>Dictionary - values (elements) enumeration returned as iterator
845      * <li>array - iterator over array returned
846      * <li>object with iterator() public method accessed by reflection
847      * <li>object - singleton iterator
848      * </ul>
849      *
850      * @param obj the object to convert to an iterator
851      * @return a suitable iterator, never null
852      */

853     public static Iterator JavaDoc getIterator(Object JavaDoc obj) {
854         if (obj == null) {
855             return emptyIterator();
856             
857         } else if (obj instanceof Iterator JavaDoc) {
858             return (Iterator JavaDoc) obj;
859             
860         } else if (obj instanceof Collection JavaDoc) {
861             return ((Collection JavaDoc) obj).iterator();
862             
863         } else if (obj instanceof Object JavaDoc[]) {
864             return new ObjectArrayIterator((Object JavaDoc[]) obj);
865             
866         } else if (obj instanceof Enumeration JavaDoc) {
867             return new EnumerationIterator((Enumeration JavaDoc) obj);
868             
869         } else if (obj instanceof Map JavaDoc) {
870             return ((Map JavaDoc) obj).values().iterator();
871             
872         } else if (obj instanceof Dictionary JavaDoc) {
873             return new EnumerationIterator(((Dictionary JavaDoc) obj).elements());
874             
875         } else if (obj != null && obj.getClass().isArray()) {
876             return new ArrayIterator(obj);
877             
878         } else {
879             try {
880                 Method JavaDoc method = obj.getClass().getMethod("iterator", null);
881                 if (Iterator JavaDoc.class.isAssignableFrom(method.getReturnType())) {
882                     Iterator JavaDoc it = (Iterator JavaDoc) method.invoke(obj, null);
883                     if (it != null) {
884                         return it;
885                     }
886                 }
887             } catch (Exception JavaDoc ex) {
888                 // ignore
889
}
890             return singletonIterator(obj);
891         }
892     }
893
894 }
895
Popular Tags