|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.metadata.index.model; |
| 10 | + |
| 11 | +import org.opensearch.common.annotation.ExperimentalApi; |
| 12 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 13 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 14 | +import org.opensearch.core.common.io.stream.Writeable; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** |
| 21 | + * A model class for Context metadata that can be used in metadata lib without |
| 22 | + * depending on the full Context class from server module. |
| 23 | + */ |
| 24 | +@ExperimentalApi |
| 25 | +public final class ContextModel implements Writeable { |
| 26 | + |
| 27 | + private final String name; |
| 28 | + private final String version; |
| 29 | + private final Map<String, Object> params; |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates a ContextModel with the given fields. |
| 33 | + * |
| 34 | + * @param name the context name |
| 35 | + * @param version the context version |
| 36 | + * @param params the context parameters |
| 37 | + */ |
| 38 | + public ContextModel(String name, String version, Map<String, Object> params) { |
| 39 | + this.name = name; |
| 40 | + this.version = version; |
| 41 | + this.params = params; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a ContextModel by reading from StreamInput. |
| 46 | + * Wire format is compatible with Context(StreamInput). |
| 47 | + * |
| 48 | + * @param in the stream to read from |
| 49 | + * @throws IOException if an I/O error occurs |
| 50 | + */ |
| 51 | + public ContextModel(StreamInput in) throws IOException { |
| 52 | + this.name = in.readString(); |
| 53 | + this.version = in.readOptionalString(); |
| 54 | + this.params = in.readMap(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Writes to StreamOutput. |
| 59 | + * Wire format is compatible with Context.writeTo. |
| 60 | + * |
| 61 | + * @param out the stream to write to |
| 62 | + * @throws IOException if an I/O error occurs |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public void writeTo(StreamOutput out) throws IOException { |
| 66 | + out.writeString(name); |
| 67 | + out.writeOptionalString(version); |
| 68 | + out.writeMap(params); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the context name. |
| 73 | + * |
| 74 | + * @return the name |
| 75 | + */ |
| 76 | + public String name() { |
| 77 | + return name; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the context version. |
| 82 | + * |
| 83 | + * @return the version |
| 84 | + */ |
| 85 | + public String version() { |
| 86 | + return version; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the context parameters. |
| 91 | + * |
| 92 | + * @return the params map |
| 93 | + */ |
| 94 | + public Map<String, Object> params() { |
| 95 | + return params; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean equals(Object o) { |
| 100 | + if (this == o) return true; |
| 101 | + if (o == null || getClass() != o.getClass()) return false; |
| 102 | + ContextModel that = (ContextModel) o; |
| 103 | + return Objects.equals(name, that.name) && Objects.equals(version, that.version) && Objects.equals(params, that.params); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int hashCode() { |
| 108 | + return Objects.hash(name, version, params); |
| 109 | + } |
| 110 | +} |
0 commit comments