KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > iterators > LoopingIterator


1 /*
2  * Copyright 1999-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.iterators;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21
22 import org.apache.commons.collections.ResettableIterator;
23
24 /**
25  * An Iterator that restarts when it reaches the end.
26  * <p>
27  * The iterator will loop continuously around the provided elements, unless
28  * there are no elements in the collection to begin with, or all the elements
29  * have been {@link #remove removed}.
30  * <p>
31  * Concurrent modifications are not directly supported, and for most collection
32  * implementations will throw a ConcurrentModificationException.
33  *
34  * @since Commons Collections 3.0
35  * @version $Revision: 1.9 $ $Date: 2004/02/18 00:59:50 $
36  *
37  * @author <a HREF="mailto:joncrlsn@users.sf.net">Jonathan Carlson</a>
38  * @author Stephen Colebourne
39  */

40 public class LoopingIterator implements ResettableIterator {
41     
42     /** The collection to base the iterator on */
43     private Collection JavaDoc collection;
44     /** The current iterator */
45     private Iterator JavaDoc iterator;
46
47     /**
48      * Constructor that wraps a collection.
49      * <p>
50      * There is no way to reset an Iterator instance without recreating it from
51      * the original source, so the Collection must be passed in.
52      *
53      * @param coll the collection to wrap
54      * @throws NullPointerException if the collection is null
55      */

56     public LoopingIterator(Collection JavaDoc coll) {
57         if (coll == null) {
58             throw new NullPointerException JavaDoc("The collection must not be null");
59         }
60         collection = coll;
61         reset();
62     }
63
64     /**
65      * Has the iterator any more elements.
66      * <p>
67      * Returns false only if the collection originally had zero elements, or
68      * all the elements have been {@link #remove removed}.
69      *
70      * @return <code>true</code> if there are more elements
71      */

72     public boolean hasNext() {
73         return (collection.size() > 0);
74     }
75
76     /**
77      * Returns the next object in the collection.
78      * <p>
79      * If at the end of the collection, return the first element.
80      *
81      * @throws NoSuchElementException if there are no elements
82      * at all. Use {@link #hasNext} to avoid this error.
83      */

84     public Object JavaDoc next() {
85         if (collection.size() == 0) {
86             throw new NoSuchElementException JavaDoc("There are no elements for this iterator to loop on");
87         }
88         if (iterator.hasNext() == false) {
89             reset();
90         }
91         return iterator.next();
92     }
93
94     /**
95      * Removes the previously retrieved item from the underlying collection.
96      * <p>
97      * This feature is only supported if the underlying collection's
98      * {@link Collection#iterator iterator} method returns an implementation
99      * that supports it.
100      * <p>
101      * This method can only be called after at least one {@link #next} method call.
102      * After a removal, the remove method may not be called again until another
103      * next has been performed. If the {@link #reset} is called, then remove may
104      * not be called until {@link #next} is called again.
105      */

106     public void remove() {
107         iterator.remove();
108     }
109
110     /**
111      * Resets the iterator back to the start of the collection.
112      */

113     public void reset() {
114         iterator = collection.iterator();
115     }
116
117     /**
118      * Gets the size of the collection underlying the iterator.
119      *
120      * @return the current collection size
121      */

122     public int size() {
123         return collection.size();
124     }
125
126 }
127
Popular Tags