Issue-id: OCS-9
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / model / catalog / ImmutableCatalogNode.java
1 /**
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.msb.wrapper.consul.model.catalog;
17
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;
26
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.annotation.Generated;
31
32 import org.openo.msb.wrapper.consul.model.health.Node;
33 import org.openo.msb.wrapper.consul.model.health.Service;
34
35 /**
36  * Immutable implementation of {@link CatalogNode}.
37  * <p>
38  * Use the builder to create immutable instances:
39  * {@code ImmutableCatalogNode.builder()}.
40  */
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;
47
48   private ImmutableCatalogNode(
49       Node node,
50       ImmutableMap<String, Service> services) {
51     this.node = node;
52     this.services = services;
53   }
54
55   /**
56    * @return The value of the {@code node} attribute
57    */
58   @JsonProperty(value = "Node")
59   @Override
60   public Node getNode() {
61     return node;
62   }
63
64   /**
65    * @return The value of the {@code services} attribute
66    */
67   @JsonProperty(value = "Services")
68   @Override
69   public ImmutableMap<String, Service> getServices() {
70     return services;
71   }
72
73   /**
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
78    */
79   public final ImmutableCatalogNode withNode(Node value) {
80     if (this.node == value) return this;
81     return new ImmutableCatalogNode(Preconditions.checkNotNull(value, "node"), this.services);
82   }
83
84   /**
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
90    */
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);
95   }
96
97   /**
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
100    */
101   @Override
102   public boolean equals(Object another) {
103     if (this == another) return true;
104     return another instanceof ImmutableCatalogNode
105         && equalTo((ImmutableCatalogNode) another);
106   }
107
108   private boolean equalTo(ImmutableCatalogNode another) {
109     return node.equals(another.node)
110         && services.equals(another.services);
111   }
112
113   /**
114    * Computes a hash code from attributes: {@code node}, {@code services}.
115    * @return hashCode value
116    */
117   @Override
118   public int hashCode() {
119     int h = 31;
120     h = h * 17 + node.hashCode();
121     h = h * 17 + services.hashCode();
122     return h;
123   }
124
125   /**
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
129    */
130   @Override
131   public String toString() {
132     return MoreObjects.toStringHelper("CatalogNode")
133         .add("node", node)
134         .add("services", services)
135         .toString();
136   }
137
138   /**
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
141    */
142   @Deprecated
143   @JsonDeserialize
144   static final class Json extends CatalogNode {
145     Node node;
146     Map<String, Service> services;
147     @JsonProperty(value = "Node")
148     public void setNode(Node node) {
149       this.node = node;
150     }
151     @JsonProperty(value = "Services")
152     public void setServices(Map<String, Service> services) {
153       this.services = services;
154     }
155     @Override
156     public Node getNode() { throw new UnsupportedOperationException(); }
157     @Override
158     public Map<String, Service> getServices() { throw new UnsupportedOperationException(); }
159   }
160
161   /**
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
165    */
166   @Deprecated
167   @JsonCreator
168   static ImmutableCatalogNode fromJson(Json json) {
169     ImmutableCatalogNode.Builder builder = ImmutableCatalogNode.builder();
170     if (json.node != null) {
171       builder.node(json.node);
172     }
173     if (json.services != null) {
174       builder.putAllServices(json.services);
175     }
176     return builder.build();
177   }
178
179   /**
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
185    */
186   public static ImmutableCatalogNode copyOf(CatalogNode instance) {
187     if (instance instanceof ImmutableCatalogNode) {
188       return (ImmutableCatalogNode) instance;
189     }
190     return ImmutableCatalogNode.builder()
191         .from(instance)
192         .build();
193   }
194
195   /**
196    * Creates a builder for {@link ImmutableCatalogNode ImmutableCatalogNode}.
197    * @return A new ImmutableCatalogNode builder
198    */
199   public static ImmutableCatalogNode.Builder builder() {
200     return new ImmutableCatalogNode.Builder();
201   }
202
203   /**
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>
209    */
210   public static final class Builder {
211     private static final long INIT_BIT_NODE = 0x1L;
212     private long initBits = 0x1;
213
214     private Node node;
215     private ImmutableMap.Builder<String, Service> servicesBuilder = ImmutableMap.builder();
216
217     private Builder() {
218     }
219
220     /**
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
227      */
228     public final Builder from(CatalogNode instance) {
229       Preconditions.checkNotNull(instance, "instance");
230       node(instance.getNode());
231       putAllServices(instance.getServices());
232       return this;
233     }
234
235     /**
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
239      */
240     public final Builder node(Node node) {
241       this.node = Preconditions.checkNotNull(node, "node");
242       initBits &= ~INIT_BIT_NODE;
243       return this;
244     }
245
246     /**
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
251      */
252     public final Builder putServices(String key, Service value) {
253       servicesBuilder.put(key, value);
254       return this;
255     }
256
257     /**
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
261      */
262     public final Builder putServices(Map.Entry<String, ? extends Service> entry) {
263       servicesBuilder.put(entry);
264       return this;
265     }
266
267     /**
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
271      */
272     public final Builder services(Map<String, ? extends Service> entries) {
273       servicesBuilder = ImmutableMap.builder();
274       return putAllServices(entries);
275     }
276
277     /**
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
281      */
282     public final Builder putAllServices(Map<String, ? extends Service> entries) {
283       servicesBuilder.putAll(entries);
284       return this;
285     }
286
287     /**
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
291      */
292     public ImmutableCatalogNode build() throws IllegalStateException {
293       if (initBits != 0) {
294         throw new IllegalStateException(formatRequiredAttributesMessage());
295       }
296       return new ImmutableCatalogNode(node, servicesBuilder.build());
297     }
298
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;
303     }
304   }
305 }