Use ObjectParser to parse AllocationID

This commit is contained in:
Yannick Welsch 2015-11-24 12:08:29 +01:00
parent 556d80501c
commit ff9c33b6c8
1 changed files with 29 additions and 31 deletions

View File

@ -20,15 +20,17 @@
package org.elasticsearch.cluster.routing; package org.elasticsearch.cluster.routing;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.gateway.CorruptStateException;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
/** /**
* Uniquely identifies an allocation. An allocation is a shard moving from unassigned to initializing, * 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 ID_KEY = "id";
private static final String RELOCATION_ID_KEY = "relocation_id"; private static final String RELOCATION_ID_KEY = "relocation_id";
private static final ObjectParser<AllocationId.Builder, Void> 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; private final String id;
@Nullable @Nullable
private final String relocationId; private final String relocationId;
@ -58,6 +84,7 @@ public class AllocationId implements ToXContent {
} }
private AllocationId(String id, String relocationId) { private AllocationId(String id, String relocationId) {
Objects.requireNonNull(id, "Argument [id] must be non-null");
this.id = id; this.id = id;
this.relocationId = relocationId; this.relocationId = relocationId;
} }
@ -164,35 +191,6 @@ public class AllocationId implements ToXContent {
} }
public static AllocationId fromXContent(XContentParser parser) throws IOException { public static AllocationId fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken(); return ALLOCATION_ID_PARSER.parse(parser, new AllocationId.Builder()).build();
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);
} }
} }