Created
February 18, 2017 10:45
-
-
Save codingtim/bb5ad37e2b57c782a03036b20230faa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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