40
40
import software .amazon .awssdk .codegen .poet .PoetUtils ;
41
41
import software .amazon .awssdk .core .async .SdkPublisher ;
42
42
import software .amazon .awssdk .core .pagination .async .AsyncPageFetcher ;
43
- import software .amazon .awssdk .core .pagination .async .EmptySubscription ;
44
43
import software .amazon .awssdk .core .pagination .async .PaginatedItemsPublisher ;
45
44
import software .amazon .awssdk .core .pagination .async .ResponsesSubscription ;
46
45
49
48
*/
50
49
public class AsyncResponseClassSpec extends PaginatorsClassSpec {
51
50
51
+ protected static final String LAST_PAGE_FIELD = "isLastPage" ;
52
52
private static final String SUBSCRIBER = "subscriber" ;
53
53
private static final String SUBSCRIBE_METHOD = "subscribe" ;
54
- private static final String LAST_PAGE_FIELD = "isLastPage" ;
55
54
56
55
public AsyncResponseClassSpec (IntermediateModel model , String c2jOperationName , PaginatorDefinition paginatorDefinition ) {
57
56
super (model , c2jOperationName , paginatorDefinition );
@@ -63,19 +62,14 @@ public TypeSpec poetSpec() {
63
62
.addModifiers (Modifier .PUBLIC )
64
63
.addAnnotation (PoetUtils .generatedAnnotation ())
65
64
.addSuperinterface (getAsyncResponseInterface ())
66
- .addFields (Stream .of (asyncClientInterfaceField (),
67
- requestClassField (),
68
- asyncPageFetcherField (),
69
- lastPageField ())
70
- .collect (Collectors .toList ()))
65
+ .addFields (fields ().collect (Collectors .toList ()))
71
66
.addMethod (publicConstructor ())
72
67
.addMethod (privateConstructor ())
73
68
.addMethod (subscribeMethod ())
74
69
.addMethods (getMethodSpecsForResultKeyList ())
75
- .addMethod (resumeMethod ())
76
70
.addJavadoc (paginationDocs .getDocsForAsyncResponseClass (
77
71
getAsyncClientInterfaceName ()))
78
- .addType (nextPageFetcherClass ());
72
+ .addType (nextPageFetcherClass (). build () );
79
73
80
74
return specBuilder .build ();
81
75
}
@@ -95,23 +89,30 @@ private TypeName getAsyncResponseInterface() {
95
89
/**
96
90
* @return A Poet {@link ClassName} for the async client interface
97
91
*/
98
- private ClassName getAsyncClientInterfaceName () {
92
+ protected ClassName getAsyncClientInterfaceName () {
99
93
return poetExtensions .getClientClass (model .getMetadata ().getAsyncInterface ());
100
94
}
101
95
102
- private FieldSpec asyncClientInterfaceField () {
96
+ protected Stream <FieldSpec > fields () {
97
+ return Stream .of (asyncClientInterfaceField (),
98
+ requestClassField (),
99
+ asyncPageFetcherField (),
100
+ lastPageField ());
101
+ }
102
+
103
+ protected FieldSpec asyncClientInterfaceField () {
103
104
return FieldSpec .builder (getAsyncClientInterfaceName (), CLIENT_MEMBER , Modifier .PRIVATE , Modifier .FINAL ).build ();
104
105
}
105
106
106
107
private FieldSpec asyncPageFetcherField () {
107
108
return FieldSpec .builder (AsyncPageFetcher .class , NEXT_PAGE_FETCHER_MEMBER , Modifier .PRIVATE , Modifier .FINAL ).build ();
108
109
}
109
110
110
- private FieldSpec lastPageField () {
111
+ protected FieldSpec lastPageField () {
111
112
return FieldSpec .builder (boolean .class , LAST_PAGE_FIELD , Modifier .PRIVATE ).build ();
112
113
}
113
114
114
- private MethodSpec publicConstructor () {
115
+ protected MethodSpec publicConstructor () {
115
116
return MethodSpec .constructorBuilder ()
116
117
.addModifiers (Modifier .PUBLIC )
117
118
.addParameter (getAsyncClientInterfaceName (), CLIENT_MEMBER )
@@ -120,7 +121,7 @@ private MethodSpec publicConstructor() {
120
121
.build ();
121
122
}
122
123
123
- private MethodSpec privateConstructor () {
124
+ protected MethodSpec privateConstructor () {
124
125
return MethodSpec .constructorBuilder ()
125
126
.addModifiers (Modifier .PRIVATE )
126
127
.addParameter (getAsyncClientInterfaceName (), CLIENT_MEMBER )
@@ -143,11 +144,16 @@ private MethodSpec subscribeMethod() {
143
144
.addParameter (ParameterizedTypeName .get (ClassName .get (Subscriber .class ),
144
145
WildcardTypeName .supertypeOf (responseType ())),
145
146
SUBSCRIBER )
146
- .addStatement ("$1L.onSubscribe($2T.builder().$1L($1L).$3L($3L).build())" ,
147
- SUBSCRIBER , ResponsesSubscription .class , NEXT_PAGE_FETCHER_MEMBER )
147
+ .addStatement ("$1L.onSubscribe($2T.builder().$1L($1L).$3L($4L).build())" ,
148
+ SUBSCRIBER , ResponsesSubscription .class ,
149
+ NEXT_PAGE_FETCHER_MEMBER , nextPageFetcherArgument ())
148
150
.build ();
149
151
}
150
152
153
+ protected String nextPageFetcherArgument () {
154
+ return NEXT_PAGE_FETCHER_MEMBER ;
155
+ }
156
+
151
157
/**
152
158
* Returns iterable of {@link MethodSpec} to generate helper methods for all members
153
159
* in {@link PaginatorDefinition#getResultKey()}.
@@ -220,7 +226,7 @@ PaginatedItemsPublisher.class, NEXT_PAGE_FETCHER_MEMBER, nextPageFetcherClassNam
220
226
* Generates a inner class that implements {@link AsyncPageFetcher}. This is a helper class that can be used
221
227
* to find if there are more pages in the response and to get the next page if exists.
222
228
*/
223
- private TypeSpec nextPageFetcherClass () {
229
+ protected TypeSpec . Builder nextPageFetcherClass () {
224
230
return TypeSpec .classBuilder (nextPageFetcherClassName ())
225
231
.addModifiers (Modifier .PRIVATE )
226
232
.addSuperinterface (ParameterizedTypeName .get (ClassName .get (AsyncPageFetcher .class ), responseType ()))
@@ -238,29 +244,6 @@ private TypeSpec nextPageFetcherClass() {
238
244
.returns (ParameterizedTypeName .get (ClassName .get (CompletableFuture .class ),
239
245
responseType ()))
240
246
.addCode (nextPageMethodBody ())
241
- .build ())
242
- .build ();
243
- }
244
-
245
- private MethodSpec resumeMethod () {
246
- return resumeMethodBuilder ().addCode (CodeBlock .builder ()
247
- .addStatement ("return $L" , anonymousClassWithEmptySubscription ())
248
- .build ())
249
- .build ();
250
- }
251
-
252
- private TypeSpec anonymousClassWithEmptySubscription () {
253
- return TypeSpec .anonymousClassBuilder ("$L, $L, true" , CLIENT_MEMBER , REQUEST_MEMBER )
254
- .addSuperinterface (className ())
255
- .addMethod (MethodSpec .methodBuilder (SUBSCRIBE_METHOD )
256
- .addAnnotation (Override .class )
257
- .addModifiers (Modifier .PUBLIC )
258
- .addParameter (ParameterizedTypeName .get (ClassName .get (Subscriber .class ),
259
- WildcardTypeName .supertypeOf (responseType ())),
260
- SUBSCRIBER )
261
- .addStatement ("$L.onSubscribe(new $T($L))" , SUBSCRIBER ,
262
- TypeName .get (EmptySubscription .class ), SUBSCRIBER )
263
- .build ())
264
- .build ();
247
+ .build ());
265
248
}
266
249
}
0 commit comments