Skip to content

Commit f28edbc

Browse files
committed
Simplify creation of PagedIterables from requests
1 parent 29e147f commit f28edbc

24 files changed

Lines changed: 334 additions & 636 deletions

src/main/java/org/kohsuke/github/GHApp.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,11 @@ public void setPermissions(Map<String, String> permissions) {
107107
*/
108108
@Preview @Deprecated
109109
public PagedIterable<GHAppInstallation> listInstallations() {
110-
return new PagedIterable<GHAppInstallation>() {
111-
public PagedIterator<GHAppInstallation> _iterator(int pageSize) {
112-
return new PagedIterator<GHAppInstallation>(root.retrieve().withPreview(MACHINE_MAN).asIterator("/app/installations", GHAppInstallation[].class, pageSize)) {
113-
protected void wrapUp(GHAppInstallation[] page) {
114-
for (GHAppInstallation appInstallation : page) {
115-
appInstallation.wrapUp(root);
116-
}
117-
}
118-
};
119-
}
120-
};
110+
return root.retrieve().withPreview(MACHINE_MAN)
111+
.asPagedIterable(
112+
"/app/installations",
113+
GHAppInstallation[].class,
114+
item -> item.wrapUp(root) );
121115
}
122116

123117
/**

src/main/java/org/kohsuke/github/GHCommit.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,11 @@ private GHUser resolveUser(User author) throws IOException {
327327
* Lists up all the commit comments in this repository.
328328
*/
329329
public PagedIterable<GHCommitComment> listComments() {
330-
return new PagedIterable<GHCommitComment>() {
331-
public PagedIterator<GHCommitComment> _iterator(int pageSize) {
332-
return new PagedIterator<GHCommitComment>(owner.root.retrieve().asIterator(String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha), GHCommitComment[].class, pageSize)) {
333-
@Override
334-
protected void wrapUp(GHCommitComment[] page) {
335-
for (GHCommitComment c : page)
336-
c.wrap(owner);
337-
}
338-
};
339-
}
340-
};
330+
return owner.root.retrieve()
331+
.asPagedIterable(
332+
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha),
333+
GHCommitComment[].class,
334+
item -> item.wrap(owner) );
341335
}
342336

343337
/**

src/main/java/org/kohsuke/github/GHCommitComment.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException {
9898

9999
@Preview @Deprecated
100100
public PagedIterable<GHReaction> listReactions() {
101-
return new PagedIterable<GHReaction>() {
102-
public PagedIterator<GHReaction> _iterator(int pageSize) {
103-
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiTail()+"/reactions", GHReaction[].class, pageSize)) {
104-
@Override
105-
protected void wrapUp(GHReaction[] page) {
106-
for (GHReaction c : page)
107-
c.wrap(owner.root);
108-
}
109-
};
110-
}
111-
};
101+
return owner.root.retrieve().withPreview(SQUIRREL_GIRL)
102+
.asPagedIterable(
103+
getApiTail()+"/reactions",
104+
GHReaction[].class,
105+
item -> item.wrap(owner.root) );
112106
}
113107

114108
/**

src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,10 @@ public GHCommitQueryBuilder until(long timestamp) {
9191
* Lists up the commits with the criteria built so far.
9292
*/
9393
public PagedIterable<GHCommit> list() {
94-
return new PagedIterable<GHCommit>() {
95-
public PagedIterator<GHCommit> _iterator(int pageSize) {
96-
return new PagedIterator<GHCommit>(req.asIterator(repo.getApiTailUrl("commits"), GHCommit[].class, pageSize)) {
97-
protected void wrapUp(GHCommit[] page) {
98-
for (GHCommit c : page)
99-
c.wrapUp(repo);
100-
}
101-
};
102-
}
103-
};
94+
return req
95+
.asPagedIterable(
96+
repo.getApiTailUrl("commits"),
97+
GHCommit[].class,
98+
item -> item.wrapUp(repo) );
10499
}
105100
}

