Skip to content

Instantly share code, notes, and snippets.

@codingtim
Created February 18, 2017 10:45
Show Gist options
  • Save codingtim/bb5ad37e2b57c782a03036b20230faa7 to your computer and use it in GitHub Desktop.
Save codingtim/bb5ad37e2b57c782a03036b20230faa7 to your computer and use it in GitHub Desktop.
public class IteratorDataSourceImpl implements IteratorDataSource {
private PagedDataSource pagedDataSource;
@Override
public Iterator<Entity> getAllEntities() {
return new PagedIterator(Paging.firstPage(), (Paging paging) -> pagedDataSource.getEntities(paging).iterator());
}
private static class PagedIterator implements Iterator<Entity> {
private final Function<Paging, Iterator<Entity>> iteratorProvider;
private Paging currentPage;
private Iterator<Entity> currentIterator;
PagedIterator(Paging initialPage, Function<Paging, Iterator<Entity>> iteratorProvider) {
currentPage = initialPage;
this.iteratorProvider = iteratorProvider;
currentIterator = iteratorProvider.apply(currentPage);
}
@Override
public boolean hasNext() {
if(currentIterator.hasNext()) {
return true;
} else {
currentPage = currentPage.next();
currentIterator = iteratorProvider.apply(currentPage);
return currentIterator.hasNext();
}
}
@Override
public Entity next() {
return currentIterator.next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment