2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.msb.wrapper.consul.model.catalog;
\r
19 import com.fasterxml.jackson.annotation.JsonCreator;
\r
20 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
\r
21 import com.fasterxml.jackson.annotation.JsonProperty;
\r
22 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
\r
23 import com.google.common.base.MoreObjects;
\r
24 import com.google.common.base.Preconditions;
\r
25 import com.google.common.collect.ImmutableMap;
\r
26 import com.google.common.collect.Lists;
\r
28 import java.util.List;
\r
29 import java.util.Map;
\r
31 import javax.annotation.Generated;
\r
33 import org.openo.msb.wrapper.consul.model.health.Node;
\r
34 import org.openo.msb.wrapper.consul.model.health.Service;
\r
37 * Immutable implementation of {@link CatalogNode}.
\r
39 * Use the builder to create immutable instances:
\r
40 * {@code ImmutableCatalogNode.builder()}.
\r
42 @SuppressWarnings("all")
\r
43 @Generated({"Immutables.generator", "CatalogNode"})
\r
44 @JsonIgnoreProperties(ignoreUnknown = true)
\r
45 public final class ImmutableCatalogNode extends CatalogNode {
\r
46 private final Node node;
\r
47 private final ImmutableMap<String, Service> services;
\r
49 private ImmutableCatalogNode(
\r
51 ImmutableMap<String, Service> services) {
\r
53 this.services = services;
\r
57 * @return The value of the {@code node} attribute
\r
59 @JsonProperty(value = "Node")
\r
61 public Node getNode() {
\r
66 * @return The value of the {@code services} attribute
\r
68 @JsonProperty(value = "Services")
\r
70 public ImmutableMap<String, Service> getServices() {
\r
75 * Copy the current immutable object by setting a value for the {@link CatalogNode#getNode() node} attribute.
\r
76 * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
\r
77 * @param value A new value for node
\r
78 * @return A modified copy of the {@code this} object
\r
80 public final ImmutableCatalogNode withNode(Node value) {
\r
81 if (this.node == value) return this;
\r
82 return new ImmutableCatalogNode(Preconditions.checkNotNull(value, "node"), this.services);
\r
86 * Copy the current immutable object by replacing the {@link CatalogNode#getServices() services} map with the specified map.
\r
87 * Nulls are not permitted as keys or values.
\r
88 * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
\r
89 * @param entries The entries to be added to the services map
\r
90 * @return A modified copy of {@code this} object
\r
92 public final ImmutableCatalogNode withServices(Map<String, ? extends Service> entries) {
\r
93 if (this.services == entries) return this;
\r
94 ImmutableMap<String, Service> value = ImmutableMap.copyOf(entries);
\r
95 return new ImmutableCatalogNode(this.node, value);
\r
99 * This instance is equal to all instances of {@code ImmutableCatalogNode} that have equal attribute values.
\r
100 * @return {@code true} if {@code this} is equal to {@code another} instance
\r
103 public boolean equals(Object another) {
\r
104 if (this == another) return true;
\r
105 return another instanceof ImmutableCatalogNode
\r
106 && equalTo((ImmutableCatalogNode) another);
\r
109 private boolean equalTo(ImmutableCatalogNode another) {
\r
110 return node.equals(another.node)
\r
111 && services.equals(another.services);
\r
115 * Computes a hash code from attributes: {@code node}, {@code services}.
\r
116 * @return hashCode value
\r
119 public int hashCode() {
\r
121 h = h * 17 + node.hashCode();
\r
122 h = h * 17 + services.hashCode();
\r
127 * Prints the immutable value {@code CatalogNode...} with all non-generated
\r
128 * and non-auxiliary attribute values.
\r
129 * @return A string representation of the value
\r
132 public String toString() {
\r
133 return MoreObjects.toStringHelper("CatalogNode")
\r
135 .add("services", services)
\r
140 * Utility type used to correctly read immutable object from JSON representation.
\r
141 * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
\r
145 static final class Json extends CatalogNode {
\r
147 Map<String, Service> services;
\r
148 @JsonProperty(value = "Node")
\r
149 public void setNode(Node node) {
\r
152 @JsonProperty(value = "Services")
\r
153 public void setServices(Map<String, Service> services) {
\r
154 this.services = services;
\r
157 public Node getNode() { throw new UnsupportedOperationException(); }
\r
159 public Map<String, Service> getServices() { throw new UnsupportedOperationException(); }
\r
163 * @param json A JSON-bindable data structure
\r
164 * @return An immutable value type
\r
165 * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
\r
169 static ImmutableCatalogNode fromJson(Json json) {
\r
170 ImmutableCatalogNode.Builder builder = ImmutableCatalogNode.builder();
\r
171 if (json.node != null) {
\r
172 builder.node(json.node);
\r
174 if (json.services != null) {
\r
175 builder.putAllServices(json.services);
\r
177 return builder.build();
\r
181 * Creates an immutable copy of a {@link CatalogNode} value.
\r
182 * Uses accessors to get values to initialize the new immutable instance.
\r
183 * If an instance is already immutable, it is returned as is.
\r
184 * @param instance The instance to copy
\r
185 * @return A copied immutable CatalogNode instance
\r
187 public static ImmutableCatalogNode copyOf(CatalogNode instance) {
\r
188 if (instance instanceof ImmutableCatalogNode) {
\r
189 return (ImmutableCatalogNode) instance;
\r
191 return ImmutableCatalogNode.builder()
\r
197 * Creates a builder for {@link ImmutableCatalogNode ImmutableCatalogNode}.
\r
198 * @return A new ImmutableCatalogNode builder
\r
200 public static ImmutableCatalogNode.Builder builder() {
\r
201 return new ImmutableCatalogNode.Builder();
\r
205 * Builds instances of type {@link ImmutableCatalogNode ImmutableCatalogNode}.
\r
206 * Initialize attributes and then invoke the {@link #build()} method to create an
\r
207 * immutable instance.
\r
208 * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
\r
209 * but instead used immediately to create instances.</em>
\r
211 public static final class Builder {
\r
212 private static final long INIT_BIT_NODE = 0x1L;
\r
213 private long initBits = 0x1;
\r
216 private ImmutableMap.Builder<String, Service> servicesBuilder = ImmutableMap.builder();
\r
218 private Builder() {
\r
222 * Fill a builder with attribute values from the provided {@code CatalogNode} instance.
\r
223 * Regular attribute values will be replaced with those from the given instance.
\r
224 * Absent optional values will not replace present values.
\r
225 * Collection elements and entries will be added, not replaced.
\r
226 * @param instance The instance from which to copy values
\r
227 * @return {@code this} builder for use in a chained invocation
\r
229 public final Builder from(CatalogNode instance) {
\r
230 Preconditions.checkNotNull(instance, "instance");
\r
231 node(instance.getNode());
\r
232 putAllServices(instance.getServices());
\r
237 * Initializes the value for the {@link CatalogNode#getNode() node} attribute.
\r
238 * @param node The value for node
\r
239 * @return {@code this} builder for use in a chained invocation
\r
241 public final Builder node(Node node) {
\r
242 this.node = Preconditions.checkNotNull(node, "node");
\r
243 initBits &= ~INIT_BIT_NODE;
\r
248 * Put one entry to the {@link CatalogNode#getServices() services} map.
\r
249 * @param key The key in the services map
\r
250 * @param value The associated value in the services map
\r
251 * @return {@code this} builder for use in a chained invocation
\r
253 public final Builder putServices(String key, Service value) {
\r
254 servicesBuilder.put(key, value);
\r
259 * Put one entry to the {@link CatalogNode#getServices() services} map. Nulls are not permitted
\r
260 * @param entry The key and value entry
\r
261 * @return {@code this} builder for use in a chained invocation
\r
263 public final Builder putServices(Map.Entry<String, ? extends Service> entry) {
\r
264 servicesBuilder.put(entry);
\r
269 * Sets or replaces all mappings from the specified map as entries for the {@link CatalogNode#getServices() services} map. Nulls are not permitted
\r
270 * @param entries The entries that will be added to the services map
\r
271 * @return {@code this} builder for use in a chained invocation
\r
273 public final Builder services(Map<String, ? extends Service> entries) {
\r
274 servicesBuilder = ImmutableMap.builder();
\r
275 return putAllServices(entries);
\r
279 * Put all mappings from the specified map as entries to {@link CatalogNode#getServices() services} map. Nulls are not permitted
\r
280 * @param entries The entries that will be added to the services map
\r
281 * @return {@code this} builder for use in a chained invocation
\r
283 public final Builder putAllServices(Map<String, ? extends Service> entries) {
\r
284 servicesBuilder.putAll(entries);
\r
289 * Builds a new {@link ImmutableCatalogNode ImmutableCatalogNode}.
\r
290 * @return An immutable instance of CatalogNode
\r
291 * @throws java.lang.IllegalStateException if any required attributes are missing
\r
293 public ImmutableCatalogNode build() throws IllegalStateException {
\r
294 if (initBits != 0) {
\r
295 throw new IllegalStateException(formatRequiredAttributesMessage());
\r
297 return new ImmutableCatalogNode(node, servicesBuilder.build());
\r
300 private String formatRequiredAttributesMessage() {
\r
301 List<String> attributes = Lists.newArrayList();
\r
302 if ((initBits & INIT_BIT_NODE) != 0) attributes.add("node");
\r
303 return "Cannot build CatalogNode, some of required attributes are not set " + attributes;
\r