Skip to content

Commit dadb032

Browse files
committed
Clean up spotbugs
1 parent c75c9c3 commit dadb032

File tree

9 files changed

+114
-49
lines changed

9 files changed

+114
-49
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private static PaginatedEndpoint<GHCommitFilesPage, File> createEndpointIterable
2323
PaginatedEndpoint<GHCommitFilesPage, File> iterable;
2424
if (files != null && files.length < GH_FILE_LIMIT_PER_COMMIT_PAGE) {
2525
// create a page iterator that only provides one page
26-
iterable = PaginatedEndpoint.ofSingleton(new GHCommitFilesPage(files));
26+
iterable = PaginatedEndpoint.ofSinglePage(new GHCommitFilesPage(files), GHCommit.File.class);
2727
} else {
2828
GitHubRequest request = owner.root()
2929
.createRequest()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public URL getHtmlUrl() {
261261
}
262262

263263
@Override
264+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
264265
public GHCompare.Commit[] getItems() {
265266
return commits;
266267
}
@@ -345,7 +346,7 @@ public PagedIterable<Commit> listCommits() {
345346
.withPageSize(10);
346347
} else {
347348
// if not using paginated commits, adapt the returned commits array
348-
return new PagedIterable<>(PaginatedEndpoint.ofSingleton(this.commits));
349+
return new PagedIterable<>(PaginatedEndpoint.ofSinglePage(this.commits, Commit.class));
349350
}
350351
}
351352

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import javax.annotation.Nonnull;
88

9-
// TODO: Auto-generated Javadoc
109
/**
1110
* {@link Iterable} that returns {@link PagedIterator}. {@link PagedIterable} is thread-safe but {@link PagedIterator}
1211
* is not. Any one instance of {@link PagedIterator} should only be called from a single thread.
@@ -19,6 +18,11 @@ public class PagedIterable<T> implements Iterable<T> {
1918

2019
private final PaginatedEndpoint<?, T> paginatedEndpoint;
2120

21+
@Deprecated
22+
public PagedIterable() {
23+
this(null);
24+
}
25+
2226
/**
2327
* Instantiates a new git hub page contents iterable.
2428
*/
@@ -50,11 +54,27 @@ public Set<T> toSet() throws IOException {
5054
return paginatedEndpoint.toSet();
5155
}
5256

