title: Using Guava Iterables.cycle as a circular list impl
body: <p>I have a <code>List&lt;Widget&gt;</code> and am looking for the most efficient/elegant solution to loop over it, again and again (kind of like a round robin algorithm):</p>
  
  <pre><code>// Returns a list of 20 widgets, with an id of 1 - 20 respectively.
  List&lt;Widget&gt; widgets = getWidgets();
  Widget widget = pickWidget(); // Returns the 1st widget with id = 1.
  widget = pickWidget(); // Returns the 2nd widget with id = 2.
  widget = pickWidget(); // Return the 3rd widget with id = 3.
  // ..etc.
  widget = pickWidget(); // Returns the 19th widget with id = 19.
  widget = pickWidget(); // Returns the 20th widget with id = 20.
  widget = pickWidget(); // Returns the 1st widget with id = 1 (it cycle back).
  </code></pre>
  
  <p>That's the usage, for the implementation the best I've been able to find is Guava's <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Iterables.html#cycle(java.lang.Iterable)" rel="nofollow"><code>Iterables.cycle(...)</code></a>:</p>
  
  <pre><code>Widget pickWidget() {
      for(Widget w : Iterables.cycle(widgets)) {
          return w;
      }
  }
  </code></pre>
  
  <p>The problem is that <code>cycle</code> doesn't leave a marker inside <code>widgets</code> so that it can "remember" where it let off the last time <code>pickWidget()</code> was called.</p>
  
  <p>Any ideas here? Apache's <code>CircularFifoQueue</code> seems close but no cigar, as I don't want anything popped off the queue, I just want it to cycle over the same list time and time again as it is called.</p>