Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
bf0da685
Commit
bf0da685
authored
Dec 11, 2018
by
Greg Messner
Browse files
Added Java 8 stream support.
parent
6675a0af
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
bf0da685
...
...
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
```
java
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 {
<dependency>
<groupId>
org.gitlab4j
</groupId>
<artifactId>
gitlab4j-api
</artifactId>
<version>
4.9.
1
</version>
<version>
4.9.
2
</version>
</dependency>
```
...
...
@@ -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<T> Support
GitLab4J-API supports Java 8 Optional
<
T
>
for API calls that result in the return of a single item. Here is an example on how to use the Java 8 Optional
<
T
>
API calls:
...
...
src/main/java/org/gitlab4j/api/Pager.java
View file @
bf0da685
...
...
@@ -3,9 +3,11 @@ package org.gitlab4j.api;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.NoSuchElementException
;
import
java.util.stream.Stream
;
import
javax.ws.rs.core.MultivaluedMap
;
import
javax.ws.rs.core.Response
;
...
...
@@ -274,4 +276,49 @@ public class Pager<T> implements Iterator<List<T>>, Constants {
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
());
}
}
src/test/java/org/gitlab4j/api/TestPager.java
View file @
bf0da685
...
...
@@ -9,6 +9,7 @@ import java.util.List;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.Commit
;
import
org.gitlab4j.api.models.Member
;
import
org.gitlab4j.api.models.Project
;
import
org.junit.Before
;
...
...
@@ -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
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment