1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.NoSuchElementException ; 21 22 import org.apache.commons.collections.ResettableIterator; 23 24 40 public class LoopingIterator implements ResettableIterator { 41 42 43 private Collection collection; 44 45 private Iterator iterator; 46 47 56 public LoopingIterator(Collection coll) { 57 if (coll == null) { 58 throw new NullPointerException ("The collection must not be null"); 59 } 60 collection = coll; 61 reset(); 62 } 63 64 72 public boolean hasNext() { 73 return (collection.size() > 0); 74 } 75 76 84 public Object next() { 85 if (collection.size() == 0) { 86 throw new NoSuchElementException ("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 106 public void remove() { 107 iterator.remove(); 108 } 109 110 113 public void reset() { 114 iterator = collection.iterator(); 115 } 116 117 122 public int size() { 123 return collection.size(); 124 } 125 126 } 127 | Popular Tags |