Package org.elasticsearch.xcontent
Class InstantiatingObjectParser<Value,Context>
java.lang.Object
org.elasticsearch.xcontent.InstantiatingObjectParser<Value,Context>
- All Implemented Interfaces:
BiFunction<XContentParser,
,Context, Value> ContextParser<Context,
Value>
public class InstantiatingObjectParser<Value,Context>
extends Object
implements 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
-
Method Summary
Modifier and TypeMethodDescriptionapply
(XContentParser xContentParser, Context context) static <Value,
Context>
InstantiatingObjectParser.Builder<Value,Context> static <Value,
Context>
InstantiatingObjectParser.Builder<Value,Context> parse
(XContentParser parser, Context context) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.function.BiFunction
andThen
-
Method Details
-
builder
public static <Value,Context> InstantiatingObjectParser.Builder<Value,Context> builder(String name, boolean ignoreUnknownFields, Class<Value> valueClass) -
builder
public static <Value,Context> InstantiatingObjectParser.Builder<Value,Context> builder(String name, Class<Value> valueClass) -
parse
- Specified by:
parse
in interfaceContextParser<Value,
Context> - Throws:
IOException
-
apply
- Specified by:
apply
in interfaceBiFunction<XContentParser,
Context, Value>
-