53-
public PagedIterable<T> withPageSize(int i) {
54-
paginatedEndpoint.withPageSize(i);
57+
/**
58+
* Sets the pagination size.
59+
*
60+
* <p>
61+
* When set to non-zero, each API call will retrieve this many entries.
62+
*
63+
* @param size
64+
* the size
65+
* @return the paged iterable
66+
*/
67+
public PagedIterable<T> withPageSize(int size) {
68+
paginatedEndpoint.withPageSize(size);
5569
return this;
5670
}
5771

72+
@Nonnull
73+
@Deprecated
74+
protected T[] toArray(final PagedIterator<T> iterator) throws IOException {
75+
return paginatedEndpoint.toArray();
76+
}
77+
5878
GitHubResponse<T[]> toResponse() throws IOException {
5979
return paginatedEndpoint.toResponse();
6080
}

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

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

3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
35
import java.util.Iterator;
46
import java.util.List;
57

@@ -19,15 +21,19 @@
1921
*/
2022
public class PagedIterator<T> implements Iterator<T> {
2123

22-
private final PaginatedEndpointItems<?, T> endpointIterator;
24+
private final PaginatedEndpointItems<T> endpointIterator;
25+
26+
@Deprecated
27+
@SuppressFBWarnings(value = { "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" }, justification = "No longer used")
28+
protected Iterator<T[]> base = null;
2329

2430
/**
2531
* Instantiates a new paged iterator.
2632
*
2733
* @param endpointIterator
2834
* the base
2935
*/
30-
PagedIterator(PaginatedEndpointItems<?, T> endpointIterator) {
36+
PagedIterator(PaginatedEndpointItems<T> endpointIterator) {
3137
this.endpointIterator = endpointIterator;
3238
}
3339

@@ -49,4 +55,8 @@ public T next() {
4955
public List<T> nextPage() {
5056
return endpointIterator.nextPage();
5157
}
58+
59+
@Deprecated
60+
protected void wrapUp(T[] page) {
61+
}
5262
}

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

Lines changed: 19 additions & 0 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.
@@ -45,4 +47,21 @@ public boolean isIncomplete() {
4547
// populate();
4648
return paginatedEndpoint.pages().peek().incompleteResults;
4749
}
50+
51+
/**
52+
* With page size.
53+
*
54+
* @param size
55+
* the size
56+
* @return the paged search iterable
57+
*/
58+
@Override
59+
public PagedSearchIterable<T> withPageSize(int size) {
60+
return (PagedSearchIterable<T>) super.withPageSize(size);
61+
}
62+
63+
@Deprecated
64+
protected Iterator<T[]> adapt(final Iterator<? extends SearchResult<T>> base) {
65+
return null;
66+
}
4867
}

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

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

3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
34
import org.jetbrains.annotations.NotNull;
45

56
import java.io.IOException;
@@ -22,20 +23,20 @@
2223
*/
2324
class PaginatedEndpoint<Page extends GitHubPage<Item>, Item> implements Iterable<Item> {
2425

25-
private static class ArrayIterable<I> extends PaginatedEndpoint<GitHubPage<I>, I> {
26+
private static class ArrayIterable<I> extends PaginatedEndpoint<GitHubArrayPage<I>, I> {
2627

27-
private class ArrayIterator extends PaginatedEndpointPages<GitHubPage<I>, I> {
28+
private class ArrayIterator extends PaginatedEndpointPages<GitHubArrayPage<I>, I> {
2829

2930
ArrayIterator(GitHubClient client,
30-
Class<GitHubPage<I>> pageType,
31+
Class<GitHubArrayPage<I>> pageType,
3132
GitHubRequest request,
3233
int pageSize,
3334
Consumer<I> itemInitializer) {
3435
super(client, pageType, request, pageSize, itemInitializer);
3536
}
3637

3738
@Override
38-
@NotNull protected GitHubResponse<GitHubPage<I>> sendNextRequest() throws IOException {
39+
@NotNull protected GitHubResponse<GitHubArrayPage<I>> sendNextRequest() throws IOException {
3940
GitHubResponse<I[]> response = client.sendRequest(nextRequest,
4041
(connectorResponse) -> GitHubResponse.parseBody(connectorResponse, receiverType));
4142
return new GitHubResponse<>(response, new GitHubArrayPage<>(response.body()));
@@ -48,32 +49,31 @@ private class ArrayIterator extends PaginatedEndpointPages<GitHubPage<I>, I> {
4849
private ArrayIterable(GitHubClient client,
4950
GitHubRequest request,
5051
Class<I[]> receiverType,
52+
Class<I> itemType,
5153
Consumer<I> itemInitializer) {
52-
super(client,
53-
request,
54-
GitHubArrayPage.getArrayPageClass(receiverType),
55-
(Class<I>) receiverType.getComponentType(),
56-
itemInitializer);
54+
super(client, request, GitHubArrayPage.getArrayPageClass(itemType), itemType, itemInitializer);
5755
this.receiverType = receiverType;
5856
}
5957

60-
@NotNull @Override
61-
public PaginatedEndpointPages<GitHubPage<I>, I> pages() {
58+
@Nonnull
59+
@Override
60+
public PaginatedEndpointPages<GitHubArrayPage<I>, I> pages() {
6261
return new ArrayIterator(client, pageType, request, pageSize, itemInitializer);
6362
}
6463
}
6564

6665
/**
67-
* Represents the result of a search.
66+
* Represents a page of results
6867
*
6968
* @author Kohsuke Kawaguchi
7069
* @param <I>
7170
* the generic type
7271
*/
7372
private static class GitHubArrayPage<I> implements GitHubPage<I> {
7473

75-
private static <P extends GitHubPage<I>, I> Class<P> getArrayPageClass(Class<I[]> receiverType) {
76-
return (Class<P>) new GitHubArrayPage<>(receiverType).getClass();
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();
7777
}
7878

7979
private final I[] items;
@@ -82,35 +82,48 @@ public GitHubArrayPage(I[] items) {
8282
this.items = items;
8383
}
8484

85-
private GitHubArrayPage(Class<I[]> receiverType) {
86-
this.items = (I[]) Array.newInstance(receiverType.getComponentType(), 0);
85+
private GitHubArrayPage(Class<I> itemType) {
86+
this.items = null;
8787
}
8888

8989
public I[] getItems() {
9090
return items;
9191
}
9292
}
9393

94-
static <I> PaginatedEndpoint<GitHubPage<I>, I> ofArrayEndpoint(GitHubClient client,
94+
private static class SinglePage<P extends GitHubPage<I>, I> extends PaginatedEndpoint<P, I> {
95+
private final P page;
96+
97+
SinglePage(P page, Class<I> itemType) {
98+
super(null, null, (Class<P>) page.getClass(), itemType, null);
99+
this.page = page;
100+
}
101+
102+
@Nonnull
103+
@Override
104+
public PaginatedEndpointPages<P, I> pages() {
105+
return PaginatedEndpointPages.ofSinglePage(pageType, page);
106+
}
107+
108+
}
109+
110+
static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> ofArrayEndpoint(GitHubClient client,
95111
GitHubRequest request,
96112
Class<I[]> receiverType,
97113
Consumer<I> itemInitializer) {
98-
return new ArrayIterable<>(client, request, receiverType, itemInitializer);
114+
return new ArrayIterable<I>(client,
115+
request,
116+
(Class<I[]>) receiverType,
117+
(Class<I>) receiverType.getComponentType(),
118+
itemInitializer);
99119
}
100120

101-
static <I> PaginatedEndpoint<GitHubPage<I>, I> ofSingleton(I[] array) {
102-
return ofSingleton(new GitHubArrayPage<>(array));
121+
static <I> PaginatedEndpoint<GitHubArrayPage<I>, I> ofSinglePage(I[] array, Class<I> itemType) {
122+
return ofSinglePage(new GitHubArrayPage<>(array), itemType);
103123
}
104124

105-
static <P extends GitHubPage<I>, I> PaginatedEndpoint<P, I> ofSingleton(P page) {
106-
Class<I> itemType = (Class<I>) page.getItems().getClass().getComponentType();
107-
return new PaginatedEndpoint<>(null, null, (Class<P>) page.getClass(), itemType, null) {
108-
@Nonnull
109-
@Override
110-
public PaginatedEndpointPages<P, I> pages() {
111-
return PaginatedEndpointPages.ofSingleton(page);
112-
}
113-
};
125+
static <P extends GitHubPage<I>, I> PaginatedEndpoint<P, I> ofSinglePage(P page, Class<I> itemType) {
126+
return new SinglePage<>(page, itemType);
114127
}
115128

116129
protected final GitHubClient client;
@@ -151,9 +164,10 @@ public PaginatedEndpointPages<P, I> pages() {
151164
}
152165

153166
@Nonnull
154-
public final PaginatedEndpointItems<Page, Item> items() {
167+
public final PaginatedEndpointItems<Item> items() {
155168
return new PaginatedEndpointItems<>(this.pages());
156169
}
170+
157171
@Nonnull
158172
@Override
159173
public final Iterator<Item> iterator() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* This class is not thread-safe. Any one instance should only be called from a single thread.
77
*/
8-
class PaginatedEndpointItems<Page extends GitHubPage<Item>, Item> implements Iterator<Item> {
8+
class PaginatedEndpointItems<Item> implements Iterator<Item> {
99

1010
/**
1111
* Current batch of items. Each time {@link #next()} is called the next item in this array will be returned. After
@@ -14,7 +14,7 @@ class PaginatedEndpointItems<Page extends GitHubPage<Item>, Item> implements Ite
1414
*
1515
* @see #fetchNext() {@link #fetchNext()} for details on how this field is used.
1616
*/
17-
private Page currentPage;
17+
private GitHubPage<Item> currentPage;
1818

1919
/**
2020
* The index of the next item on the page, the item that will be returned when {@link #next()} is called.
@@ -23,9 +23,9 @@ class PaginatedEndpointItems<Page extends GitHubPage<Item>, Item> implements Ite
2323
*/
2424
private int nextItemIndex;
2525

26-
private final PaginatedEndpointPages<Page, Item> pageIterator;
26+
private final PaginatedEndpointPages<? extends GitHubPage<Item>, Item> pageIterator;
2727

28-
PaginatedEndpointItems(PaginatedEndpointPages<Page, Item> pageIterator) {
28+
PaginatedEndpointItems(PaginatedEndpointPages<? extends GitHubPage<Item>, Item> pageIterator) {
2929
this.pageIterator = pageIterator;
3030
}
3131

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
*/
2727
class PaginatedEndpointPages<P extends GitHubPage<Item>, Item> implements java.util.Iterator<P> {
2828

29-
static <P extends GitHubPage<Item>, Item> PaginatedEndpointPages<P, Item> ofSingleton(final P page) {
30-
return new PaginatedEndpointPages<>(page);
29+
static <P extends GitHubPage<Item>, Item> PaginatedEndpointPages<P, Item> ofSinglePage(Class<P> pageType,
30+
final P page) {
31+
return new PaginatedEndpointPages<>(pageType, page);
3132
}
3233
/**
3334
* When done iterating over pages, it is on rare occasions useful to be able to get information from the final
@@ -57,8 +58,8 @@ static <P extends GitHubPage<Item>, Item> PaginatedEndpointPages<P, Item> ofSing
5758

5859
protected final Class<P> pageType;
5960

60-
private PaginatedEndpointPages(P page) {
61-
this(null, (Class<P>) page.getClass(), null, 0, null);
61+
private PaginatedEndpointPages(Class<P> pageType, P page) {
62+
this(null, pageType, null, 0, null);
6263
this.next = page;
6364
}
6465

@@ -75,9 +76,10 @@ private PaginatedEndpointPages(P page) {
7576
request = builder.build();
7677
}
7778

78-
if (request != null && !"GET".equals(request.method())) {
79-
throw new IllegalArgumentException("Request method \"GET\" is required for page iterator.");
80-
}
79+
// if (request != null && !"GET".equals(request.method())) {
80+
// throw new IllegalArgumentException("Request method \"GET\" is required for page iterator.");
81+
// }
82+
// assert (request == null || "GET".equals(request.method()));
8183

8284
this.client = client;
8385
this.nextRequest = request;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,4 @@ public <R> PagedIterable<R> toIterable(Class<R[]> receiverType, Consumer<R> item
225225
GitHubRequest request = build();
226226
return new PagedIterable<>(PaginatedEndpoint.ofArrayEndpoint(client, request, receiverType, itemInitializer));
227227
}
228-
229228
}

0 commit comments

Comments
 (0)