Class InstantiatingObjectParser<Value,Context>
- All Implemented Interfaces:
BiFunction<XContentParser,
,Context, Value> ContextParser<Context,
Value>
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 than 2 constructors with the same number of arguments, one of them needs to be marked with ParserConstructor annotation. It is also possible for the constructor to accept Context as the first parameter, in this case as in the case with multiple constructors it is required for the constructor 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;
static {
InstantiatingObjectParser.Builder<Thing, SomeContext> parser =
InstantiatingObjectParser,builder<>("thing", true, Thing.class);
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 = parser.build()
}
public static class AnotherThing {
@ParserConstructor
public AnotherThing(SomeContext continent, String animal, String vegetable, int mineral) {
....
}
}
private static final InstantiatingObjectParser<AnotherThing, SomeContext> PARSER;
static {
InstantiatingObjectParser.Builder<AnotherThing, SomeContext> parser =
InstantiatingObjectParser,builder<>("thing", true, AnotherThing.class);
parser.declareString(constructorArg(), new ParseField("animal"));
parser.declareString(constructorArg(), new ParseField("vegetable"));
parser.declareInt(optionalConstructorArg(), new ParseField("mineral"));
PARSER = parser.build()
}
-
Nested Class Summary
Nested Classes -
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>
-