@Test public void shouldReturnElementsOfTheIterableInBatchesOfTheSpecifiedSize() { Iterable<String> input = iterableWith("a", "b", "c", "d"); Iterator<String> expectedFirstBatch = iterableWith("a", "b").iterator(); Iterator<String> expectedSecondBatch = iterableWith("c", "d").iterator(); Iterator<Iterable<String>> returnedIterator = Lazily.batch(input, 2).iterator(); Iterator<String> actualFirstBatch = returnedIterator.next().iterator(); Iterator<String> actualSecondBatch = returnedIterator.next().iterator(); assertThat(actualFirstBatch.next(), is(expectedFirstBatch.next())); assertThat(actualFirstBatch.next(), is(expectedFirstBatch.next())); assertThat(actualSecondBatch.next(), is(expectedSecondBatch.next())); assertThat(actualSecondBatch.next(), is(expectedSecondBatch.next())); }
@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();  }