package uk.co.jezuk.mango.iterators;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ChainIterator<T> implements Iterator<T>
{
  private final Iterator<Iterator<T>> chain_;
  private Iterator<T> current_;

  public ChainIterator(final Object... iterables)
  {
    final List<Iterator<T>> list = Helpers.toIterators(iterables);
    chain_ = list.iterator();
    current_ = chain_.hasNext() ? chain_.next() : null;
  } // ChainIterator

  public boolean hasNext()
  {
    if(current_ == null)
      return false;

    if(current_.hasNext())
      return true;

    current_ = chain_.hasNext() ? chain_.next() : null;
    return hasNext();
  } // hasNext

  public T next()
  {
    return current_.next();
  } // next

  public void remove()
  {
    throw new UnsupportedOperationException("uk.co.jezuk.mango.ChainIterator does not support the remove method");
  } // remove
} // ChainIterator
@@ -108,16 +108,57 @@
 rayList;
+%0Aimport java.util.NoSuchElementException;
 %0A%0Apublic
@@ -762,29 +762,119 @@
     
-return current_.next(
+if (hasNext())%0A      return current_.next();%0A%0A    throw new NoSuchElementException(%22ChainIterator is exhausted%22
 );%0A 
@@ -1043,24 +1043,25 @@
 emove%0A%7D // ChainIterator
+%0A

Call hasNext in next to make sure we have an element available