Skip to content

Commit

Permalink
fixed issue #20
Browse files Browse the repository at this point in the history
PagedIterator is updated to cope with the base iterator returning array of length 0
  • Loading branch information
kohsuke committed Sep 13, 2012
1 parent aed8880 commit 2e74517
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/kohsuke/github/PagedIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
* Iterator over a pagenated data source.
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/kohsuke/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -51,6 +52,17 @@ public void testCredentialValid() throws IOException {
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());
}
Expand Down

0 comments on commit 2e74517

Please sign in to comment.