diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-01 17:08:57 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-02 10:10:27 +0100 |
commit | 03c00de5438637108875365dbe949e390f10d4bc (patch) | |
tree | 4341217bd9e2d05d8dacb8175fc500bf1c223281 /configserver | |
parent | c04726727464a042c3ce0c0bad9b8935eb9b5d8f (diff) |
Move serialized value definition to State enum
Diffstat (limited to 'configserver')
3 files changed, 16 insertions, 13 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ClusterReindexing.java b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ClusterReindexing.java index ca9aa01ea56..7f0671820d3 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ClusterReindexing.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ClusterReindexing.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.config.server.application; import java.time.Instant; +import java.util.Arrays; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -10,6 +11,7 @@ import java.util.Optional; * Reindexing status for each document type in a content cluster. * * @author jonmv + * @author bjorncs */ public class ClusterReindexing { @@ -51,8 +53,19 @@ public class ClusterReindexing { public enum State { + PENDING("pending"), RUNNING("running"), FAILED("failed"), SUCCESSFUL("successful"); - PENDING, RUNNING, FAILED, SUCCESSFUL; + private final String stringValue; + State(String stringValue) { this.stringValue = stringValue; } + + public static State fromString(String value) { + return Arrays.stream(values()) + .filter(v -> v.stringValue.equals(value)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("Unknown value: " + value)); + } + + public String asString() { return stringValue; } } } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java index b924e07ff47..0c56dfd7bd7 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java @@ -481,21 +481,11 @@ public class ApplicationHandler extends HttpHandler { private static void setStatus(Cursor object, ClusterReindexing.Status status) { object.setLong("startedMillis", status.startedAt().toEpochMilli()); status.endedAt().ifPresent(endedAt -> object.setLong("endedMillis", endedAt.toEpochMilli())); - status.state().map(ReindexingResponse::toString).ifPresent(state -> object.setString("state", state)); + status.state().map(ClusterReindexing.State::asString).ifPresent(state -> object.setString("state", state)); status.message().ifPresent(message -> object.setString("message", message)); status.progress().ifPresent(progress -> object.setString("progress", progress)); } - static String toString(ClusterReindexing.State state) { - switch (state) { - case PENDING: return "pending"; - case RUNNING: return "running"; - case FAILED: return "failed"; - case SUCCESSFUL: return "successful"; - default: throw new IllegalArgumentException("Unexpected state '" + state + "'"); - } - } - } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index 910c4b069e3..3ca21a46d12 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -440,7 +440,7 @@ public class ApplicationHandlerTest { @Test public void testClusterReindexingStateSerialization() { - Stream.of(ClusterReindexing.State.values()).forEach(ReindexingResponse::toString); + Stream.of(ClusterReindexing.State.values()).forEach(ClusterReindexing.State::toString); } @Test |