CyclicalConcurrentIterator implements Iterator<E> { public E next() { int index = computeIndex(); E result = members.get( index ); if ( result == null ) { throw new IllegalStateException( "Null object found for index: " + index ); } return result; } CyclicalConcurrentIterator(Collection<E> members); E next(); @Override boolean hasNext(); int size();  }
@Test public void testWithThreeItems() throws Exception { List<String> list = Lists.newArrayList( "1", "2", "3" ); CyclicalConcurrentIterator<String> iterator = new CyclicalConcurrentIterator<>( list ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); iterator.position.set( Integer.MAX_VALUE ); assertEquals( "2", iterator.next() ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); }
@Test public void testWithOverflow() throws Exception { List<String> list = Lists.newArrayList( "1", "2", "3" ); CyclicalConcurrentIterator<String> iterator = new CyclicalConcurrentIterator<>( list ); iterator.position.set( Integer.MAX_VALUE ); assertEquals( "2", iterator.next() ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); assertEquals( "1", iterator.next() ); assertEquals( "2", iterator.next() ); assertEquals( "3", iterator.next() ); }