fixed issue #20

PagedIterator is updated to cope with the base iterator returning array of length 0
This commit is contained in:
Kohsuke Kawaguchi
2012-09-13 15:46:25 -07:00
parent aed888051e
commit 2e74517a4a
2 changed files with 22 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package org.kohsuke.github;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* Iterator over a pagenated data source.
@@ -28,17 +29,24 @@ public abstract class PagedIterator<T> implements Iterator<T> {
protected abstract void wrapUp(T[] page);
public boolean hasNext() {
return (current!=null && pos<current.length) || base.hasNext();
fetch();
return current!=null;
}
public T next() {
fetch();
if (current==null) throw new NoSuchElementException();
return current[pos++];
}
private void fetch() {
while (current==null || current.length<=pos) {
if (!base.hasNext()) {// no more to retrieve
current = null;
pos = 0;
return;
}
current = base.next();
wrapUp(current);
pos = 0;

View File

@@ -12,6 +12,7 @@ import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHKey;
import org.kohsuke.github.GHMyself;
@@ -51,6 +52,17 @@ public class AppTest extends TestCase {
assertFalse(GitHub.connect("totally", "bogus").isCredentialValid());
}
public void testIssueWithNoComment() throws IOException {
GHRepository repository = GitHub.connect().getRepository("kohsuke/github-api");
List<GHIssueComment> v = repository.getIssue(13).getComments();
System.out.println(v);
assertTrue(v.isEmpty());
v = repository.getIssue(5).getComments();
System.out.println(v);
assertTrue(v.size()==3);
}
public void testRateLimit() throws IOException {
System.out.println(GitHub.connect().getRateLimit());
}