001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.iterators; 018 019import java.util.Collection; 020import java.util.Iterator; 021import java.util.NoSuchElementException; 022import java.util.Objects; 023 024import org.apache.commons.collections4.ResettableIterator; 025 026/** 027 * An Iterator that restarts when it reaches the end. 028 * <p> 029 * The iterator will loop continuously around the provided elements, unless 030 * there are no elements in the collection to begin with, or all the elements 031 * have been {@link #remove removed}. 032 * </p> 033 * <p> 034 * Concurrent modifications are not directly supported, and for most collection 035 * implementations will throw a ConcurrentModificationException. 036 * </p> 037 * 038 * @param <E> the type of elements returned by this iterator. 039 * @since 3.0 040 */ 041public class LoopingIterator<E> implements ResettableIterator<E> { 042 043 /** The collection to base the iterator on */ 044 private final Collection<? extends E> collection; 045 /** The current iterator */ 046 private Iterator<? extends E> iterator; 047 048 /** 049 * Constructor that wraps a collection. 050 * <p> 051 * There is no way to reset an Iterator instance without recreating it from 052 * the original source, so the Collection must be passed in. 053 * </p> 054 * 055 * @param collection the collection to wrap 056 * @throws NullPointerException if the collection is null 057 */ 058 public LoopingIterator(final Collection<? extends E> collection) { 059 this.collection = Objects.requireNonNull(collection, "collection"); 060 reset(); 061 } 062 063 /** 064 * Has the iterator any more elements. 065 * <p> 066 * Returns false only if the collection originally had zero elements, or 067 * all the elements have been {@link #remove removed}. 068 * </p> 069 * 070 * @return {@code true} if there are more elements 071 */ 072 @Override 073 public boolean hasNext() { 074 return !collection.isEmpty(); 075 } 076 077 /** 078 * Returns the next object in the collection. 079 * <p> 080 * If at the end of the collection, return the first element. 081 * </p> 082 * 083 * @return the next object 084 * @throws NoSuchElementException if there are no elements 085 * at all. Use {@link #hasNext} to avoid this error. 086 */ 087 @Override 088 public E next() { 089 if (collection.isEmpty()) { 090 throw new NoSuchElementException("There are no elements for this iterator to loop on"); 091 } 092 if (!iterator.hasNext()) { 093 reset(); 094 } 095 return iterator.next(); 096 } 097 098 /** 099 * Removes the previously retrieved item from the underlying collection. 100 * <p> 101 * This feature is only supported if the underlying collection's 102 * {@link Collection#iterator()} method returns an implementation 103 * that supports it. 104 * </p> 105 * <p> 106 * This method can only be called after at least one {@link #next} method call. 107 * After a removal, the remove method may not be called again until another 108 * next has been performed. If the {@link #reset} is called, then remove may 109 * not be called until {@link #next} is called again. 110 * </p> 111 */ 112 @Override 113 public void remove() { 114 iterator.remove(); 115 } 116 117 /** 118 * Resets the iterator back to the start of the collection. 119 */ 120 @Override 121 public void reset() { 122 iterator = collection.iterator(); 123 } 124 125 /** 126 * Gets the size of the collection underlying the iterator. 127 * 128 * @return the current collection size 129 */ 130 public int size() { 131 return collection.size(); 132 } 133 134}