11package org .kohsuke .github ;
22
3+ import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
34import org .jetbrains .annotations .NotNull ;
45
56import java .io .IOException ;
2223 */
2324class 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 () {
0 commit comments