|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.dataframe.transforms; |
| 20 | + |
| 21 | +import org.elasticsearch.common.ParseField; |
| 22 | +import org.elasticsearch.common.Strings; |
| 23 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * A Pojo class containing an Elastic Node's attributes |
| 35 | + */ |
| 36 | +public class NodeAttributes implements ToXContentObject { |
| 37 | + |
| 38 | + public static final ParseField ID = new ParseField("id"); |
| 39 | + public static final ParseField NAME = new ParseField("name"); |
| 40 | + public static final ParseField EPHEMERAL_ID = new ParseField("ephemeral_id"); |
| 41 | + public static final ParseField TRANSPORT_ADDRESS = new ParseField("transport_address"); |
| 42 | + public static final ParseField ATTRIBUTES = new ParseField("attributes"); |
| 43 | + |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + public static final ConstructingObjectParser<NodeAttributes, Void> PARSER = |
| 46 | + new ConstructingObjectParser<>("node", true, |
| 47 | + (a) -> { |
| 48 | + int i = 0; |
| 49 | + String id = (String) a[i++]; |
| 50 | + String name = (String) a[i++]; |
| 51 | + String ephemeralId = (String) a[i++]; |
| 52 | + String transportAddress = (String) a[i++]; |
| 53 | + Map<String, String> attributes = (Map<String, String>) a[i]; |
| 54 | + return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); |
| 55 | + }); |
| 56 | + |
| 57 | + static { |
| 58 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); |
| 59 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); |
| 60 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), EPHEMERAL_ID); |
| 61 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), TRANSPORT_ADDRESS); |
| 62 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), |
| 63 | + (p, c) -> p.mapStrings(), |
| 64 | + ATTRIBUTES, |
| 65 | + ObjectParser.ValueType.OBJECT); |
| 66 | + } |
| 67 | + |
| 68 | + private final String id; |
| 69 | + private final String name; |
| 70 | + private final String ephemeralId; |
| 71 | + private final String transportAddress; |
| 72 | + private final Map<String, String> attributes; |
| 73 | + |
| 74 | + public NodeAttributes(String id, String name, String ephemeralId, String transportAddress, Map<String, String> attributes) { |
| 75 | + this.id = id; |
| 76 | + this.name = name; |
| 77 | + this.ephemeralId = ephemeralId; |
| 78 | + this.transportAddress = transportAddress; |
| 79 | + this.attributes = Collections.unmodifiableMap(attributes); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * The unique identifier of the node. |
| 84 | + */ |
| 85 | + public String getId() { |
| 86 | + return id; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The node name. |
| 91 | + */ |
| 92 | + public String getName() { |
| 93 | + return name; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * The ephemeral id of the node. |
| 98 | + */ |
| 99 | + public String getEphemeralId() { |
| 100 | + return ephemeralId; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The host and port where transport HTTP connections are accepted. |
| 105 | + */ |
| 106 | + public String getTransportAddress() { |
| 107 | + return transportAddress; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Additional attributes related to this node |
| 112 | + */ |
| 113 | + public Map<String, String> getAttributes() { |
| 114 | + return attributes; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 119 | + builder.startObject(); |
| 120 | + builder.field(ID.getPreferredName(), id); |
| 121 | + builder.field(NAME.getPreferredName(), name); |
| 122 | + builder.field(EPHEMERAL_ID.getPreferredName(), ephemeralId); |
| 123 | + builder.field(TRANSPORT_ADDRESS.getPreferredName(), transportAddress); |
| 124 | + builder.field(ATTRIBUTES.getPreferredName(), attributes); |
| 125 | + builder.endObject(); |
| 126 | + return builder; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public int hashCode() { |
| 131 | + return Objects.hash(id, name, ephemeralId, transportAddress, attributes); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean equals(Object other) { |
| 136 | + if (this == other) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + if (other == null || getClass() != other.getClass()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + NodeAttributes that = (NodeAttributes) other; |
| 145 | + return Objects.equals(id, that.id) && |
| 146 | + Objects.equals(name, that.name) && |
| 147 | + Objects.equals(ephemeralId, that.ephemeralId) && |
| 148 | + Objects.equals(transportAddress, that.transportAddress) && |
| 149 | + Objects.equals(attributes, that.attributes); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String toString() { |
| 154 | + return Strings.toString(this); |
| 155 | + } |
| 156 | +} |
0 commit comments