26
26
import java .io .IOException ;
27
27
import java .util .ArrayList ;
28
28
import java .util .List ;
29
- import java .util .Objects ;
30
29
import java .util .function .BiConsumer ;
31
30
import java .util .function .BiFunction ;
32
31
import java .util .function .Consumer ;
@@ -207,16 +206,15 @@ public <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Contex
207
206
throw new IllegalArgumentException ("[type] is required" );
208
207
}
209
208
210
- if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER ) {
209
+ if (isConstructorArg ( consumer ) ) {
211
210
/*
212
- * Constructor arguments are detected by these "marker" consumers. It keeps the API looking clean even if it is a bit sleezy. We
213
- * then build a new consumer directly against the object parser that triggers the "constructor arg just arrived behavior" of the
214
- * parser. Conveniently, we can close over the position of the constructor in the argument list so we don't need to do any fancy
211
+ * Build a new consumer directly against the object parser that
212
+ * triggers the "constructor arg just arrived behavior" of the
213
+ * parser. Conveniently, we can close over the position of the
214
+ * constructor in the argument list so we don't need to do any fancy
215
215
* or expensive lookups whenever the constructor args come in.
216
216
*/
217
- int position = constructorArgInfos .size ();
218
- boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER ;
219
- constructorArgInfos .add (new ConstructorArgInfo (parseField , required ));
217
+ int position = addConstructorArg (consumer , parseField );
220
218
objectParser .declareField ((target , v ) -> target .constructorArg (position , v ), parser , parseField , type );
221
219
} else {
222
220
numberOfFields += 1 ;
@@ -237,19 +235,15 @@ public <T> void declareNamedObject(BiConsumer<Value, T> consumer, NamedObjectPar
237
235
throw new IllegalArgumentException ("[parseField] is required" );
238
236
}
239
237
240
- if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER ) {
238
+ if (isConstructorArg ( consumer ) ) {
241
239
/*
242
- * Constructor arguments are detected by this "marker" consumer. It
243
- * keeps the API looking clean even if it is a bit sleezy. We then
244
- * build a new consumer directly against the object parser that
240
+ * Build a new consumer directly against the object parser that
245
241
* triggers the "constructor arg just arrived behavior" of the
246
242
* parser. Conveniently, we can close over the position of the
247
243
* constructor in the argument list so we don't need to do any fancy
248
244
* or expensive lookups whenever the constructor args come in.
249
245
*/
250
- int position = constructorArgInfos .size ();
251
- boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER ;
252
- constructorArgInfos .add (new ConstructorArgInfo (parseField , required ));
246
+ int position = addConstructorArg (consumer , parseField );
253
247
objectParser .declareNamedObject ((target , v ) -> target .constructorArg (position , v ), namedObjectParser , parseField );
254
248
} else {
255
249
numberOfFields += 1 ;
@@ -260,6 +254,7 @@ public <T> void declareNamedObject(BiConsumer<Value, T> consumer, NamedObjectPar
260
254
@ Override
261
255
public <T > void declareNamedObjects (BiConsumer <Value , List <T >> consumer , NamedObjectParser <T , Context > namedObjectParser ,
262
256
ParseField parseField ) {
257
+
263
258
if (consumer == null ) {
264
259
throw new IllegalArgumentException ("[consumer] is required" );
265
260
}
@@ -270,19 +265,15 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
270
265
throw new IllegalArgumentException ("[parseField] is required" );
271
266
}
272
267
273
- if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER ) {
268
+ if (isConstructorArg ( consumer ) ) {
274
269
/*
275
- * Constructor arguments are detected by this "marker" consumer. It
276
- * keeps the API looking clean even if it is a bit sleezy. We then
277
- * build a new consumer directly against the object parser that
270
+ * Build a new consumer directly against the object parser that
278
271
* triggers the "constructor arg just arrived behavior" of the
279
272
* parser. Conveniently, we can close over the position of the
280
273
* constructor in the argument list so we don't need to do any fancy
281
274
* or expensive lookups whenever the constructor args come in.
282
275
*/
283
- int position = constructorArgInfos .size ();
284
- boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER ;
285
- constructorArgInfos .add (new ConstructorArgInfo (parseField , required ));
276
+ int position = addConstructorArg (consumer , parseField );
286
277
objectParser .declareNamedObjects ((target , v ) -> target .constructorArg (position , v ), namedObjectParser , parseField );
287
278
} else {
288
279
numberOfFields += 1 ;
@@ -306,19 +297,15 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
306
297
throw new IllegalArgumentException ("[parseField] is required" );
307
298
}
308
299
309
- if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER ) {
300
+ if (isConstructorArg ( consumer ) ) {
310
301
/*
311
- * Constructor arguments are detected by this "marker" consumer. It
312
- * keeps the API looking clean even if it is a bit sleezy. We then
313
- * build a new consumer directly against the object parser that
302
+ * Build a new consumer directly against the object parser that
314
303
* triggers the "constructor arg just arrived behavior" of the
315
304
* parser. Conveniently, we can close over the position of the
316
305
* constructor in the argument list so we don't need to do any fancy
317
306
* or expensive lookups whenever the constructor args come in.
318
307
*/
319
- int position = constructorArgInfos .size ();
320
- boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER ;
321
- constructorArgInfos .add (new ConstructorArgInfo (parseField , required ));
308
+ int position = addConstructorArg (consumer , parseField );
322
309
objectParser .declareNamedObjects ((target , v ) -> target .constructorArg (position , v ), namedObjectParser ,
323
310
wrapOrderedModeCallBack (orderedModeCallback ), parseField );
324
311
} else {
@@ -327,6 +314,27 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
327
314
wrapOrderedModeCallBack (orderedModeCallback ), parseField );
328
315
}
329
316
}
317
+
318
+ /**
319
+ * Constructor arguments are detected by this "marker" consumer. It
320
+ * keeps the API looking clean even if it is a bit sleezy.
321
+ */
322
+ private boolean isConstructorArg (BiConsumer <?, ?> consumer ) {
323
+ return consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER ;
324
+ }
325
+
326
+ /**
327
+ * Add a constructor argument
328
+ * @param consumer Either {@link #REQUIRED_CONSTRUCTOR_ARG_MARKER} or {@link #REQUIRED_CONSTRUCTOR_ARG_MARKER}
329
+ * @param parseField Parse field
330
+ * @return The argument position
331
+ */
332
+ private int addConstructorArg (BiConsumer <?, ?> consumer , ParseField parseField ) {
333
+ int position = constructorArgInfos .size ();
334
+ boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER ;
335
+ constructorArgInfos .add (new ConstructorArgInfo (parseField , required ));
336
+ return position ;
337
+ }
330
338
331
339
@ Override
332
340
public String getName () {
0 commit comments