Skip to content

Commit 6fdda65

Browse files
committed
Rearraging and streamlining
1 parent 7857e89 commit 6fdda65

17 files changed

+311
-191
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
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>

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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
*/
1818
public class PagedIterable<T> implements Iterable<T> {
1919

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

2222
/**
2323
* Instantiates a new git hub page contents iterable.
2424
*/
25-
PagedIterable(PaginatedEndpoint<?, T> paginatedEndpoint) {
25+
<Page extends GitHubPage<T>> PagedIterable(PaginatedEndpoint<Page, T> paginatedEndpoint) {
2626
this.paginatedEndpoint = paginatedEndpoint;
2727
}
2828

2929
@Nonnull
30-
public final Iterator<T> iterator() {
31-
return paginatedEndpoint.iterator();
30+
public final PagedIterator<T> iterator() {
31+
return new PagedIterator<>(paginatedEndpoint.items());
3232
}
3333

3434
@Nonnull
@@ -61,6 +61,22 @@ public PagedIterable<T> withPageSize(int size) {
6161
return this;
6262
}
6363

64+
Iterator<T> itemIterator() {
65+
return paginatedEndpoint.items();
66+
}
67+
68+
PaginatedEndpointItems<? extends GitHubPage<T>, T> items() {
69+
return paginatedEndpoint.items();
70+
}
71+
72+
Iterator<? extends GitHubPage<T>> pageIterator() {
73+
return paginatedEndpoint.pages();
74+
}
75+
76+
PaginatedEndpointPages<? extends GitHubPage<T>, T> pages() {
77+
return paginatedEndpoint.pages();
78+
}
79+
6480
GitHubResponse<T[]> toResponse() throws IOException {
6581
return paginatedEndpoint.toResponse();
6682
}

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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44

5+
import java.util.Iterator;
6+
57
// TODO: Auto-generated Javadoc
68
/**
79
* {@link PagedIterable} enhanced to report search result specific information.
@@ -16,34 +18,42 @@
1618
justification = "Constructed by JSON API")
1719
public class PagedSearchIterable<T> extends PagedIterable<T> {
1820

19-
private final PaginatedEndpoint<? extends SearchResult<T>, T> paginatedEndpoint;
20-
21+
private final PaginatedEndpoint<? extends SearchResult<T>, T> searchPaginatedEndpoint;
2122
/**
2223
* Instantiates a new git hub page contents iterable.
2324
*/
2425
<Result extends SearchResult<T>> PagedSearchIterable(PaginatedEndpoint<Result, T> paginatedEndpoint) {
2526
super(paginatedEndpoint);
26-
this.paginatedEndpoint = paginatedEndpoint;
27+
this.searchPaginatedEndpoint = paginatedEndpoint;
2728
}
2829

2930
/**
3031
* Returns the total number of hit, including the results that's not yet fetched.
3132
*
3233
* @return the total count
34+
* @deprecated Use {@link #pageIterator()}.peek().totalCount
3335
*/
36+
@Deprecated
3437
public int getTotalCount() {
3538
// populate();
36-
return paginatedEndpoint.pages().peek().totalCount;
39+
return searchPaginatedEndpoint.pages().peek().totalCount;
3740
}
3841

3942
/**
4043
* Is incomplete boolean.
4144
*
4245
* @return the boolean
46+
* @deprecated Use {@link #pageIterator()}.peek().incompleteResults
4347
*/
48+
@Deprecated
4449
public boolean isIncomplete() {
4550
// populate();
46-
return paginatedEndpoint.pages().peek().incompleteResults;
51+
return searchPaginatedEndpoint.pages().peek().incompleteResults;
52+
}
53+
54+
@Override
55+
public Iterator<? extends SearchResult<T>> pageIterator() {
56+
return searchPaginatedEndpoint.pages();
4757
}
4858

4959
/**
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+
}

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

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

3-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4-
import org.jetbrains.annotations.NotNull;
5-
63
import java.io.IOException;
74
import java.lang.reflect.Array;
85
import java.util.*;
@@ -23,75 +20,7 @@
2320
*/
2421
class PaginatedEndpoint<Page extends GitHubPage<Item>, Item> implements Iterable<Item> {
2522

26-
private static class ArrayIterable<I> extends PaginatedEndpoint<GitHubArrayPage<I>, I> {
27-
28-
private class ArrayIterator extends PaginatedEndpointPages<GitHubArrayPage<I>, I> {
29-
30-
ArrayIterator(GitHubClient client,
31-
Class<GitHubArrayPage<I>> pageType,
32-
GitHubRequest request,
33-
int pageSize,
34-
Consumer<I> itemInitializer) {
35-
super(client, pageType, request, pageSize, itemInitializer);
36-
}
37-
38-
@Override
39-
@NotNull protected GitHubResponse<GitHubArrayPage<I>> sendNextRequest() throws IOException {
40-
GitHubResponse<I[]> response = client.sendRequest(nextRequest,
41-
(connectorResponse) -> GitHubResponse.parseBody(connectorResponse, receiverType));
42-
return new GitHubResponse<>(response, new GitHubArrayPage<>(response.body()));
43-
}
44-
45-
}
46-
47-
private final Class<I[]> receiverType;
48-
49-
private ArrayIterable(GitHubClient client,
50-
GitHubRequest request,
51-
Class<I[]> receiverType,
52-
Class<I> itemType,
53-
Consumer<I> itemInitializer) {
54-
super(client, request, GitHubArrayPage.getArrayPageClass(itemType), itemType, itemInitializer);
55-
this.receiverType = receiverType;
56-
}
57-
58-
@Nonnull
59-
@Override
60-
public PaginatedEndpointPages<GitHubArrayPage<I>, I> pages() {
61-
return new ArrayIterator(client, pageType, request, pageSize, itemInitializer);
62-
}
63-
}
64-
65-
/**
66-
* Represents a page of results
67-
*
68-
* @author Kohsuke Kawaguchi
69-
* @param <I>
70-
* the generic type
71-
*/
72-
private static class GitHubArrayPage<I> implements GitHubPage<I> {
73-
74-
@SuppressFBWarnings(value = { "DM_NEW_FOR_GETCLASS" }, justification = "BUG?")
75-
private static <P extends GitHubPage<I>, I> Class<P> getArrayPageClass(Class<I> itemType) {
76-
return (Class<P>) new GitHubArrayPage<>(itemType).getClass();
77-
}
78-
79-
private final I[] items;
80-
81-
public GitHubArrayPage(I[] items) {
82-
this.items = items;
83-
}
84-
85-
private GitHubArrayPage(Class<I> itemType) {
86-
this.items = null;
87-
}
88-
89-
public I[] getItems() {
90-
return items;
91-
}
92-
}
93-
94-
private static class SinglePageEndpoint<P extends GitHubPage<I>, I> extends PaginatedEndpoint<P, I> {
23+
static class SinglePageEndpoint<P extends GitHubPage<I>, I> extends PaginatedEndpoint<P, I> {
9524
private final P page;
9625

9726
SinglePageEndpoint(P page, Class<I> itemType) {
@@ -107,23 +36,20 @@ public PaginatedEndpointPages<P, I> pages() {
10736

10837
}
10938

110-
static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> fromSinglePage(I[] array, Class<I> itemType) {
111-
return fromSinglePage(new GitHubArrayPage<>(array), itemType);
39+
static <I> PaginatedEndpoint<GitHubPageArrayAdapter<I>, I> fromSinglePage(I[] array, Class<I> itemType) {
40+
return fromSinglePage(new GitHubPageArrayAdapter<>(array), itemType);
11241
}
11342

11443
static <P extends GitHubPage<I>, I> PaginatedEndpoint<P, I> fromSinglePage(P page, Class<I> itemType) {
11544
return new SinglePageEndpoint<>(page, itemType);
11645
}
11746

118-
static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> ofArrayEndpoint(GitHubClient client,
47+
static <I> PaginatedEndpoint<GitHubPage<I>, I> ofArrayEndpoint(GitHubClient client,
11948
GitHubRequest request,
12049
Class<I[]> receiverType,
12150
Consumer<I> itemInitializer) {
122-
return new ArrayIterable<I>(client,
123-
request,
124-
(Class<I[]>) receiverType,
125-
(Class<I>) receiverType.getComponentType(),
126-
itemInitializer);
51+
Class<I> itemType = (Class<I>) receiverType.getComponentType();
52+
return new PaginatedArrayEndpoint<I>(client, request, receiverType, itemType, itemInitializer);
12753
}
12854

12955
protected final GitHubClient client;
@@ -134,7 +60,6 @@ static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> ofArrayEndpoint(GitHubClient
13460
* Page size. 0 is default.
13561
*/
13662
protected int pageSize = 0;
137-
13863
protected final Class<Page> pageType;
13964

14065
protected final GitHubRequest request;
@@ -164,7 +89,7 @@ static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> ofArrayEndpoint(GitHubClient
16489
}
16590

16691
@Nonnull
167-
public final PaginatedEndpointItems<Item> items() {
92+
public final PaginatedEndpointItems<Page, Item> items() {
16893
return new PaginatedEndpointItems<>(this.pages());
16994
}
17095

0 commit comments

Comments
 (0)