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. import java.util.Collection;
  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;
  20. import org.apache.commons.collections.ResettableIterator;
  21. /**
  22. * An Iterator that restarts when it reaches the end.
  23. * <p>
  24. * The iterator will loop continuously around the provided elements, unless
  25. * there are no elements in the collection to begin with, or all the elements
  26. * have been {@link #remove removed}.
  27. * <p>
  28. * Concurrent modifications are not directly supported, and for most collection
  29. * implementations will throw a ConcurrentModificationException.
  30. *
  31. * @since Commons Collections 3.0
  32. * @version $Revision: 1.9 $ $Date: 2004/02/18 00:59:50 $
  33. *
  34. * @author <a href="mailto:joncrlsn@users.sf.net">Jonathan Carlson</a>
  35. * @author Stephen Colebourne
  36. */
  37. public class LoopingIterator implements ResettableIterator {
  38. /** The collection to base the iterator on */
  39. private Collection collection;
  40. /** The current iterator */
  41. private Iterator iterator;
  42. /**
  43. * Constructor that wraps a collection.
  44. * <p>
  45. * There is no way to reset an Iterator instance without recreating it from
  46. * the original source, so the Collection must be passed in.
  47. *
  48. * @param coll the collection to wrap
  49. * @throws NullPointerException if the collection is null
  50. */
  51. public LoopingIterator(Collection coll) {
  52. if (coll == null) {
  53. throw new NullPointerException("The collection must not be null");
  54. }
  55. collection = coll;
  56. reset();
  57. }
  58. /**
  59. * Has the iterator any more elements.
  60. * <p>
  61. * Returns false only if the collection originally had zero elements, or
  62. * all the elements have been {@link #remove removed}.
  63. *
  64. * @return <code>true</code> if there are more elements
  65. */
  66. public boolean hasNext() {
  67. return (collection.size() > 0);
  68. }
  69. /**
  70. * Returns the next object in the collection.
  71. * <p>
  72. * If at the end of the collection, return the first element.
  73. *
  74. * @throws NoSuchElementException if there are no elements
  75. * at all. Use {@link #hasNext} to avoid this error.
  76. */
  77. public Object next() {
  78. if (collection.size() == 0) {
  79. throw new NoSuchElementException("There are no elements for this iterator to loop on");
  80. }
  81. if (iterator.hasNext() == false) {
  82. reset();
  83. }
  84. return iterator.next();
  85. }
  86. /**
  87. * Removes the previously retrieved item from the underlying collection.
  88. * <p>
  89. * This feature is only supported if the underlying collection's
  90. * {@link Collection#iterator iterator} method returns an implementation
  91. * that supports it.
  92. * <p>
  93. * This method can only be called after at least one {@link #next} method call.
  94. * After a removal, the remove method may not be called again until another
  95. * next has been performed. If the {@link #reset} is called, then remove may
  96. * not be called until {@link #next} is called again.
  97. */
  98. public void remove() {
  99. iterator.remove();
  100. }
  101. /**
  102. * Resets the iterator back to the start of the collection.
  103. */
  104. public void reset() {
  105. iterator = collection.iterator();
  106. }
  107. /**
  108. * Gets the size of the collection underlying the iterator.
  109. *
  110. * @return the current collection size
  111. */
  112. public int size() {
  113. return collection.size();
  114. }
  115. }