@Test public void shouldLeaveLastIterableShortIfBatchSizeDoesNotDivideNumberOfItems() { Iterable<Integer> input = iterableWith(1, 2, 3, 4); Collection<Integer> expectedFirstBatch = collectionWith(1, 2, 3); Collection<Integer> expectedSecondBatch = collectionWith(4); Iterator<Iterable<Integer>> returnedIterator = Lazily.batch(input, 3).iterator(); Collection<Integer> actualFirstBatch = materialize(returnedIterator.next()); Collection<Integer> actualSecondBatch = materialize(returnedIterator.next()); assertThat(actualFirstBatch, hasOnlyItemsInOrder(expectedFirstBatch)); assertThat(actualSecondBatch, hasOnlyItemsInOrder(expectedSecondBatch)); }
@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();  }