src/main/java/org/kohsuke/github/GHContent.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,11 @@ public PagedIterable<GHContent> listDirectoryContent() throws IOException {
150150
if (!isDirectory())
151151
throw new IllegalStateException(path+" is not a directory");
152152

153-
return new PagedIterable<GHContent>() {
154-
public PagedIterator<GHContent> _iterator(int pageSize) {
155-
return new PagedIterator<GHContent>(root.retrieve().asIterator(url, GHContent[].class, pageSize)) {
156-
@Override
157-
protected void wrapUp(GHContent[] page) {
158-
GHContent.wrap(page, repository);
159-
}
160-
};
161-
}
162-
};
153+
return root.retrieve()
154+
.asPagedIterable(
155+
url,
156+
GHContent[].class,
157+
item -> item.wrap(repository) );
163158
}
164159

165160
@SuppressFBWarnings("DM_DEFAULT_ENCODING")

src/main/java/org/kohsuke/github/GHDeployment.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,11 @@ public GHDeploymentStatusBuilder createStatus(GHDeploymentState state) {
7171
}
7272

7373
public PagedIterable<GHDeploymentStatus> listStatuses() {
74-
return new PagedIterable<GHDeploymentStatus>() {
75-
public PagedIterator<GHDeploymentStatus> _iterator(int pageSize) {
76-
return new PagedIterator<GHDeploymentStatus>(root.retrieve().asIterator(statuses_url, GHDeploymentStatus[].class, pageSize)) {
77-
@Override
78-
protected void wrapUp(GHDeploymentStatus[] page) {
79-
for (GHDeploymentStatus c : page)
80-
c.wrap(owner);
81-
}
82-
};
83-
}
84-
};
74+
return root.retrieve()
75+
.asPagedIterable(
76+
statuses_url,
77+
GHDeploymentStatus[].class,
78+
item -> item.wrap(owner) );
8579
}
8680

8781
}

src/main/java/org/kohsuke/github/GHGist.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,11 @@ public GHGist fork() throws IOException {
145145
}
146146

147147
public PagedIterable<GHGist> listForks() {
148-
return new PagedIterable<GHGist>() {
149-
public PagedIterator<GHGist> _iterator(int pageSize) {
150-
return new PagedIterator<GHGist>(root.retrieve().asIterator(getApiTailUrl("forks"), GHGist[].class, pageSize)) {
151-
@Override
152-
protected void wrapUp(GHGist[] page) {
153-
for (GHGist c : page)
154-
c.wrapUp(root);
155-
}
156-
};
157-
}
158-
};
148+
return root.retrieve()
149+
.asPagedIterable(
150+
getApiTailUrl("forks"),
151+
GHGist[].class,
152+
item -> item.wrapUp(root) );
159153
}
160154

161155
/**

src/main/java/org/kohsuke/github/GHIssue.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,11 @@ public List<GHIssueComment> getComments() throws IOException {
292292
* Obtains all the comments associated with this issue.
293293
*/
294294
public PagedIterable<GHIssueComment> listComments() throws IOException {
295-
return new PagedIterable<GHIssueComment>() {
296-
public PagedIterator<GHIssueComment> _iterator(int pageSize) {
297-
return new PagedIterator<GHIssueComment>(root.retrieve().asIterator(getIssuesApiRoute() + "/comments", GHIssueComment[].class, pageSize)) {
298-
protected void wrapUp(GHIssueComment[] page) {
299-
for (GHIssueComment c : page)
300-
c.wrapUp(GHIssue.this);
301-
}
302-
};
303-
}
304-
};
295+
return root.retrieve()
296+
.asPagedIterable(
297+
getIssuesApiRoute() + "/comments",
298+
GHIssueComment[].class,
299+
item -> item.wrapUp(GHIssue.this) );
305300
}
306301

