From ff9c33b6c8eb95df29f51f631a635db21c3f3da8 Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Tue, 24 Nov 2015 12:08:29 +0100 Subject: [PATCH] Use ObjectParser to parse AllocationID --- .../cluster/routing/AllocationId.java | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/AllocationId.java b/core/src/main/java/org/elasticsearch/cluster/routing/AllocationId.java index 20da5ce8cd3..528ed8b1c3f 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/AllocationId.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/AllocationId.java @@ -20,15 +20,17 @@ package org.elasticsearch.cluster.routing; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.gateway.CorruptStateException; import java.io.IOException; +import java.util.Objects; /** * Uniquely identifies an allocation. An allocation is a shard moving from unassigned to initializing, @@ -43,6 +45,30 @@ public class AllocationId implements ToXContent { private static final String ID_KEY = "id"; private static final String RELOCATION_ID_KEY = "relocation_id"; + private static final ObjectParser ALLOCATION_ID_PARSER = new ObjectParser<>("allocationId"); + + static { + ALLOCATION_ID_PARSER.declareString(AllocationId.Builder::setId, new ParseField(ID_KEY)); + ALLOCATION_ID_PARSER.declareString(AllocationId.Builder::setRelocationId, new ParseField(RELOCATION_ID_KEY)); + } + + private static class Builder { + private String id; + private String relocationId; + + public void setId(String id) { + this.id = id; + } + + public void setRelocationId(String relocationId) { + this.relocationId = relocationId; + } + + public AllocationId build() { + return new AllocationId(id, relocationId); + } + } + private final String id; @Nullable private final String relocationId; @@ -58,6 +84,7 @@ public class AllocationId implements ToXContent { } private AllocationId(String id, String relocationId) { + Objects.requireNonNull(id, "Argument [id] must be non-null"); this.id = id; this.relocationId = relocationId; } @@ -164,35 +191,6 @@ public class AllocationId implements ToXContent { } public static AllocationId fromXContent(XContentParser parser) throws IOException { - XContentParser.Token token = parser.currentToken(); - if (token == null) { // fresh parser? move to the first real token under object - token = parser.nextToken(); - } - assert token == XContentParser.Token.START_OBJECT; - - String id = null; - String relocationId = null; - - String currentFieldName = null; - - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token.isValue()) { - if (ID_KEY.equals(currentFieldName)) { - id = parser.text(); - } else if (RELOCATION_ID_KEY.equals(currentFieldName)) { - relocationId = parser.text(); - } else { - throw new CorruptStateException("unexpected field in allocation id [" + currentFieldName + "]"); - } - } else { - throw new CorruptStateException("unexpected token in allocation id [" + token.name() + "]"); - } - } - if (id == null) { - throw new CorruptStateException("missing value for [id] in allocation id"); - } - return new AllocationId(id, relocationId); + return ALLOCATION_ID_PARSER.parse(parser, new AllocationId.Builder()).build(); } }