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