Skip to content

Commit bf0da68

Browse files
committed
Added Java 8 stream support.
1 parent 6675a0a commit bf0da68

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.9.1'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.9.2'
1515
}
1616
```
1717

@@ -22,7 +22,7 @@ dependencies {
2222
<dependency>
2323
<groupId>org.gitlab4j</groupId>
2424
<artifactId>gitlab4j-api</artifactId>
25-
<version>4.9.1</version>
25+
<version>4.9.2</version>
2626
</dependency>
2727
```
2828

@@ -130,6 +130,21 @@ while (projectsPager.hasNext())) {
130130
}
131131
}
132132
```
133+
As of GitLab4J-API 4.9.2, you can also fetch all the items as a single list or get a Java 8+ Stream instance using a Pager instance:
134+
```java
135+
// Get a Pager instance so we can load all the projects into a single list, 10 items at a time:
136+
Pager<Project> projectPager = gitlabApi.getProjectsApi().getProjects(10);
137+
List<Project> allProjects = projectPager.all();
138+
```
139+
140+
```java
141+
// Get a Pager instance to get a Stream<Project> instance.
142+
Pager<Project> projectPager = gitlabApi.getProjectsApi().getProjects(10);
143+
144+
// Stream the Projects printing out the project name.
145+
projectPager.stream().map(Project::getName).forEach(name -> System.out.println(name));
146+
```
147+
133148
---
134149
## Java 8 Optional&lt;T&gt; Support
135150
GitLab4J-API supports Java 8 Optional&lt;T&gt; for API calls that result in the return of a single item. Here is an example on how to use the Java 8 Optional&lt;T&gt; API calls:

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.util.ArrayList;
6+
import java.util.Collections;
67
import java.util.Iterator;
78
import java.util.List;
89
import java.util.NoSuchElementException;
10+
import java.util.stream.Stream;
911

1012
import javax.ws.rs.core.MultivaluedMap;
1113
import javax.ws.rs.core.Response;
@@ -274,4 +276,49 @@ public List<T> page(int pageNumber) {
274276
throw new RuntimeException(e);
275277
}
276278
}
279+
280+
/**
281+
* Gets all the items from each page as a single List instance.
282+
*
283+
* @return all the items from each page as a single List instance
284+
* @throws GitLabApiException if any error occurs
285+
*/
286+
public List<T> all() throws GitLabApiException {
287+
288+
// Make sure that current page is 0, this will ensure the whole list is fetched
289+
// regardless of what page the instance is currently on.
290+
currentPage = 0;
291+
List<T> allItems = new ArrayList<>(totalItems);
292+
293+
// Iterate through the pages and append each page of items to the list
294+
while (hasNext()) {
295+
allItems.addAll(next());
296+
}
297+
298+
return (allItems);
299+
}
300+
301+
/**
302+
* Builds and returns a Stream instance for streaming all the items from each page.
303+
*
304+
* @return a Stream instance for streaming all the items from each pag
305+
* @throws GitLabApiException if any error occurs
306+
*/
307+
public Stream<T> stream() throws GitLabApiException {
308+
309+
// Make sure that current page is 0, this will ensure the whole list is streamed
310+
// regardless of what page the instance is currently on.
311+
currentPage = 0;
312+
313+
// Create a Stream.Builder to ontain all the items. This is more efficient than
314+
// getting a List with all() and streaming that List
315+
Stream.Builder<T> streamBuilder = Stream.builder();
316+
317+
// Iterate through the pages and append each page of items to the stream builder
318+
while (hasNext()) {
319+
next().forEach(streamBuilder);
320+
}
321+
322+
return (streamBuilder.build());
323+
}
277324
}

src/test/java/org/gitlab4j/api/TestPager.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.gitlab4j.api.GitLabApi.ApiVersion;
1111
import org.gitlab4j.api.models.Branch;
12+
import org.gitlab4j.api.models.Commit;
1213
import org.gitlab4j.api.models.Member;
1314
import org.gitlab4j.api.models.Project;
1415
import org.junit.Before;
@@ -200,4 +201,43 @@ public void testMembersPager() throws GitLabApiException {
200201
}
201202
}
202203
}
204+
205+
@Test
206+
public void testAll() throws GitLabApiException {
207+
208+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
209+
assertNotNull(project);
210+
211+
Pager<Commit> pager = gitLabApi.getCommitsApi().getCommits(project, 1);
212+
assertNotNull(pager);
213+
assertEquals(1, pager.getItemsPerPage());
214+
assertTrue(0 < pager.getTotalPages());
215+
216+
int numCommits = pager.getTotalItems();
217+
assertTrue(0 < numCommits);
218+
219+
List<Commit> allCommits = pager.all();
220+
System.out.println("All commits:");
221+
allCommits.stream().map(Commit::getId).forEach(System.out::println);
222+
223+
assertEquals(numCommits, allCommits.size());
224+
}
225+
226+
@Test
227+
public void testStream() throws GitLabApiException {
228+
229+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
230+
assertNotNull(project);
231+
232+
Pager<Commit> pager = gitLabApi.getCommitsApi().getCommits(project, 1);
233+
assertNotNull(pager);
234+
assertEquals(1, pager.getItemsPerPage());
235+
assertTrue(0 < pager.getTotalPages());
236+
237+
int numCommits = pager.getTotalItems();
238+
assertTrue(0 < numCommits);
239+
240+
System.out.println("Streamed commits:");
241+
assertEquals(numCommits, pager.stream().map(Commit::getId).peek(System.out::println).count());
242+
}
203243
}

0 commit comments

Comments
 (0)