Massaging the pull request 15.

- we need to maintain the binary compatibility, so I reverted
  getPullRequests and added listPullRequests that exposes PagedIterable.

- Made PagedIterator expose asList.
This commit is contained in:
Kohsuke Kawaguchi
2012-08-28 09:39:49 -07:00
parent 9fd34aec7f
commit 8f95c4f179
5 changed files with 32 additions and 21 deletions

View File

@@ -129,7 +129,7 @@ public class GHOrganization extends GHPerson {
public List<GHPullRequest> getPullRequests() throws IOException {
List<GHPullRequest> all = new ArrayList<GHPullRequest>();
for (GHRepository r : getRepositoriesWithOpenPullRequests()) {
all.addAll(r.getPullRequests(GHIssueState.OPEN).iterator().asList());
all.addAll(r.getPullRequests(GHIssueState.OPEN));
}
return all;
}

View File

@@ -358,8 +358,17 @@ public class GHRepository {
/**
* Retrieves all the pull requests of a particular state.
*
* @see #listPullRequests(GHIssueState)
*/
public PagedIterable<GHPullRequest> getPullRequests(final GHIssueState state) {
public List<GHPullRequest> getPullRequests(GHIssueState state) throws IOException {
return listPullRequests(state).asList();
}
/**
* Retrieves all the pull requests of a particular state.
*/
public PagedIterable<GHPullRequest> listPullRequests(final GHIssueState state) {
return new PagedIterable<GHPullRequest>() {
public PagedIterator<GHPullRequest> iterator() {
return new PagedIterator<GHPullRequest>(root.retrievePaged(String.format("/repos/%s/%s/pulls?state=%s", owner.login,name,state.name().toLowerCase(Locale.ENGLISH)), GHPullRequest[].class, false)) {
@@ -369,7 +378,7 @@ public class GHRepository {
pr.wrap(GHRepository.this);
}
};
};
}
};
}

View File

@@ -1,8 +1,24 @@
package org.kohsuke.github;
import java.util.ArrayList;
import java.util.List;
/**
* {@link Iterable} that returns {@link PagedIterator}
*
* @author Kohsuke Kawaguchi
*/
public interface PagedIterable<T> extends Iterable<T> {
PagedIterator<T> iterator();
public abstract class PagedIterable<T> implements Iterable<T> {
public abstract PagedIterator<T> iterator();
/**
* Eagerly walk {@link Iterable} and return the result in a list.
*/
public List<T> asList() {
List<T> r = new ArrayList<T>();
for(PagedIterator<T> i = iterator(); i.hasNext();) {
r.addAll(i.nextPage());
}
return r;
}
}

View File

@@ -1,6 +1,5 @@
package org.kohsuke.github;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@@ -9,8 +8,7 @@ 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 and {@link #asList()}
* that allows the caller to retrieve all items at once.
* that allows the caller to retrieve items per page.
*
* @author Kohsuke Kawaguchi
*/
@@ -63,15 +61,4 @@ public abstract class PagedIterator<T> implements Iterator<T> {
pos = 0;
return r;
}
/**
* Gets a list of all items
*/
public List<T> asList() {
List<T> r = new ArrayList<T>();
for(Iterator i = this; i.hasNext();) {
r.addAll(nextPage());
}
return r;
}
}