Skip to content

Commit f6656fa

Browse files
committed
Small refactor removing duplicate code
1 parent 3468a68 commit f6656fa

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/ConstructingObjectParser.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.IOException;
2727
import java.util.ArrayList;
2828
import java.util.List;
29-
import java.util.Objects;
3029
import java.util.function.BiConsumer;
3130
import java.util.function.BiFunction;
3231
import java.util.function.Consumer;
@@ -207,16 +206,15 @@ public <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Contex
207206
throw new IllegalArgumentException("[type] is required");
208207
}
209208

210-
if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER) {
209+
if (isConstructorArg(consumer)) {
211210
/*
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
215215
* or expensive lookups whenever the constructor args come in.
216216
*/
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);
220218
objectParser.declareField((target, v) -> target.constructorArg(position, v), parser, parseField, type);
221219
} else {
222220
numberOfFields += 1;
@@ -237,19 +235,15 @@ public <T> void declareNamedObject(BiConsumer<Value, T> consumer, NamedObjectPar
237235
throw new IllegalArgumentException("[parseField] is required");
238236
}
239237

240-
if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER) {
238+
if (isConstructorArg(consumer)) {
241239
/*
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
245241
* triggers the "constructor arg just arrived behavior" of the
246242
* parser. Conveniently, we can close over the position of the
247243
* constructor in the argument list so we don't need to do any fancy
248244
* or expensive lookups whenever the constructor args come in.
249245
*/
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);
253247
objectParser.declareNamedObject((target, v) -> target.constructorArg(position, v), namedObjectParser, parseField);
254248
} else {
255249
numberOfFields += 1;
@@ -260,6 +254,7 @@ public <T> void declareNamedObject(BiConsumer<Value, T> consumer, NamedObjectPar
260254
@Override
261255
public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedObjectParser<T, Context> namedObjectParser,
262256
ParseField parseField) {
257+
263258
if (consumer == null) {
264259
throw new IllegalArgumentException("[consumer] is required");
265260
}
@@ -270,19 +265,15 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
270265
throw new IllegalArgumentException("[parseField] is required");
271266
}
272267

273-
if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER) {
268+
if (isConstructorArg(consumer)) {
274269
/*
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
278271
* triggers the "constructor arg just arrived behavior" of the
279272
* parser. Conveniently, we can close over the position of the
280273
* constructor in the argument list so we don't need to do any fancy
281274
* or expensive lookups whenever the constructor args come in.
282275
*/
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);
286277
objectParser.declareNamedObjects((target, v) -> target.constructorArg(position, v), namedObjectParser, parseField);
287278
} else {
288279
numberOfFields += 1;
@@ -306,19 +297,15 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
306297
throw new IllegalArgumentException("[parseField] is required");
307298
}
308299

309-
if (consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER || consumer == OPTIONAL_CONSTRUCTOR_ARG_MARKER) {
300+
if (isConstructorArg(consumer)) {
310301
/*
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
314303
* triggers the "constructor arg just arrived behavior" of the
315304
* parser. Conveniently, we can close over the position of the
316305
* constructor in the argument list so we don't need to do any fancy
317306
* or expensive lookups whenever the constructor args come in.
318307
*/
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);
322309
objectParser.declareNamedObjects((target, v) -> target.constructorArg(position, v), namedObjectParser,
323310
wrapOrderedModeCallBack(orderedModeCallback), parseField);
324311
} else {
@@ -327,6 +314,27 @@ public <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, NamedOb
327314
wrapOrderedModeCallBack(orderedModeCallback), parseField);
328315
}
329316
}
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+
}
330338

331339
@Override
332340
public String getName() {

0 commit comments

Comments
 (0)