org.kohsuke.github.PagedIterator Maven / Gradle / Ivy
package org.kohsuke.github;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Iterator over a pagenated data source.
*
* Aside from the normal iterator operation, this method exposes {@link #nextPage()}
* that allows the caller to retrieve items per page.
*
* @author Kohsuke Kawaguchi
*/
public abstract class PagedIterator implements Iterator {
private final Iterator base;
/**
* Current batch that we retrieved but haven't returned to the caller.
*/
private T[] current;
private int pos;
/*package*/ PagedIterator(Iterator base) {
this.base = base;
}
protected abstract void wrapUp(T[] page);
public boolean hasNext() {
return (current!=null && pos nextPage() {
fetch();
List r = Arrays.asList(current);
r = r.subList(pos,r.size());
current = null;
pos = 0;
return r;
}
}