Commit bf0da685 authored by Greg Messner's avatar Greg Messner
Browse files

Added Java 8 stream support.

parent 6675a0af
...@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep ...@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
```java ```java
dependencies { dependencies {
... ...
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.9.1' compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.9.2'
} }
``` ```
...@@ -22,7 +22,7 @@ dependencies { ...@@ -22,7 +22,7 @@ dependencies {
<dependency> <dependency>
<groupId>org.gitlab4j</groupId> <groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId> <artifactId>gitlab4j-api</artifactId>
<version>4.9.1</version> <version>4.9.2</version>
</dependency> </dependency>
``` ```
...@@ -130,6 +130,21 @@ while (projectsPager.hasNext())) { ...@@ -130,6 +130,21 @@ while (projectsPager.hasNext())) {
} }
} }
``` ```
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:
```java
// Get a Pager instance so we can load all the projects into a single list, 10 items at a time:
Pager<Project> projectPager = gitlabApi.getProjectsApi().getProjects(10);
List<Project> allProjects = projectPager.all();
```
```java
// Get a Pager instance to get a Stream<Project> instance.
Pager<Project> projectPager = gitlabApi.getProjectsApi().getProjects(10);
// Stream the Projects printing out the project name.
projectPager.stream().map(Project::getName).forEach(name -> System.out.println(name));
```
--- ---
## Java 8 Optional&lt;T&gt; Support ## Java 8 Optional&lt;T&gt; Support
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: 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:
......
...@@ -3,9 +3,11 @@ package org.gitlab4j.api; ...@@ -3,9 +3,11 @@ package org.gitlab4j.api;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.stream.Stream;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
...@@ -274,4 +276,49 @@ public class Pager<T> implements Iterator<List<T>>, Constants { ...@@ -274,4 +276,49 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
/**
* Gets all the items from each page as a single List instance.
*
* @return all the items from each page as a single List instance
* @throws GitLabApiException if any error occurs
*/
public List<T> all() throws GitLabApiException {
// Make sure that current page is 0, this will ensure the whole list is fetched
// regardless of what page the instance is currently on.
currentPage = 0;
List<T> allItems = new ArrayList<>(totalItems);
// Iterate through the pages and append each page of items to the list
while (hasNext()) {
allItems.addAll(next());
}
return (allItems);
}
/**
* Builds and returns a Stream instance for streaming all the items from each page.
*
* @return a Stream instance for streaming all the items from each pag
* @throws GitLabApiException if any error occurs
*/
public Stream<T> stream() throws GitLabApiException {
// Make sure that current page is 0, this will ensure the whole list is streamed
// regardless of what page the instance is currently on.
currentPage = 0;
// Create a Stream.Builder to ontain all the items. This is more efficient than
// getting a List with all() and streaming that List
Stream.Builder<T> streamBuilder = Stream.builder();
// Iterate through the pages and append each page of items to the stream builder
while (hasNext()) {
next().forEach(streamBuilder);
}
return (streamBuilder.build());
}
} }
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.junit.Before; import org.junit.Before;
...@@ -200,4 +201,43 @@ public class TestPager { ...@@ -200,4 +201,43 @@ public class TestPager {
} }
} }
} }
@Test
public void testAll() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Pager<Commit> pager = gitLabApi.getCommitsApi().getCommits(project, 1);
assertNotNull(pager);
assertEquals(1, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages());
int numCommits = pager.getTotalItems();
assertTrue(0 < numCommits);
List<Commit> allCommits = pager.all();
System.out.println("All commits:");
allCommits.stream().map(Commit::getId).forEach(System.out::println);
assertEquals(numCommits, allCommits.size());
}
@Test
public void testStream() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Pager<Commit> pager = gitLabApi.getCommitsApi().getCommits(project, 1);
assertNotNull(pager);
assertEquals(1, pager.getItemsPerPage());
assertTrue(0 < pager.getTotalPages());
int numCommits = pager.getTotalItems();
assertTrue(0 < numCommits);
System.out.println("Streamed commits:");
assertEquals(numCommits, pager.stream().map(Commit::getId).peek(System.out::println).count());
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment