### Question:
ChainedIterator implements Iterator<T> { @Override public T next() { if (hasNext()) { return currentIterator.next(); } else { throw new NoSuchElementException(); } } ChainedIterator(Iterator<? extends Iterator<? extends T>> iteratorsIterator);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2, Iterator<? extends T> i3);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2, Iterator<? extends T> i3, Iterator<? extends T> i4);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2, Iterator<? extends T> i3, Iterator<? extends T> i4,
            Iterator<? extends T> i5);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2, Iterator<? extends T> i3, Iterator<? extends T> i4,
            Iterator<? extends T> i5, Iterator<? extends T> i6);  ChainedIterator(
            Iterator<? extends T> i1, Iterator<? extends T> i2, Iterator<? extends T> i3, Iterator<? extends T> i4,
            Iterator<? extends T> i5, Iterator<? extends T> i6, Iterator<? extends T>... i7on); @Override boolean hasNext(); @Override T next(); @Override void remove(); @Override String toString();  }### Answer:
@Test public void shouldReturnElementsFromEachIteratorInOrder() throws Exception { Iterator<Integer> firstIterator = iterableWith(1, 2).iterator(); Iterator<Integer> secondIterator = iterableWith(3, 4).iterator(); Iterator<Integer> thirdIterator = iterableWith(5, 6).iterator(); Iterator<Integer> chainedIterator = new ChainedIterator<Integer>(firstIterator, secondIterator, thirdIterator); assertThat(chainedIterator.next(), is(1)); assertThat(chainedIterator.next(), is(2)); assertThat(chainedIterator.next(), is(3)); assertThat(chainedIterator.next(), is(4)); assertThat(chainedIterator.next(), is(5)); assertThat(chainedIterator.next(), is(6)); }
@Test public void shouldAllowEmptyIteratorsToBeSupplied() throws Exception { Iterator<Integer> firstIterator = emptyIterator(); Iterator<Integer> secondIterator = iterableWith(1, 2, 3).iterator(); Iterator<Integer> thirdIterator = emptyIterator(); Iterator<Integer> fourthIterator = iterableWith(4).iterator(); Iterator<Integer> chainedIterator = new ChainedIterator<Integer>( firstIterator, secondIterator, thirdIterator, fourthIterator); assertThat(chainedIterator.next(), is(1)); assertThat(chainedIterator.next(), is(2)); assertThat(chainedIterator.next(), is(3)); assertThat(chainedIterator.next(), is(4)); }
@Test(expected = NoSuchElementException.class) public void shouldThrowANoSuchElementExceptionWhenAllIteratorsAreExhausted() throws Exception { Iterator<Integer> firstIterator = iterableWith(1, 2).iterator(); Iterator<Integer> secondIterator = emptyIterator(); Iterator<Integer> thirdIterator = iterableWith(3).iterator(); Iterator<Integer> fourthIterator = emptyIterator(); Iterator<Integer> chainedIterator = new ChainedIterator<Integer>( firstIterator, secondIterator, thirdIterator, fourthIterator); chainedIterator.next(); chainedIterator.next(); chainedIterator.next(); chainedIterator.next(); }
@Test public void shouldAllowNullValuesInTheIterator() throws Exception { Iterator<Integer> firstIterator = iterableWith(1, null).iterator(); Iterator<Integer> secondIterator = iterableWith(2).iterator(); Iterator<Integer> chainedIterator = new ChainedIterator<Integer>(firstIterator, secondIterator); assertThat(chainedIterator.next(), is(1)); assertThat(chainedIterator.next(), is(nullValue())); assertThat(chainedIterator.next(), is(2)); }