com.ecwid.consul.v1.catalog.CatalogServiceRequest Maven / Gradle / Ivy
package com.ecwid.consul.v1.catalog;
import com.ecwid.consul.ConsulRequest;
import com.ecwid.consul.SingleUrlParameters;
import com.ecwid.consul.v1.TagsParameters;
import com.ecwid.consul.UrlParameters;
import com.ecwid.consul.v1.NodeMetaParameters;
import com.ecwid.consul.v1.QueryParams;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public final class CatalogServiceRequest implements ConsulRequest {
private final String datacenter;
private final String[] tags;
private final String near;
private final Map nodeMeta;
private final QueryParams queryParams;
private final String token;
private CatalogServiceRequest(String datacenter, String[] tags, String near, Map nodeMeta, QueryParams queryParams, String token) {
this.datacenter = datacenter;
this.tags = tags;
this.near = near;
this.nodeMeta = nodeMeta;
this.queryParams = queryParams;
this.token = token;
}
public String getDatacenter() {
return datacenter;
}
public String getTag() {
return tags != null && tags.length > 0 ? tags[0] : null;
}
public String[] getTags() {
return tags;
}
public String getNear() {
return near;
}
public Map getNodeMeta() {
return nodeMeta;
}
public QueryParams getQueryParams() {
return queryParams;
}
public String getToken() {
return token;
}
public static class Builder {
private String datacenter;
private String[] tags;
private String near;
private Map nodeMeta;
private QueryParams queryParams;
private String token;
private Builder() {
}
public Builder setDatacenter(String datacenter) {
this.datacenter = datacenter;
return this;
}
public Builder setTag(String tag) {
this.tags = new String[]{tag};
return this;
}
public Builder setTags(String[] tags) {
this.tags = tags;
return this;
}
public Builder setNear(String near) {
this.near = near;
return this;
}
public Builder setNodeMeta(Map nodeMeta) {
if (nodeMeta == null) {
this.nodeMeta = null;
} else {
this.nodeMeta = Collections.unmodifiableMap(nodeMeta);
}
return this;
}
public Builder setQueryParams(QueryParams queryParams) {
this.queryParams = queryParams;
return this;
}
public Builder setToken(String token) {
this.token = token;
return this;
}
public CatalogServiceRequest build() {
return new CatalogServiceRequest(datacenter, tags, near, nodeMeta, queryParams, token);
}
}
public static Builder newBuilder() {
return new Builder();
}
@Override
public List asUrlParameters() {
List params = new ArrayList<>();
if (datacenter != null) {
params.add(new SingleUrlParameters("dc", datacenter));
}
if (tags != null) {
params.add(new TagsParameters(tags));
}
if (near != null) {
params.add(new SingleUrlParameters("near", near));
}
if (nodeMeta != null) {
params.add(new NodeMetaParameters(nodeMeta));
}
if (queryParams != null) {
params.add(queryParams);
}
if (token != null) {
params.add(new SingleUrlParameters("token", token));
}
return params;
}
}