Skip to content

Commit 4948299

Browse files
committed
Rearraging and streamlining
1 parent 7857e89 commit 4948299

17 files changed

+365
-224
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
<dependency>
146146
<groupId>com.fasterxml.jackson.datatype</groupId>
147147
<artifactId>jackson-datatype-jsr310</artifactId>
148+
<version>2.20.0</version>
148149
</dependency>
149150
<dependency>
150151
<groupId>com.github.spotbugs</groupId>
@@ -550,7 +551,6 @@
550551
<exclude>org.kohsuke.github.PagedIterable#toArray(org.kohsuke.github.PagedIterator)</exclude>
551552
<exclude>org.kohsuke.github.PagedIterable#PagedIterable()</exclude>
552553
<exclude>org.kohsuke.github.PagedIterator#base</exclude>
553-
<exclude>org.kohsuke.github.PagedIterable#iterator()</exclude>
554554
<exclude>org.kohsuke.github.PagedIterator#wrapUp(java.lang.Object[])</exclude>
555555
<exclude>org.kohsuke.github.PagedSearchIterable#_iterator(int)</exclude>
556556
<exclude>org.kohsuke.github.PagedSearchIterable#adapt(java.util.Iterator)</exclude>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.kohsuke.github;
22

3+
import java.util.Arrays;
4+
import java.util.List;
5+
36
/**
47
* A page of results from GitHub.
58
*
@@ -13,4 +16,8 @@ interface GitHubPage<I> {
1316
* @return the items
1417
*/
1518
I[] getItems();
19+
20+
default List<I> getItemsList() {
21+
return Arrays.asList(this.getItems());
22+
}
1623
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Represents a page of results
5+
*
6+
* @author Kohsuke Kawaguchi
7+
* @param <I>
8+
* the generic type
9+
*/
10+
class GitHubPageArrayAdapter<I> implements GitHubPage<I> {
11+
12+
private final I[] items;
13+
14+
public GitHubPageArrayAdapter(I[] items) {
15+
this.items = items;
16+
}
17+
18+
public I[] getItems() {
19+
return items;
20+
}
21+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.kohsuke.github;
22

33
import java.io.IOException;
4-
import java.util.Iterator;
54
import java.util.List;
65
import java.util.Set;
76

@@ -17,18 +16,18 @@
1716
*/
1817
public class PagedIterable<T> implements Iterable<T> {
1918

20-
private final PaginatedEndpoint<?, T> paginatedEndpoint;
19+
private final PaginatedEndpoint<? extends GitHubPage<T>, T> paginatedEndpoint;
2120

2221
/**
2322
* Instantiates a new git hub page contents iterable.
2423
*/
25-
PagedIterable(PaginatedEndpoint<?, T> paginatedEndpoint) {
24+
<Page extends GitHubPage<T>> PagedIterable(PaginatedEndpoint<Page, T> paginatedEndpoint) {
2625
this.paginatedEndpoint = paginatedEndpoint;
2726
}
2827

2928
@Nonnull
30-
public final Iterator<T> iterator() {
31-
return paginatedEndpoint.iterator();
29+
public final PagedIterator<T> iterator() {
30+
return new PagedIterator<>(items());
3231
}
3332

3433
@Nonnull
@@ -61,6 +60,14 @@ public PagedIterable<T> withPageSize(int size) {
6160
return this;
6261
}
6362

63+
PaginatedEndpointItems<? extends GitHubPage<T>, T> items() {
64+
return paginatedEndpoint.items();
65+
}
66+
67+
PaginatedEndpointPages<? extends GitHubPage<T>, T> pages() {
68+
return paginatedEndpoint.pages();
69+
}
70+
6471
GitHubResponse<T[]> toResponse() throws IOException {
6572
return paginatedEndpoint.toResponse();
6673
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
* @param <T>
1818
* the type parameter
1919
*/
20+
@Deprecated
2021
public class PagedIterator<T> implements Iterator<T> {
2122

22-
private final PaginatedEndpointItems<T> endpointIterator;
23+
private final PaginatedEndpointItems<? extends GitHubPage<T>, T> endpointIterator;
2324

2425
/**
2526
* Instantiates a new paged iterator.
2627
*
2728
* @param endpointIterator
2829
* the base
2930
*/
30-
PagedIterator(PaginatedEndpointItems<T> endpointIterator) {
31+
PagedIterator(PaginatedEndpointItems<? extends GitHubPage<T>, T> endpointIterator) {
3132
this.endpointIterator = endpointIterator;
3233
}
3334

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,35 @@
1616
justification = "Constructed by JSON API")
1717
public class PagedSearchIterable<T> extends PagedIterable<T> {
1818

19-
private final PaginatedEndpoint<? extends SearchResult<T>, T> paginatedEndpoint;
20-
19+
private final PaginatedEndpoint<? extends SearchResult<T>, T> searchPaginatedEndpoint;
2120
/**
2221
* Instantiates a new git hub page contents iterable.
2322
*/
2423
<Result extends SearchResult<T>> PagedSearchIterable(PaginatedEndpoint<Result, T> paginatedEndpoint) {
2524
super(paginatedEndpoint);
26-
this.paginatedEndpoint = paginatedEndpoint;
25+
this.searchPaginatedEndpoint = paginatedEndpoint;
2726
}
2827

2928
/**
3029
* Returns the total number of hit, including the results that's not yet fetched.
3130
*
3231
* @return the total count
3332
*/
33+
@Deprecated
3434
public int getTotalCount() {
3535
// populate();
36-
return paginatedEndpoint.pages().peek().totalCount;
36+
return searchPaginatedEndpoint.pages().peek().totalCount;
3737
}
3838

3939
/**
4040
* Is incomplete boolean.
4141
*
4242
* @return the boolean
4343
*/
44+
@Deprecated
4445
public boolean isIncomplete() {
4546
// populate();
46-
return paginatedEndpoint.pages().peek().incompleteResults;
47+
return searchPaginatedEndpoint.pages().peek().incompleteResults;
4748
}
4849

4950
/**
@@ -57,4 +58,16 @@ public boolean isIncomplete() {
5758
public PagedSearchIterable<T> withPageSize(int size) {
5859
return (PagedSearchIterable<T>) super.withPageSize(size);
5960
}
61+
62+
@Override
63+
PaginatedEndpointItems<? extends GitHubPage<T>, T> items() {
64+
// TODO Auto-generated method stub
65+
return super.items();
66+
}
67+
68+
@Override
69+
PaginatedEndpointPages<? extends GitHubPage<T>, T> pages() {
70+
// TODO Auto-generated method stub
71+
return super.pages();
72+
}
6073
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.kohsuke.github;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.io.IOException;
6+
import java.util.*;
7+
import java.util.function.Consumer;
8+
9+
import javax.annotation.Nonnull;
10+
11+
/**
12+
* {@link PaginatedEndpoint} implementation that take a {@link Consumer} that initializes all the items on each page as
13+
* they are retrieved.
14+
*
15+
* {@link PaginatedEndpoint} is immutable and thread-safe, but the iterator returned from {@link #iterator()} is not.
16+
* Any one instance of iterator should only be called from a single thread.
17+
*
18+
* @author Liam Newman
19+
* @param <Item>
20+
* the type of items on each page
21+
*/
22+
class PaginatedArrayEndpoint<Item> extends PaginatedEndpoint<GitHubPage<Item>, Item> {
23+
24+
private class ArrayPages extends PaginatedEndpointPages<GitHubPage<Item>, Item> {
25+
26+
ArrayPages(GitHubClient client,
27+
Class<GitHubPage<Item>> pageType,
28+
GitHubRequest request,
29+
int pageSize,
30+
Consumer<Item> itemInitializer) {
31+
super(client, pageType, request, pageSize, itemInitializer);
32+
}
33+
34+
@Override
35+
@NotNull protected GitHubResponse<GitHubPage<Item>> sendNextRequest() throws IOException {
36+
GitHubResponse<Item[]> response = client.sendRequest(nextRequest,
37+
(connectorResponse) -> GitHubResponse.parseBody(connectorResponse, arrayReceiverType));
38+
return new GitHubResponse<>(response, new GitHubPageArrayAdapter<>(response.body()));
39+
}
40+
}
41+
42+
/**
43+
* Pretend to get a specific page class type for the sake of the compile time strong typing.
44+
*
45+
* This class never uses {@code pageType}, so it is safe to pass null as the actual class value.
46+
*
47+
* @param <I>
48+
* The type of items in the array page.
49+
* @param itemType
50+
* The class instance for items in the array page.
51+
* @return Always null, but cast to the appropriate class for compile time strong typing.
52+
*/
53+
private static <I> Class<GitHubPage<I>> getArrayPageClass(Class<I> itemType) {
54+
return (Class<GitHubPage<I>>) null;
55+
}
56+
57+
private final Class<Item[]> arrayReceiverType;
58+
59+
PaginatedArrayEndpoint(GitHubClient client,
60+
GitHubRequest request,
61+
Class<Item[]> arrayReceiverType,
62+
Class<Item> itemType,
63+
Consumer<Item> itemInitializer) {
64+
super(client, request, getArrayPageClass(itemType), itemType, itemInitializer);
65+
this.arrayReceiverType = arrayReceiverType;
66+
}
67+
68+
@Nonnull
69+
@Override
70+
public PaginatedEndpointPages<GitHubPage<Item>, Item> pages() {
71+
return new ArrayPages(client, pageType, request, pageSize, itemInitializer);
72+
}
73+
}

0 commit comments

Comments
 (0)