|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ml.process; |
| 8 | + |
| 9 | +import org.elasticsearch.common.ParseField; |
| 10 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 11 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 12 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +public class ControllerResponse implements ToXContentObject { |
| 18 | + |
| 19 | + public static final ParseField TYPE = new ParseField("controller_response"); |
| 20 | + |
| 21 | + public static final ParseField COMMAND_ID = new ParseField("id"); |
| 22 | + public static final ParseField SUCCESS = new ParseField("success"); |
| 23 | + public static final ParseField REASON = new ParseField("reason"); |
| 24 | + |
| 25 | + public static final ConstructingObjectParser<ControllerResponse, Void> PARSER = new ConstructingObjectParser<>( |
| 26 | + TYPE.getPreferredName(), a -> new ControllerResponse((int) a[0], (boolean) a[1], (String) a[2])); |
| 27 | + |
| 28 | + static { |
| 29 | + PARSER.declareInt(ConstructingObjectParser.constructorArg(), COMMAND_ID); |
| 30 | + PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), SUCCESS); |
| 31 | + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), REASON); |
| 32 | + } |
| 33 | + |
| 34 | + private final int commandId; |
| 35 | + private final boolean success; |
| 36 | + private final String reason; |
| 37 | + |
| 38 | + ControllerResponse(int commandId, boolean success, String reason) { |
| 39 | + this.commandId = commandId; |
| 40 | + this.success = success; |
| 41 | + this.reason = reason; |
| 42 | + } |
| 43 | + |
| 44 | + public int getCommandId() { |
| 45 | + return commandId; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean isSuccess() { |
| 49 | + return success; |
| 50 | + } |
| 51 | + |
| 52 | + public String getReason() { |
| 53 | + return reason; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 58 | + builder.startObject(); |
| 59 | + builder.field(COMMAND_ID.getPreferredName(), commandId); |
| 60 | + builder.field(SUCCESS.getPreferredName(), success); |
| 61 | + if (reason != null) { |
| 62 | + builder.field(REASON.getPreferredName(), reason); |
| 63 | + } |
| 64 | + builder.endObject(); |
| 65 | + return builder; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object o) { |
| 70 | + if (this == o) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + if (o == null || getClass() != o.getClass()) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + ControllerResponse that = (ControllerResponse) o; |
| 77 | + return this.commandId == that.commandId && |
| 78 | + this.success == that.success && |
| 79 | + Objects.equals(this.reason, that.reason); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + return Objects.hash(commandId, success, reason); |
| 85 | + } |
| 86 | +} |
0 commit comments