Share common parser in some AcknowledgedResponses (#31169)

Several AcknowledgedResponse implementations only parse the boolean acknowledged
flag and then create an instance of their class using that flag. This can be
simplified by adding this basic parser to the superclass, provide a common
helper method and call the appropriate ctor in the fromXContent methods.
This commit is contained in:
Christoph Büscher 2018-06-07 13:52:10 +02:00 committed by GitHub
parent 280a2f55d6
commit c352ff1615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 25 additions and 75 deletions

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.cluster.repositories.delete; package org.elasticsearch.action.admin.cluster.repositories.delete;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -28,13 +27,6 @@ import org.elasticsearch.common.xcontent.XContentParser;
*/ */
public class DeleteRepositoryResponse extends AcknowledgedResponse { public class DeleteRepositoryResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<DeleteRepositoryResponse, Void> PARSER =
new ConstructingObjectParser<>("delete_repository", true, args -> new DeleteRepositoryResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
DeleteRepositoryResponse() { DeleteRepositoryResponse() {
} }
@ -43,6 +35,6 @@ public class DeleteRepositoryResponse extends AcknowledgedResponse {
} }
public static DeleteRepositoryResponse fromXContent(XContentParser parser) { public static DeleteRepositoryResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new DeleteRepositoryResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.cluster.repositories.put; package org.elasticsearch.action.admin.cluster.repositories.put;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -28,13 +27,6 @@ import org.elasticsearch.common.xcontent.XContentParser;
*/ */
public class PutRepositoryResponse extends AcknowledgedResponse { public class PutRepositoryResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<PutRepositoryResponse, Void> PARSER = new ConstructingObjectParser<>("put_repository",
true, args -> new PutRepositoryResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
PutRepositoryResponse() { PutRepositoryResponse() {
} }
@ -43,6 +35,6 @@ public class PutRepositoryResponse extends AcknowledgedResponse {
} }
public static PutRepositoryResponse fromXContent(XContentParser parser) { public static PutRepositoryResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new PutRepositoryResponse(parseAcknowledged(parser));
} }
} }

View File

@ -42,6 +42,6 @@ public class IndicesAliasesResponse extends AcknowledgedResponse {
} }
public static IndicesAliasesResponse fromXContent(XContentParser parser) { public static IndicesAliasesResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new IndicesAliasesResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,20 +20,12 @@
package org.elasticsearch.action.admin.indices.close; package org.elasticsearch.action.admin.indices.close;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
* A response for a close index action. * A response for a close index action.
*/ */
public class CloseIndexResponse extends AcknowledgedResponse { public class CloseIndexResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<CloseIndexResponse, Void> PARSER = new ConstructingObjectParser<>("close_index", true,
args -> new CloseIndexResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
CloseIndexResponse() { CloseIndexResponse() {
} }
@ -42,6 +34,6 @@ public class CloseIndexResponse extends AcknowledgedResponse {
} }
public static CloseIndexResponse fromXContent(XContentParser parser) { public static CloseIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new CloseIndexResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.indices.delete; package org.elasticsearch.action.admin.indices.delete;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -28,13 +27,6 @@ import org.elasticsearch.common.xcontent.XContentParser;
*/ */
public class DeleteIndexResponse extends AcknowledgedResponse { public class DeleteIndexResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<DeleteIndexResponse, Void> PARSER = new ConstructingObjectParser<>("delete_index",
true, args -> new DeleteIndexResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
DeleteIndexResponse() { DeleteIndexResponse() {
} }
@ -43,6 +35,6 @@ public class DeleteIndexResponse extends AcknowledgedResponse {
} }
public static DeleteIndexResponse fromXContent(XContentParser parser) { public static DeleteIndexResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new DeleteIndexResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.indices.mapping.put; package org.elasticsearch.action.admin.indices.mapping.put;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -28,15 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
*/ */
public class PutMappingResponse extends AcknowledgedResponse { public class PutMappingResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<PutMappingResponse, Void> PARSER = new ConstructingObjectParser<>("put_mapping",
true, args -> new PutMappingResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
protected PutMappingResponse() { protected PutMappingResponse() {
} }
protected PutMappingResponse(boolean acknowledged) { protected PutMappingResponse(boolean acknowledged) {
@ -44,6 +35,6 @@ public class PutMappingResponse extends AcknowledgedResponse {
} }
public static PutMappingResponse fromXContent(XContentParser parser) { public static PutMappingResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new PutMappingResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.indices.settings.put; package org.elasticsearch.action.admin.indices.settings.put;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -28,13 +27,6 @@ import org.elasticsearch.common.xcontent.XContentParser;
*/ */
public class UpdateSettingsResponse extends AcknowledgedResponse { public class UpdateSettingsResponse extends AcknowledgedResponse {
private static final ConstructingObjectParser<UpdateSettingsResponse, Void> PARSER = new ConstructingObjectParser<>(
"update_index_settings", true, args -> new UpdateSettingsResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
UpdateSettingsResponse() { UpdateSettingsResponse() {
} }
@ -43,7 +35,6 @@ public class UpdateSettingsResponse extends AcknowledgedResponse {
} }
public static UpdateSettingsResponse fromXContent(XContentParser parser) { public static UpdateSettingsResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new UpdateSettingsResponse(parseAcknowledged(parser));
} }
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.admin.indices.template.put; package org.elasticsearch.action.admin.indices.template.put;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
/** /**
@ -34,13 +33,7 @@ public class PutIndexTemplateResponse extends AcknowledgedResponse {
super(acknowledged); super(acknowledged);
} }
private static final ConstructingObjectParser<PutIndexTemplateResponse, Void> PARSER;
static {
PARSER = new ConstructingObjectParser<>("put_index_template", true, args -> new PutIndexTemplateResponse((boolean) args[0]));
declareAcknowledgedField(PARSER);
}
public static PutIndexTemplateResponse fromXContent(XContentParser parser) { public static PutIndexTemplateResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new PutIndexTemplateResponse(parseAcknowledged(parser));
} }
} }

View File

@ -20,21 +20,12 @@
package org.elasticsearch.action.ingest; package org.elasticsearch.action.ingest;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
public class WritePipelineResponse extends AcknowledgedResponse implements ToXContentObject { public class WritePipelineResponse extends AcknowledgedResponse implements ToXContentObject {
private static final ConstructingObjectParser<WritePipelineResponse, Void> PARSER = new ConstructingObjectParser<>(
"write_pipeline_response", true, args -> new WritePipelineResponse((boolean) args[0]));
static {
declareAcknowledgedField(PARSER);
}
WritePipelineResponse() { WritePipelineResponse() {
} }
public WritePipelineResponse(boolean acknowledged) { public WritePipelineResponse(boolean acknowledged) {
@ -42,6 +33,6 @@ public class WritePipelineResponse extends AcknowledgedResponse implements ToXCo
} }
public static WritePipelineResponse fromXContent(XContentParser parser) { public static WritePipelineResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return new WritePipelineResponse(parseAcknowledged(parser));
} }
} }

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -88,6 +89,21 @@ public abstract class AcknowledgedResponse extends ActionResponse implements ToX
} }
/**
* A generic parser that simply parses the acknowledged flag
*/
private static final ConstructingObjectParser<Boolean, Void> ACKNOWLEDGED_FLAG_PARSER = new ConstructingObjectParser<>(
"acknowledged_flag", true, args -> (Boolean) args[0]);
static {
ACKNOWLEDGED_FLAG_PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ACKNOWLEDGED,
ObjectParser.ValueType.BOOLEAN);
}
protected static boolean parseAcknowledged(XContentParser parser) {
return ACKNOWLEDGED_FLAG_PARSER.apply(parser, null);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {