Class InstantiatingObjectParser<Value,Context>
java.lang.Object
org.elasticsearch.common.xcontent.InstantiatingObjectParser<Value,Context>
- All Implemented Interfaces:
java.util.function.BiFunction<XContentParser,Context,Value>
,ContextParser<Context,Value>
public class InstantiatingObjectParser<Value,Context> extends java.lang.Object implements java.util.function.BiFunction<XContentParser,Context,Value>, ContextParser<Context,Value>
Like
ConstructingObjectParser
but works with objects which have a constructor that matches declared fields.
Declaring a InstantiatingObjectParser is intentionally quite similar to declaring an ConstructingObjectParser with two important differences.
The main differences being that it is using Builder to construct the parser and takes a class of the target object instead of the object builder. The target object must have exactly one constructor with the number and order of arguments matching the number of order of declared fields. If there are more then 2 constructors with the same number of arguments, one of them needs to be marked with ParserConstructor annotation.
public static class Thing{
public Thing(String animal, String vegetable, int mineral) {
....
}
public void setFruit(int fruit) { ... }
public void setBug(int bug) { ... }
}
private static final InstantiatingObjectParser<Thing, SomeContext> PARSER = new InstantiatingObjectParser<>("thing", Thing.class);
static {
PARSER.declareString(constructorArg(), new ParseField("animal"));
PARSER.declareString(constructorArg(), new ParseField("vegetable"));
PARSER.declareInt(optionalConstructorArg(), new ParseField("mineral"));
PARSER.declareInt(Thing::setFruit, new ParseField("fruit"));
PARSER.declareInt(Thing::setBug, new ParseField("bug"));
PARSER.finalizeFields()
}
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InstantiatingObjectParser.Builder<Value,Context>
-
Method Summary
Modifier and Type Method Description Value
apply(XContentParser xContentParser, Context context)
static <Value, Context>
InstantiatingObjectParser.Builder<Value,Context>builder(java.lang.String name, boolean ignoreUnknownFields, java.lang.Class<Value> valueClass)
static <Value, Context>
InstantiatingObjectParser.Builder<Value,Context>builder(java.lang.String name, java.lang.Class<Value> valueClass)
Value
parse(XContentParser parser, Context context)
-
Method Details
-
builder
public static <Value, Context> InstantiatingObjectParser.Builder<Value,Context> builder(java.lang.String name, boolean ignoreUnknownFields, java.lang.Class<Value> valueClass) -
builder
public static <Value, Context> InstantiatingObjectParser.Builder<Value,Context> builder(java.lang.String name, java.lang.Class<Value> valueClass) -
parse
- Specified by:
parse
in interfaceContextParser<Value,Context>
- Throws:
java.io.IOException
-
apply
- Specified by:
apply
in interfacejava.util.function.BiFunction<XContentParser,Context,Value>
-