@Test void iteratorNoResults() { Iterable<Integer> iterable = new BatchingIterable<>(1000, 0, 10) { @Override protected List<Integer> getBatch(int offset, int batchSize) { return Collections.emptyList(); } }; assertFalse(iterable.iterator().hasNext()); }
@Override public Iterator<T> iterator() { return new Iterator<T>() { private int index = offset; private Iterator<T> it; @Override public boolean hasNext() { boolean hasNext; if (it == null) { it = nextBatch(); hasNext = it.hasNext(); if (!hasNext) { return false; } } else { hasNext = it.hasNext(); } if (!hasNext) { it = nextBatch(); hasNext = it.hasNext(); } return hasNext; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T element = it.next(); ++index; return element; } @Override public void remove() { throw new UnsupportedOperationException(); } private Iterator<T> nextBatch() { int nextBatchSize; if (index == offset || (index - offset) % batchSize == 0) { if (limit == 0) { nextBatchSize = batchSize; } else { if (index == offset + limit) { nextBatchSize = 0; } else if (index + batchSize <= offset + limit) { nextBatchSize = batchSize; } else { nextBatchSize = offset + limit - index; } } } else { nextBatchSize = 0; } if (nextBatchSize == 0) { return Collections.emptyIterator(); } else { return getBatch(index, nextBatchSize).iterator(); } } }; }
BatchingIterable implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { private int index = offset; private Iterator<T> it; @Override public boolean hasNext() { boolean hasNext; if (it == null) { it = nextBatch(); hasNext = it.hasNext(); if (!hasNext) { return false; } } else { hasNext = it.hasNext(); } if (!hasNext) { it = nextBatch(); hasNext = it.hasNext(); } return hasNext; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T element = it.next(); ++index; return element; } @Override public void remove() { throw new UnsupportedOperationException(); } private Iterator<T> nextBatch() { int nextBatchSize; if (index == offset || (index - offset) % batchSize == 0) { if (limit == 0) { nextBatchSize = batchSize; } else { if (index == offset + limit) { nextBatchSize = 0; } else if (index + batchSize <= offset + limit) { nextBatchSize = batchSize; } else { nextBatchSize = offset + limit - index; } } } else { nextBatchSize = 0; } if (nextBatchSize == 0) { return Collections.emptyIterator(); } else { return getBatch(index, nextBatchSize).iterator(); } } }; } }
BatchingIterable implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { private int index = offset; private Iterator<T> it; @Override public boolean hasNext() { boolean hasNext; if (it == null) { it = nextBatch(); hasNext = it.hasNext(); if (!hasNext) { return false; } } else { hasNext = it.hasNext(); } if (!hasNext) { it = nextBatch(); hasNext = it.hasNext(); } return hasNext; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T element = it.next(); ++index; return element; } @Override public void remove() { throw new UnsupportedOperationException(); } private Iterator<T> nextBatch() { int nextBatchSize; if (index == offset || (index - offset) % batchSize == 0) { if (limit == 0) { nextBatchSize = batchSize; } else { if (index == offset + limit) { nextBatchSize = 0; } else if (index + batchSize <= offset + limit) { nextBatchSize = batchSize; } else { nextBatchSize = offset + limit - index; } } } else { nextBatchSize = 0; } if (nextBatchSize == 0) { return Collections.emptyIterator(); } else { return getBatch(index, nextBatchSize).iterator(); } } }; } BatchingIterable(int batchSize, int offset, int limit); }
BatchingIterable implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { private int index = offset; private Iterator<T> it; @Override public boolean hasNext() { boolean hasNext; if (it == null) { it = nextBatch(); hasNext = it.hasNext(); if (!hasNext) { return false; } } else { hasNext = it.hasNext(); } if (!hasNext) { it = nextBatch(); hasNext = it.hasNext(); } return hasNext; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T element = it.next(); ++index; return element; } @Override public void remove() { throw new UnsupportedOperationException(); } private Iterator<T> nextBatch() { int nextBatchSize; if (index == offset || (index - offset) % batchSize == 0) { if (limit == 0) { nextBatchSize = batchSize; } else { if (index == offset + limit) { nextBatchSize = 0; } else if (index + batchSize <= offset + limit) { nextBatchSize = batchSize; } else { nextBatchSize = offset + limit - index; } } } else { nextBatchSize = 0; } if (nextBatchSize == 0) { return Collections.emptyIterator(); } else { return getBatch(index, nextBatchSize).iterator(); } } }; } BatchingIterable(int batchSize, int offset, int limit); @Override Iterator<T> iterator(); }
BatchingIterable implements Iterable<T> { @Override public Iterator<T> iterator() { return new Iterator<T>() { private int index = offset; private Iterator<T> it; @Override public boolean hasNext() { boolean hasNext; if (it == null) { it = nextBatch(); hasNext = it.hasNext(); if (!hasNext) { return false; } } else { hasNext = it.hasNext(); } if (!hasNext) { it = nextBatch(); hasNext = it.hasNext(); } return hasNext; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T element = it.next(); ++index; return element; } @Override public void remove() { throw new UnsupportedOperationException(); } private Iterator<T> nextBatch() { int nextBatchSize; if (index == offset || (index - offset) % batchSize == 0) { if (limit == 0) { nextBatchSize = batchSize; } else { if (index == offset + limit) { nextBatchSize = 0; } else if (index + batchSize <= offset + limit) { nextBatchSize = batchSize; } else { nextBatchSize = offset + limit - index; } } } else { nextBatchSize = 0; } if (nextBatchSize == 0) { return Collections.emptyIterator(); } else { return getBatch(index, nextBatchSize).iterator(); } } }; } BatchingIterable(int batchSize, int offset, int limit); @Override Iterator<T> iterator();  }