package io.vertx.ext.consul;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static io.vertx.ext.consul.impl.Utils.listOf;
import static io.vertx.ext.consul.impl.Utils.mapStringString;
@DataObject
public class Service {
private static final String NODE = "Node";
private static final String ADDRESS = "Address";
private static final String SERVICE_ID = "ServiceID";
private static final String SERVICE_NAME = "ServiceName";
private static final String SERVICE_TAGS = "ServiceTags";
private static final String SERVICE_ADDRESS = "ServiceAddress";
private static final String SERVICE_META = "ServiceMeta";
private static final String SERVICE_PORT = "ServicePort";
private String node;
private String nodeAddress;
private String id;
private String name;
private List<String> tags;
private String address;
private Map<String, String> meta;
private int port;
public Service() {
}
public Service(Service other) {
this.node = other.node;
this.nodeAddress = other.nodeAddress;
this.id = other.id;
this.name = other.name;
this.tags = other.tags;
this.address = other.address;
this.meta = other.meta;
this.port = other.port;
}
public Service(JsonObject service) {
this.node = service.getString(NODE);
this.nodeAddress = service.getString(ADDRESS);
this.id = service.getString(SERVICE_ID);
this.name = service.getString(SERVICE_NAME);
this.tags = listOf(service.getJsonArray(SERVICE_TAGS));
this.address = service.getString(SERVICE_ADDRESS);
this.meta = mapStringString(service.getJsonObject(SERVICE_META));
this.port = service.getInteger(SERVICE_PORT, 0);
}
public JsonObject toJson() {
JsonObject jsonObject = new JsonObject();
if (node != null) {
jsonObject.put(NODE, node);
}
if (nodeAddress != null) {
jsonObject.put(ADDRESS, nodeAddress);
}
if (id != null) {
jsonObject.put(SERVICE_ID, id);
}
if (name != null) {
jsonObject.put(SERVICE_NAME, name);
}
if (tags != null) {
jsonObject.put(SERVICE_TAGS, new JsonArray(tags));
}
if (address != null) {
jsonObject.put(SERVICE_ADDRESS, address);
}
if (meta != null && !meta.isEmpty()) {
jsonObject.put(SERVICE_META, meta);
}
if (port != 0) {
jsonObject.put(SERVICE_PORT, port);
}
return jsonObject;
}
public String getNode() {
return node;
}
public Service setNode(String node) {
this.node = node;
return this;
}
public String getNodeAddress() {
return nodeAddress;
}
public Service setNodeAddress(String nodeAddress) {
this.nodeAddress = nodeAddress;
return this;
}
public String getId() {
return id;
}
public Service setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public Service setName(String name) {
this.name = name;
return this;
}
public List<String> getTags() {
return tags;
}
public Service setTags(List<String> tags) {
this.tags = tags;
return this;
}
public String getAddress() {
return address;
}
public Service setAddress(String address) {
this.address = address;
return this;
}
public Map<String, String> getMeta() {
return meta;
}
public Service setMeta(Map<String, String> meta) {
this.meta = meta;
return this;
}
public int getPort() {
return port;
}
public Service setPort(int port) {
this.port = port;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Service service = (Service) o;
if (port != service.port) return false;
if (node != null ? !node.equals(service.node) : service.node != null) return false;
if (nodeAddress != null ? !nodeAddress.equals(service.nodeAddress) : service.nodeAddress != null) return false;
if (id != null ? !id.equals(service.id) : service.id != null) return false;
if (name != null ? !name.equals(service.name) : service.name != null) return false;
if (meta != null ? !meta.equals(service.meta) : service.meta != null) return false;
if (tags != null ? !sortedTags().equals(service.sortedTags()) : service.tags != null) return false;
return address != null ? address.equals(service.address) : service.address == null;
}
@Override
public int hashCode() {
int result = node != null ? node.hashCode() : 0;
result = 31 * result + (nodeAddress != null ? nodeAddress.hashCode() : 0);
result = 31 * result + (id != null ? id.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (tags != null ? sortedTags().hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (meta != null ? meta.hashCode() : 0);
result = 31 * result + port;
return result;
}
private List<String> sortedTags() {
List<String> sorted = null;
if (tags != null) {
sorted = new ArrayList<>(tags);
sorted.sort(String::compareTo);
}
return sorted;
}
}