@@ -51,13 +51,15 @@ public class EvaluatorImplementer {
51
51
private final ProcessFunction processFunction ;
52
52
private final ClassName implementation ;
53
53
private final boolean processOutputsMultivalued ;
54
+ private final boolean allNullsIsNull ;
54
55
55
56
public EvaluatorImplementer (
56
57
Elements elements ,
57
58
javax .lang .model .util .Types types ,
58
59
ExecutableElement processFunction ,
59
60
String extraName ,
60
- List <TypeMirror > warnExceptions
61
+ List <TypeMirror > warnExceptions ,
62
+ boolean allNullsIsNull
61
63
) {
62
64
this .declarationType = (TypeElement ) processFunction .getEnclosingElement ();
63
65
this .processFunction = new ProcessFunction (types , processFunction , warnExceptions );
@@ -66,7 +68,8 @@ public EvaluatorImplementer(
66
68
elements .getPackageOf (declarationType ).toString (),
67
69
declarationType .getSimpleName () + extraName + "Evaluator"
68
70
);
69
- this .processOutputsMultivalued = this .processFunction .hasBlockType && (this .processFunction .builderArg != null );
71
+ this .processOutputsMultivalued = this .processFunction .hasBlockType ;
72
+ this .allNullsIsNull = allNullsIsNull ;
70
73
}
71
74
72
75
public JavaFile sourceFile () {
@@ -131,7 +134,7 @@ private MethodSpec ctor() {
131
134
MethodSpec .Builder builder = MethodSpec .constructorBuilder ().addModifiers (Modifier .PUBLIC );
132
135
builder .addParameter (SOURCE , "source" );
133
136
builder .addStatement ("this.source = source" );
134
- processFunction .args .stream (). forEach (a -> a .implementCtor (builder ));
137
+ processFunction .args .forEach (a -> a .implementCtor (builder ));
135
138
builder .addParameter (DRIVER_CONTEXT , "driverContext" );
136
139
builder .addStatement ("this.driverContext = driverContext" );
137
140
return builder .build ();
@@ -140,15 +143,15 @@ private MethodSpec ctor() {
140
143
private MethodSpec eval () {
141
144
MethodSpec .Builder builder = MethodSpec .methodBuilder ("eval" ).addAnnotation (Override .class );
142
145
builder .addModifiers (Modifier .PUBLIC ).returns (BLOCK ).addParameter (PAGE , "page" );
143
- processFunction .args .stream (). forEach (a -> a .evalToBlock (builder ));
146
+ processFunction .args .forEach (a -> a .evalToBlock (builder ));
144
147
String invokeBlockEval = invokeRealEval (true );
145
148
if (processOutputsMultivalued ) {
146
149
builder .addStatement (invokeBlockEval );
147
150
} else {
148
- processFunction .args .stream (). forEach (a -> a .resolveVectors (builder , invokeBlockEval ));
151
+ processFunction .args .forEach (a -> a .resolveVectors (builder , invokeBlockEval ));
149
152
builder .addStatement (invokeRealEval (false ));
150
153
}
151
- processFunction .args .stream (). forEach (a -> a .closeEvalToBlock (builder ));
154
+ processFunction .args .forEach (a -> a .closeEvalToBlock (builder ));
152
155
return builder .build ();
153
156
}
154
157
@@ -157,7 +160,7 @@ private String invokeRealEval(boolean blockStyle) {
157
160
158
161
String params = processFunction .args .stream ()
159
162
.map (a -> a .paramName (blockStyle ))
160
- .filter (a -> a != null )
163
+ .filter (Objects :: nonNull )
161
164
.collect (Collectors .joining (", " ));
162
165
if (params .length () > 0 ) {
163
166
builder .append (", " );
@@ -189,23 +192,23 @@ private MethodSpec realEval(boolean blockStyle) {
189
192
buildFromFactory (builderType )
190
193
);
191
194
{
192
- processFunction .args .stream (). forEach (a -> {
195
+ processFunction .args .forEach (a -> {
193
196
if (a .paramName (blockStyle ) != null ) {
194
197
builder .addParameter (a .dataType (blockStyle ), a .paramName (blockStyle ));
195
198
}
196
199
});
197
200
198
- processFunction .args .stream (). forEach (a -> a .createScratch (builder ));
201
+ processFunction .args .forEach (a -> a .createScratch (builder ));
199
202
200
203
builder .beginControlFlow ("position: for (int p = 0; p < positionCount; p++)" );
201
204
{
202
205
if (blockStyle ) {
203
206
if (processOutputsMultivalued == false ) {
204
- processFunction .args .stream (). forEach (a -> a .skipNull (builder ));
205
- } else {
207
+ processFunction .args .forEach (a -> a .skipNull (builder ));
208
+ } else if ( allNullsIsNull ) {
206
209
builder .addStatement ("boolean allBlocksAreNulls = true" );
207
210
// allow block type inputs to be null
208
- processFunction .args .stream (). forEach (a -> a .allBlocksAreNull (builder ));
211
+ processFunction .args .forEach (a -> a .allBlocksAreNull (builder ));
209
212
210
213
builder .beginControlFlow ("if (allBlocksAreNulls)" );
211
214
{
@@ -214,25 +217,30 @@ private MethodSpec realEval(boolean blockStyle) {
214
217
}
215
218
builder .endControlFlow ();
216
219
}
220
+ } else {
221
+ assert allNullsIsNull : "allNullsIsNull == false is only supported for block style." ;
217
222
}
218
- processFunction .args .stream (). forEach (a -> a .read (builder , blockStyle ));
223
+ processFunction .args .forEach (a -> a .read (builder , blockStyle ));
219
224
220
225
StringBuilder pattern = new StringBuilder ();
221
226
List <Object > args = new ArrayList <>();
222
227
pattern .append ("$T.$N(" );
223
228
args .add (declarationType );
224
229
args .add (processFunction .function .getSimpleName ());
225
- processFunction .args .stream ().forEach (a -> {
226
- if (args .size () > 2 ) {
227
- pattern .append (", " );
228
- }
229
- a .buildInvocation (pattern , args , blockStyle );
230
- });
230
+ pattern .append (processFunction .args .stream ().map (argument -> {
231
+ var invocation = new StringBuilder ();
232
+ argument .buildInvocation (invocation , args , blockStyle );
233
+ return invocation .toString ();
234
+ }).collect (Collectors .joining (", " )));
231
235
pattern .append (")" );
232
236
String builtPattern ;
233
237
if (processFunction .builderArg == null ) {
234
- builtPattern = vectorize ? "result.$L(p, " + pattern + ")" : "result.$L(" + pattern + ")" ;
235
- args .add (0 , processFunction .appendMethod ());
238
+ if (vectorize ) {
239
+ builtPattern = "result.$L(p, " + pattern + ")" ;
240
+ } else {
241
+ builtPattern = "result.$L(" + pattern + ")" ;
242
+ }
243
+ args .addFirst (processFunction .appendMethod ());
236
244
} else {
237
245
builtPattern = pattern .toString ();
238
246
}
@@ -246,7 +254,7 @@ private MethodSpec realEval(boolean blockStyle) {
246
254
String catchPattern = "catch ("
247
255
+ processFunction .warnExceptions .stream ().map (m -> "$T" ).collect (Collectors .joining (" | " ))
248
256
+ " e)" ;
249
- builder .nextControlFlow (catchPattern , processFunction .warnExceptions .stream ().map (m -> TypeName . get ( m ) ).toArray ());
257
+ builder .nextControlFlow (catchPattern , processFunction .warnExceptions .stream ().map (TypeName :: get ).toArray ());
250
258
builder .addStatement ("warnings().registerException(e)" );
251
259
builder .addStatement ("result.appendNull()" );
252
260
builder .endControlFlow ();
@@ -282,7 +290,7 @@ private TypeSpec factory() {
282
290
builder .addModifiers (Modifier .STATIC );
283
291
284
292
builder .addField (SOURCE , "source" , Modifier .PRIVATE , Modifier .FINAL );
285
- processFunction .args .stream (). forEach (a -> a .declareFactoryField (builder ));
293
+ processFunction .args .forEach (a -> a .declareFactoryField (builder ));
286
294
287
295
builder .addMethod (processFunction .factoryCtor ());
288
296
builder .addMethod (processFunction .factoryGet (implementation ));
@@ -371,7 +379,7 @@ MethodSpec factoryCtor() {
371
379
MethodSpec .Builder builder = MethodSpec .constructorBuilder ().addModifiers (Modifier .PUBLIC );
372
380
builder .addParameter (SOURCE , "source" );
373
381
builder .addStatement ("this.source = source" );
374
- args .stream (). forEach (a -> a .implementFactoryCtor (builder ));
382
+ args .forEach (a -> a .implementFactoryCtor (builder ));
375
383
return builder .build ();
376
384
}
377
385
0 commit comments