Skip to content

Commit c178300

Browse files
committed
stream improvment #295 - pageSplitter introduction
1 parent 1eaee11 commit c178300

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import java.util.Spliterator;
5+
import java.util.function.Consumer;
6+
7+
public class PagerSpliterator<T> implements Spliterator<T> {
8+
9+
private Pager<T> pager;
10+
11+
private List<T> list;
12+
private int current;
13+
14+
public PagerSpliterator(Pager<T> pager) {
15+
this.pager = pager;
16+
}
17+
18+
@Override
19+
public boolean tryAdvance(Consumer<? super T> action) {
20+
21+
if (list == null) {
22+
if (pager.hasNext()) {
23+
resetList();
24+
} else {
25+
return false;
26+
}
27+
}
28+
29+
if (current < list.size()) {
30+
action.accept(list.get(current));
31+
current++;
32+
return true;
33+
} else if (pager.hasNext()) {
34+
resetList();
35+
action.accept(list.get(current));
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
private void resetList() {
42+
list = pager.next();
43+
current = 0;
44+
}
45+
46+
@Override
47+
public Spliterator<T> trySplit() {
48+
return null;
49+
}
50+
51+
@Override
52+
public long estimateSize() {
53+
return pager.getTotalItems();
54+
}
55+
56+
@Override
57+
public int characteristics() {
58+
return 0;
59+
}
60+
}

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Map;
3232
import java.util.Optional;
3333
import java.util.stream.Stream;
34+
import java.util.stream.StreamSupport;
3435

3536
import javax.ws.rs.core.Form;
3637
import javax.ws.rs.core.GenericType;
@@ -109,7 +110,7 @@ public Pager<Project> getProjects(int itemsPerPage) throws GitLabApiException {
109110
* @throws GitLabApiException if any exception occurs
110111
*/
111112
public Stream<Project> getProjectsStream() throws GitLabApiException {
112-
return (getProjects(getDefaultPerPage()).stream());
113+
return StreamSupport.stream(new PagerSpliterator(getProjects(getDefaultPerPage())), false);
113114
}
114115

115116
/**
@@ -818,15 +819,15 @@ public Project createProject(Project project, String importUrl) throws GitLabApi
818819
if (isApiVersion(ApiVersion.V3)) {
819820
boolean isPublic = (project.getPublic() != null ? project.getPublic() : project.getVisibility() == Visibility.PUBLIC);
820821
formData.withParam("public", isPublic);
821-
822+
822823
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
823824
throw new IllegalArgumentException("GitLab API v3 does not support tag lists when creating projects");
824825
}
825826
} else {
826827
Visibility visibility = (project.getVisibility() != null ? project.getVisibility() :
827828
project.getPublic() == Boolean.TRUE ? Visibility.PUBLIC : null);
828829
formData.withParam("visibility", visibility);
829-
830+
830831
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
831832
formData.withParam("tag_list", String.join(",", project.getTagList()));
832833
}
@@ -1057,15 +1058,15 @@ public Project updateProject(Project project) throws GitLabApiException {
10571058
formData.withParam("visibility_level", project.getVisibilityLevel());
10581059
boolean isPublic = (project.getPublic() != null ? project.getPublic() : project.getVisibility() == Visibility.PUBLIC);
10591060
formData.withParam("public", isPublic);
1060-
1061+
10611062
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
10621063
throw new IllegalArgumentException("GitLab API v3 does not support tag lists when updating projects");
10631064
}
10641065
} else {
10651066
Visibility visibility = (project.getVisibility() != null ? project.getVisibility() :
10661067
project.getPublic() == Boolean.TRUE ? Visibility.PUBLIC : null);
10671068
formData.withParam("visibility", visibility);
1068-
1069+
10691070
if (project.getTagList() != null && !project.getTagList().isEmpty()) {
10701071
formData.withParam("tag_list", String.join(",", project.getTagList()));
10711072
}
@@ -1090,7 +1091,7 @@ public void deleteProject(Object projectIdOrPath) throws GitLabApiException {
10901091

10911092
/**
10921093
* Forks a project into the user namespace of the authenticated user or the one provided.
1093-
* The forking operation for a project is asynchronous and is completed in a background job.
1094+
* The forking operation for a project is asynchronous and is completed in a background job.
10941095
* The request will return immediately.
10951096
*
10961097
* <pre><code>POST /projects/:id/fork</code></pre>
@@ -1109,7 +1110,7 @@ public Project forkProject(Object projectIdOrPath, String namespace) throws GitL
11091110

11101111
/**
11111112
* Forks a project into the user namespace of the authenticated user or the one provided.
1112-
* The forking operation for a project is asynchronous and is completed in a background job.
1113+
* The forking operation for a project is asynchronous and is completed in a background job.
11131114
* The request will return immediately.
11141115
*
11151116
* <pre><code>POST /projects/:id/fork</code></pre>
@@ -1130,7 +1131,7 @@ public Project forkProject(Object projectIdOrPath, Integer namespaceId) throws G
11301131
* Create a forked from/to relation between existing projects.
11311132
*
11321133
* <pre><code>POST /projects/:id/fork/:forkFromId</code></pre>
1133-
*
1134+
*
11341135
*
11351136
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
11361137
* @param forkedFromId the ID of the project that was forked from
@@ -1455,7 +1456,7 @@ public List<ProjectUser> getProjectUsers(Object projectIdOrPath, String search)
14551456
}
14561457

14571458
/**
1458-
* Get a Pager of project users matching the specified search string. This Pager includes
1459+
* Get a Pager of project users matching the specified search string. This Pager includes
14591460
* all project members and all users assigned to project parent groups.
14601461
*
14611462
* <pre><code>GET /projects/:id/users</code></pre>
@@ -1644,7 +1645,7 @@ public Optional<ProjectHook> getOptionalHook(Object projectIdOrPath, Integer hoo
16441645
* @return the added ProjectHook instance
16451646
* @throws GitLabApiException if any exception occurs
16461647
*/
1647-
public ProjectHook addHook(String projectName, String url, ProjectHook enabledHooks, boolean enableSslVerification, String secretToken)
1648+
public ProjectHook addHook(String projectName, String url, ProjectHook enabledHooks, boolean enableSslVerification, String secretToken)
16481649
throws GitLabApiException {
16491650

16501651
if (projectName == null) {
@@ -2266,9 +2267,9 @@ public void deletePushRules(Object projectIdOrPath) throws GitLabApiException {
22662267

22672268
/**
22682269
* Get a list of projects that were forked from the specified project.
2269-
*
2270+
*
22702271
* <pre><code>GET /projects/:id/forks</code></pre>
2271-
*
2272+
*
22722273
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
22732274
* @return a List of forked projects
22742275
* @throws GitLabApiException if any exception occurs
@@ -2382,7 +2383,7 @@ public Project transferProject(Object projectIdOrPath, String namespace) throws
23822383
}
23832384

23842385
/**
2385-
* Uploads and sets the project avatar for the specified project.
2386+
* Uploads and sets the project avatar for the specified project.
23862387
*
23872388
* <pre><code>PUT /projects/:id/uploads</code></pre>
23882389
*

0 commit comments

Comments
 (0)