2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.msb.wrapper.consul.model.catalog;
18 import com.fasterxml.jackson.annotation.JsonCreator;
19 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20 import com.fasterxml.jackson.annotation.JsonProperty;
21 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22 import com.google.common.base.MoreObjects;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.Lists;
27 import java.util.List;
30 import javax.annotation.Generated;
32 import org.openo.msb.wrapper.consul.model.health.Node;
33 import org.openo.msb.wrapper.consul.model.health.Service;
36 * Immutable implementation of {@link CatalogNode}.
38 * Use the builder to create immutable instances:
39 * {@code ImmutableCatalogNode.builder()}.
41 @SuppressWarnings("all")
42 @Generated({"Immutables.generator", "CatalogNode"})
43 @JsonIgnoreProperties(ignoreUnknown = true)
44 public final class ImmutableCatalogNode extends CatalogNode {
45 private final Node node;
46 private final ImmutableMap<String, Service> services;
48 private ImmutableCatalogNode(
50 ImmutableMap<String, Service> services) {
52 this.services = services;
56 * @return The value of the {@code node} attribute
58 @JsonProperty(value = "Node")
60 public Node getNode() {
65 * @return The value of the {@code services} attribute
67 @JsonProperty(value = "Services")
69 public ImmutableMap<String, Service> getServices() {
74 * Copy the current immutable object by setting a value for the {@link CatalogNode#getNode() node} attribute.
75 * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
76 * @param value A new value for node
77 * @return A modified copy of the {@code this} object
79 public final ImmutableCatalogNode withNode(Node value) {
80 if (this.node == value) return this;
81 return new ImmutableCatalogNode(Preconditions.checkNotNull(value, "node"), this.services);
85 * Copy the current immutable object by replacing the {@link CatalogNode#getServices() services} map with the specified map.
86 * Nulls are not permitted as keys or values.
87 * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
88 * @param entries The entries to be added to the services map
89 * @return A modified copy of {@code this} object
91 public final ImmutableCatalogNode withServices(Map<String, ? extends Service> entries) {
92 if (this.services == entries) return this;
93 ImmutableMap<String, Service> value = ImmutableMap.copyOf(entries);
94 return new ImmutableCatalogNode(this.node, value);
98 * This instance is equal to all instances of {@code ImmutableCatalogNode} that have equal attribute values.
99 * @return {@code true} if {@code this} is equal to {@code another} instance
102 public boolean equals(Object another) {
103 if (this == another) return true;
104 return another instanceof ImmutableCatalogNode
105 && equalTo((ImmutableCatalogNode) another);
108 private boolean equalTo(ImmutableCatalogNode another) {
109 return node.equals(another.node)
110 && services.equals(another.services);
114 * Computes a hash code from attributes: {@code node}, {@code services}.
115 * @return hashCode value
118 public int hashCode() {
120 h = h * 17 + node.hashCode();
121 h = h * 17 + services.hashCode();
126 * Prints the immutable value {@code CatalogNode...} with all non-generated
127 * and non-auxiliary attribute values.
128 * @return A string representation of the value
131 public String toString() {
132 return MoreObjects.toStringHelper("CatalogNode")
134 .add("services", services)
139 * Utility type used to correctly read immutable object from JSON representation.
140 * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
144 static final class Json extends CatalogNode {
146 Map<String, Service> services;
147 @JsonProperty(value = "Node")
148 public void setNode(Node node) {
151 @JsonProperty(value = "Services")
152 public void setServices(Map<String, Service> services) {
153 this.services = services;
156 public Node getNode() { throw new UnsupportedOperationException(); }
158 public Map<String, Service> getServices() { throw new UnsupportedOperationException(); }
162 * @param json A JSON-bindable data structure
163 * @return An immutable value type
164 * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
168 static ImmutableCatalogNode fromJson(Json json) {
169 ImmutableCatalogNode.Builder builder = ImmutableCatalogNode.builder();
170 if (json.node != null) {
171 builder.node(json.node);
173 if (json.services != null) {
174 builder.putAllServices(json.services);
176 return builder.build();
180 * Creates an immutable copy of a {@link CatalogNode} value.
181 * Uses accessors to get values to initialize the new immutable instance.
182 * If an instance is already immutable, it is returned as is.
183 * @param instance The instance to copy
184 * @return A copied immutable CatalogNode instance
186 public static ImmutableCatalogNode copyOf(CatalogNode instance) {
187 if (instance instanceof ImmutableCatalogNode) {
188 return (ImmutableCatalogNode) instance;
190 return ImmutableCatalogNode.builder()
196 * Creates a builder for {@link ImmutableCatalogNode ImmutableCatalogNode}.
197 * @return A new ImmutableCatalogNode builder
199 public static ImmutableCatalogNode.Builder builder() {
200 return new ImmutableCatalogNode.Builder();
204 * Builds instances of type {@link ImmutableCatalogNode ImmutableCatalogNode}.
205 * Initialize attributes and then invoke the {@link #build()} method to create an
206 * immutable instance.
207 * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
208 * but instead used immediately to create instances.</em>
210 public static final class Builder {
211 private static final long INIT_BIT_NODE = 0x1L;
212 private long initBits = 0x1;
215 private ImmutableMap.Builder<String, Service> servicesBuilder = ImmutableMap.builder();
221 * Fill a builder with attribute values from the provided {@code CatalogNode} instance.
222 * Regular attribute values will be replaced with those from the given instance.
223 * Absent optional values will not replace present values.
224 * Collection elements and entries will be added, not replaced.
225 * @param instance The instance from which to copy values
226 * @return {@code this} builder for use in a chained invocation
228 public final Builder from(CatalogNode instance) {
229 Preconditions.checkNotNull(instance, "instance");
230 node(instance.getNode());
231 putAllServices(instance.getServices());
236 * Initializes the value for the {@link CatalogNode#getNode() node} attribute.
237 * @param node The value for node
238 * @return {@code this} builder for use in a chained invocation
240 public final Builder node(Node node) {
241 this.node = Preconditions.checkNotNull(node, "node");
242 initBits &= ~INIT_BIT_NODE;
247 * Put one entry to the {@link CatalogNode#getServices() services} map.
248 * @param key The key in the services map
249 * @param value The associated value in the services map
250 * @return {@code this} builder for use in a chained invocation
252 public final Builder putServices(String key, Service value) {
253 servicesBuilder.put(key, value);
258 * Put one entry to the {@link CatalogNode#getServices() services} map. Nulls are not permitted
259 * @param entry The key and value entry
260 * @return {@code this} builder for use in a chained invocation
262 public final Builder putServices(Map.Entry<String, ? extends Service> entry) {
263 servicesBuilder.put(entry);
268 * Sets or replaces all mappings from the specified map as entries for the {@link CatalogNode#getServices() services} map. Nulls are not permitted
269 * @param entries The entries that will be added to the services map
270 * @return {@code this} builder for use in a chained invocation
272 public final Builder services(Map<String, ? extends Service> entries) {
273 servicesBuilder = ImmutableMap.builder();
274 return putAllServices(entries);
278 * Put all mappings from the specified map as entries to {@link CatalogNode#getServices() services} map. Nulls are not permitted
279 * @param entries The entries that will be added to the services map
280 * @return {@code this} builder for use in a chained invocation
282 public final Builder putAllServices(Map<String, ? extends Service> entries) {
283 servicesBuilder.putAll(entries);
288 * Builds a new {@link ImmutableCatalogNode ImmutableCatalogNode}.
289 * @return An immutable instance of CatalogNode
290 * @throws java.lang.IllegalStateException if any required attributes are missing
292 public ImmutableCatalogNode build() throws IllegalStateException {
294 throw new IllegalStateException(formatRequiredAttributesMessage());
296 return new ImmutableCatalogNode(node, servicesBuilder.build());
299 private String formatRequiredAttributesMessage() {
300 List<String> attributes = Lists.newArrayList();
301 if ((initBits & INIT_BIT_NODE) != 0) attributes.add("node");
302 return "Cannot build CatalogNode, some of required attributes are not set " + attributes;