ChainedIterable implements CloseableIterable<T> { @Override public CloseableIterator<T> iterator() { return new IteratorWrapper(); } ChainedIterable(final Iterable... itrs); @Override void close(); @Override CloseableIterator<T> iterator();  }
@Test public void shouldThrowNSEXWhenNoNextIterableWhenOneElementAndNo2ndNext() { final Iterable<Integer> chainedIterable = new ChainedIterable<>(Collections.singletonList(1)); final Iterator<Integer> iterator = chainedIterable.iterator(); iterator.next(); assertThrows(NoSuchElementException.class, () -> iterator.next()); }
@Test public void shouldRemoveElementFromFirstIterable() { final List<String> itr1 = Lists.newArrayList("a"); final List<String> emptyItr2 = new ArrayList<>(0); final List<String> itr3 = Lists.newArrayList("b", "c", "d", "e"); final List<String> itr4 = Lists.newArrayList("f", "g"); final Iterable<String> wrappedItr = new ChainedIterable<>(itr1, emptyItr2, itr3, itr4); final Iterator<String> itr = wrappedItr.iterator(); assertEquals("a", itr.next()); itr.remove(); assertEquals(0, itr1.size()); assertEquals(0, emptyItr2.size()); assertEquals(4, itr3.size()); assertEquals(2, itr4.size()); }
@Test public void shouldRemoveElementFromThirdIterable() { final List<String> itr1 = Lists.newArrayList("a"); final List<String> emptyItr2 = new ArrayList<>(0); final List<String> itr3 = Lists.newArrayList("b", "c", "d", "e"); final List<String> itr4 = Lists.newArrayList("f", "g"); final Iterable<String> wrappedItr = new ChainedIterable<>(itr1, emptyItr2, itr3, itr4); final Iterator<String> itr = wrappedItr.iterator(); assertEquals("a", itr.next()); assertEquals("b", itr.next()); itr.remove(); assertEquals(1, itr1.size()); assertEquals(0, emptyItr2.size()); assertEquals(3, itr3.size()); assertEquals(2, itr4.size()); }