@Test public void shouldThrowNoSuchElementExceptionOnceEachIterableHasBeenDepleted() { Iterable<String> input = iterableWith("a", "b"); Iterator<Iterable<String>> returnedIterator = Lazily.batch(input, 1).iterator(); Iterator<String> firstBatchIterator = returnedIterator.next().iterator(); Iterator<String> secondBatchIterator = returnedIterator.next().iterator(); firstBatchIterator.next(); secondBatchIterator.next(); try { returnedIterator.next(); fail("Expected parent iterator to throw a NoSuchElementException."); } catch (NoSuchElementException exception) { } try { firstBatchIterator.next(); fail("Expected first child iterator to throw a NoSuchElementException."); } catch (NoSuchElementException exception) { } try { secondBatchIterator.next(); fail("Expected second child iterator to throw a NoSuchElementException."); } catch (NoSuchElementException exception) { } }
@Override public Iterable<T> next() { if (hasNext()) { List<T> nextBatch = new ArrayList<T>(); for (int i = 0; i < batchSize; i++) { if (iterator.hasNext()) { nextBatch.add(iterator.next()); } } return unmodifiableList(nextBatch); } else { throw new NoSuchElementException(); } }
BatchedIterator implements Iterator<Iterable<T>> { @Override public Iterable<T> next() { if (hasNext()) { List<T> nextBatch = new ArrayList<T>(); for (int i = 0; i < batchSize; i++) { if (iterator.hasNext()) { nextBatch.add(iterator.next()); } } return unmodifiableList(nextBatch); } else { throw new NoSuchElementException(); } } }
BatchedIterator implements Iterator<Iterable<T>> { @Override public Iterable<T> next() { if (hasNext()) { List<T> nextBatch = new ArrayList<T>(); for (int i = 0; i < batchSize; i++) { if (iterator.hasNext()) { nextBatch.add(iterator.next()); } } return unmodifiableList(nextBatch); } else { throw new NoSuchElementException(); } } BatchedIterator(Iterator<? extends T> iterator, int batchSize); }
BatchedIterator implements Iterator<Iterable<T>> { @Override public Iterable<T> next() { if (hasNext()) { List<T> nextBatch = new ArrayList<T>(); for (int i = 0; i < batchSize; i++) { if (iterator.hasNext()) { nextBatch.add(iterator.next()); } } return unmodifiableList(nextBatch); } else { throw new NoSuchElementException(); } } BatchedIterator(Iterator<? extends T> iterator, int batchSize); @Override boolean hasNext(); @Override Iterable<T> next(); @Override void remove(); @Override String toString(); }
BatchedIterator implements Iterator<Iterable<T>> { @Override public Iterable<T> next() { if (hasNext()) { List<T> nextBatch = new ArrayList<T>(); for (int i = 0; i < batchSize; i++) { if (iterator.hasNext()) { nextBatch.add(iterator.next()); } } return unmodifiableList(nextBatch); } else { throw new NoSuchElementException(); } } BatchedIterator(Iterator<? extends T> iterator, int batchSize); @Override boolean hasNext(); @Override Iterable<T> next(); @Override void remove(); @Override String toString();  }