/** * Resets the iterator back to the start of the list. */ @Override public void reset() { _reset(); }
/** * Gets an iterator that loops continuously over the supplied list. * <p> * The iterator will only stop looping if the remove method is called * enough times to empty the list, or if the list is empty to start with. * * @param <E> the element type * @param list the list to iterate over, not null * @return a new looping iterator * @throws NullPointerException if the list is null * @since 3.2 */ public static <E> ResettableListIterator<E> loopingListIterator(final List<E> list) { if (list == null) { throw new NullPointerException("List must not be null"); } return new LoopingListIterator<>(list); }
/** * Returns the next object in the list. * <p> * If at the end of the list, returns the first element. * * @return the object after the last element returned * @throws NoSuchElementException if there are no elements in the list */ @Override public E next() { if (list.isEmpty()) { throw new NoSuchElementException( "There are no elements for this iterator to loop on"); } if (iterator.hasNext() == false) { reset(); } return iterator.next(); }
/** * Constructor that wraps a list. * <p> * There is no way to reset a ListIterator instance without * recreating it from the original source, so the List must be * passed in and a reference to it held. * * @param list the list to wrap * @throws NullPointerException if the list it null */ public LoopingListIterator(final List<E> list) { if (list == null) { throw new NullPointerException("The list must not be null"); } this.list = list; _reset(); }