307302
@Preview @Deprecated
@@ -314,17 +309,11 @@ public GHReaction createReaction(ReactionContent content) throws IOException {
314309

315310
@Preview @Deprecated
316311
public PagedIterable<GHReaction> listReactions() {
317-
return new PagedIterable<GHReaction>() {
318-
public PagedIterator<GHReaction> _iterator(int pageSize) {
319-
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
320-
@Override
321-
protected void wrapUp(GHReaction[] page) {
322-
for (GHReaction c : page)
323-
c.wrap(owner.root);
324-
}
325-
};
326-
}
327-
};
312+
return owner.root.retrieve().withPreview(SQUIRREL_GIRL)
313+
.asPagedIterable(
314+
getApiRoute()+"/reactions",
315+
GHReaction[].class,
316+
item -> item.wrap(owner.root) );
328317
}
329318

330319
public void addAssignees(GHUser... assignees) throws IOException {

src/main/java/org/kohsuke/github/GHIssueComment.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,12 @@ public GHReaction createReaction(ReactionContent content) throws IOException {
109109

110110
@Preview @Deprecated
111111
public PagedIterable<GHReaction> listReactions() {
112-
return new PagedIterable<GHReaction>() {
113-
public PagedIterator<GHReaction> _iterator(int pageSize) {
114-
return new PagedIterator<GHReaction>(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) {
115-
@Override
116-
protected void wrapUp(GHReaction[] page) {
117-
for (GHReaction c : page)
118-
c.wrap(owner.root);
119-
}
120-
};
121-
}
122-
};
112+
return owner.root.retrieve()
113+
.withPreview(SQUIRREL_GIRL)
114+
.asPagedIterable(
115+
getApiRoute()+"/reactions",
116+
GHReaction[].class,
117+
item -> item.wrap(owner.root) );
123118
}
124119

125120
private String getApiRoute() {

src/main/java/org/kohsuke/github/GHMyself.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,13 @@ public PagedIterable<GHRepository> listRepositories(final int pageSize) {
156156
* @param repoType type of repository returned in the listing
157157
*/
158158
public PagedIterable<GHRepository> listRepositories(final int pageSize, final RepositoryListFilter repoType) {
159-
return new PagedIterable<GHRepository>() {
160-
public PagedIterator<GHRepository> _iterator(int pageSize) {
161-
return new PagedIterator<GHRepository>(root.retrieve().with("type",repoType).asIterator("/user/repos", GHRepository[].class, pageSize)) {
162-
@Override
163-
protected void wrapUp(GHRepository[] page) {
164-
for (GHRepository c : page)
165-
c.wrap(root);
166-
}
167-
};
168-
}
169-
}.withPageSize(pageSize);
159+
return root.retrieve()
160+
.with("type",repoType)
161+
.asPagedIterable(
162+
"/user/repos",
163+
GHRepository[].class,
164+
item -> item.wrap(root)
165+
).withPageSize(pageSize);
170166
}
171167

172168
/**
@@ -191,16 +187,12 @@ public PagedIterable<GHMembership> listOrgMemberships() {
191187
* Filter by a specific state
192188
*/
193189
public PagedIterable<GHMembership> listOrgMemberships(final GHMembership.State state) {
194-
return new PagedIterable<GHMembership>() {
195-
public PagedIterator<GHMembership> _iterator(int pageSize) {
196-
return new PagedIterator<GHMembership>(root.retrieve().with("state",state).asIterator("/user/memberships/orgs", GHMembership[].class, pageSize)) {
197-
@Override
198-
protected void wrapUp(GHMembership[] page) {
199-
GHMembership.wrap(page,root);
200-
}
201-
};
202-
}
203-
};
190+
return root.retrieve()
191+
.with("state",state)
192+
.asPagedIterable(
193+
"/user/memberships/orgs",
194+
GHMembership[].class,
195+
item -> item.wrap(root) );
204196
}
205197

206198
/**

0 commit comments

Comments
 (0)