6d4717d81434ee4393020c2805c9b080cc9ccf8e
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / model / health / ImmutableService.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.health;\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.ImmutableList;\r
26 import com.google.common.collect.Lists;\r
27 import java.util.List;\r
28 import javax.annotation.Generated;\r
29 \r
30 /**\r
31  * Immutable implementation of {@link Service}.\r
32  * <p>\r
33  * Use the builder to create immutable instances:\r
34  * {@code ImmutableService.builder()}.\r
35  */\r
36 @SuppressWarnings("all")\r
37 @Generated({"Immutables.generator", "Service"})\r
38 @JsonIgnoreProperties(ignoreUnknown = true)\r
39 public final class ImmutableService extends Service {\r
40   private final String id;\r
41   private final String service;\r
42   private final ImmutableList<String> tags;\r
43   private final String address;\r
44   private final int port;\r
45 \r
46   private ImmutableService(\r
47       String id,\r
48       String service,\r
49       ImmutableList<String> tags,\r
50       String address,\r
51       int port) {\r
52     this.id = id;\r
53     this.service = service;\r
54     this.tags = tags;\r
55     this.address = address;\r
56     this.port = port;\r
57   }\r
58 \r
59   /**\r
60    * @return The value of the {@code id} attribute\r
61    */\r
62   @JsonProperty(value = "ID")\r
63   @Override\r
64   public String getId() {\r
65     return id;\r
66   }\r
67 \r
68   /**\r
69    * @return The value of the {@code service} attribute\r
70    */\r
71   @JsonProperty(value = "Service")\r
72   @Override\r
73   public String getService() {\r
74     return service;\r
75   }\r
76 \r
77   /**\r
78    * @return The value of the {@code tags} attribute\r
79    */\r
80   @JsonProperty(value = "Tags")\r
81   @JsonDeserialize(as = ImmutableList.class, contentAs = String.class)\r
82   @Override\r
83   public ImmutableList<String> getTags() {\r
84     return tags;\r
85   }\r
86 \r
87   /**\r
88    * @return The value of the {@code address} attribute\r
89    */\r
90   @JsonProperty(value = "Address")\r
91   @Override\r
92   public String getAddress() {\r
93     return address;\r
94   }\r
95 \r
96   /**\r
97    * @return The value of the {@code port} attribute\r
98    */\r
99   @JsonProperty(value = "Port")\r
100   @Override\r
101   public int getPort() {\r
102     return port;\r
103   }\r
104 \r
105   /**\r
106    * Copy the current immutable object by setting a value for the {@link Service#getId() id} attribute.\r
107    * An equals check used to prevent copying of the same value by returning {@code this}.\r
108    * @param value A new value for id\r
109    * @return A modified copy of the {@code this} object\r
110    */\r
111   public final ImmutableService withId(String value) {\r
112     if (this.id.equals(value)) return this;\r
113     return new ImmutableService(\r
114         Preconditions.checkNotNull(value, "id"),\r
115         this.service,\r
116         this.tags,\r
117         this.address,\r
118         this.port);\r
119   }\r
120 \r
121   /**\r
122    * Copy the current immutable object by setting a value for the {@link Service#getService() service} attribute.\r
123    * An equals check used to prevent copying of the same value by returning {@code this}.\r
124    * @param value A new value for service\r
125    * @return A modified copy of the {@code this} object\r
126    */\r
127   public final ImmutableService withService(String value) {\r
128     if (this.service.equals(value)) return this;\r
129     return new ImmutableService(\r
130         this.id,\r
131         Preconditions.checkNotNull(value, "service"),\r
132         this.tags,\r
133         this.address,\r
134         this.port);\r
135   }\r
136 \r
137   /**\r
138    * Copy the current immutable object with elements that replace the content of {@link Service#getTags() tags}.\r
139    * @param elements The elements to set\r
140    * @return A modified copy of {@code this} object\r
141    */\r
142   public final ImmutableService withTags(String... elements) {\r
143     ImmutableList<String> newValue = ImmutableList.copyOf(elements);\r
144     return new ImmutableService(this.id, this.service, newValue, this.address, this.port);\r
145   }\r
146 \r
147   /**\r
148    * Copy the current immutable object with elements that replace the content of {@link Service#getTags() tags}.\r
149    * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.\r
150    * @param elements An iterable of tags elements to set\r
151    * @return A modified copy of {@code this} object\r
152    */\r
153   public final ImmutableService withTags(Iterable<String> elements) {\r
154     if (this.tags == elements) return this;\r
155     ImmutableList<String> newValue = ImmutableList.copyOf(elements);\r
156     return new ImmutableService(this.id, this.service, newValue, this.address, this.port);\r
157   }\r
158 \r
159   /**\r
160    * Copy the current immutable object by setting a value for the {@link Service#getAddress() address} attribute.\r
161    * An equals check used to prevent copying of the same value by returning {@code this}.\r
162    * @param value A new value for address\r
163    * @return A modified copy of the {@code this} object\r
164    */\r
165   public final ImmutableService withAddress(String value) {\r
166     if (this.address.equals(value)) return this;\r
167     return new ImmutableService(\r
168         this.id,\r
169         this.service,\r
170         this.tags,\r
171         Preconditions.checkNotNull(value, "address"),\r
172         this.port);\r
173   }\r
174 \r
175   /**\r
176    * Copy the current immutable object by setting a value for the {@link Service#getPort() port} attribute.\r
177    * A value equality check is used to prevent copying of the same value by returning {@code this}.\r
178    * @param value A new value for port\r
179    * @return A modified copy of the {@code this} object\r
180    */\r
181   public final ImmutableService withPort(int value) {\r
182     if (this.port == value) return this;\r
183     return new ImmutableService(this.id, this.service, this.tags, this.address, value);\r
184   }\r
185 \r
186   /**\r
187    * This instance is equal to all instances of {@code ImmutableService} that have equal attribute values.\r
188    * @return {@code true} if {@code this} is equal to {@code another} instance\r
189    */\r
190   @Override\r
191   public boolean equals(Object another) {\r
192     if (this == another) return true;\r
193     return another instanceof ImmutableService\r
194         && equalTo((ImmutableService) another);\r
195   }\r
196 \r
197   private boolean equalTo(ImmutableService another) {\r
198     return id.equals(another.id)\r
199         && service.equals(another.service)\r
200         && tags.equals(another.tags)\r
201         && address.equals(another.address)\r
202         && port == another.port;\r
203   }\r
204 \r
205   /**\r
206    * Computes a hash code from attributes: {@code id}, {@code service}, {@code tags}, {@code address}, {@code port}.\r
207    * @return hashCode value\r
208    */\r
209   @Override\r
210   public int hashCode() {\r
211     int h = 31;\r
212     h = h * 17 + id.hashCode();\r
213     h = h * 17 + service.hashCode();\r
214     h = h * 17 + tags.hashCode();\r
215     h = h * 17 + address.hashCode();\r
216     h = h * 17 + port;\r
217     return h;\r
218   }\r
219 \r
220   /**\r
221    * Prints the immutable value {@code Service...} with all non-generated\r
222    * and non-auxiliary attribute values.\r
223    * @return A string representation of the value\r
224    */\r
225   @Override\r
226   public String toString() {\r
227     return MoreObjects.toStringHelper("Service")\r
228         .add("id", id)\r
229         .add("service", service)\r
230         .add("tags", tags)\r
231         .add("address", address)\r
232         .add("port", port)\r
233         .toString();\r
234   }\r
235 \r
236   /**\r
237    * Utility type used to correctly read immutable object from JSON representation.\r
238    * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure\r
239    */\r
240   @Deprecated\r
241   @JsonDeserialize\r
242   static final class Json extends Service {\r
243     String id;\r
244     String service;\r
245     List<String> tags = ImmutableList.of();\r
246     String address;\r
247     Integer port;\r
248     @JsonProperty(value = "ID")\r
249     public void setId(String id) {\r
250       this.id = id;\r
251     }\r
252     @JsonProperty(value = "Service")\r
253     public void setService(String service) {\r
254       this.service = service;\r
255     }\r
256     @JsonProperty(value = "Tags")\r
257     @JsonDeserialize(as = ImmutableList.class, contentAs = String.class)\r
258     public void setTags(List<String> tags) {\r
259       this.tags = tags;\r
260     }\r
261     @JsonProperty(value = "Address")\r
262     public void setAddress(String address) {\r
263       this.address = address;\r
264     }\r
265     @JsonProperty(value = "Port")\r
266     public void setPort(int port) {\r
267       this.port = port;\r
268     }\r
269     @Override\r
270     public String getId() { throw new UnsupportedOperationException(); }\r
271     @Override\r
272     public String getService() { throw new UnsupportedOperationException(); }\r
273     @Override\r
274     public List<String> getTags() { throw new UnsupportedOperationException(); }\r
275     @Override\r
276     public String getAddress() { throw new UnsupportedOperationException(); }\r
277     @Override\r
278     public int getPort() { throw new UnsupportedOperationException(); }\r
279   }\r
280 \r
281   /**\r
282    * @param json A JSON-bindable data structure\r
283    * @return An immutable value type\r
284    * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure\r
285    */\r
286   @Deprecated\r
287   @JsonCreator\r
288   static ImmutableService fromJson(Json json) {\r
289     ImmutableService.Builder builder = ImmutableService.builder();\r
290     if (json.id != null) {\r
291       builder.id(json.id);\r
292     }\r
293     if (json.service != null) {\r
294       builder.service(json.service);\r
295     }\r
296     if (json.tags != null) {\r
297       builder.addAllTags(json.tags);\r
298     }\r
299     if (json.address != null) {\r
300       builder.address(json.address);\r
301     }\r
302     if (json.port != null) {\r
303       builder.port(json.port);\r
304     }\r
305     return builder.build();\r
306   }\r
307 \r
308   /**\r
309    * Creates an immutable copy of a {@link Service} value.\r
310    * Uses accessors to get values to initialize the new immutable instance.\r
311    * If an instance is already immutable, it is returned as is.\r
312    * @param instance The instance to copy\r
313    * @return A copied immutable Service instance\r
314    */\r
315   public static ImmutableService copyOf(Service instance) {\r
316     if (instance instanceof ImmutableService) {\r
317       return (ImmutableService) instance;\r
318     }\r
319     return ImmutableService.builder()\r
320         .from(instance)\r
321         .build();\r
322   }\r
323 \r
324   /**\r
325    * Creates a builder for {@link ImmutableService ImmutableService}.\r
326    * @return A new ImmutableService builder\r
327    */\r
328   public static ImmutableService.Builder builder() {\r
329     return new ImmutableService.Builder();\r
330   }\r
331 \r
332   /**\r
333    * Builds instances of type {@link ImmutableService ImmutableService}.\r
334    * Initialize attributes and then invoke the {@link #build()} method to create an\r
335    * immutable instance.\r
336    * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,\r
337    * but instead used immediately to create instances.</em>\r
338    */\r
339   public static final class Builder {\r
340     private static final long INIT_BIT_ID = 0x1L;\r
341     private static final long INIT_BIT_SERVICE = 0x2L;\r
342     private static final long INIT_BIT_ADDRESS = 0x4L;\r
343     private static final long INIT_BIT_PORT = 0x8L;\r
344     private long initBits = 0xf;\r
345 \r
346     private String id;\r
347     private String service;\r
348     private ImmutableList.Builder<String> tagsBuilder = ImmutableList.builder();\r
349     private String address;\r
350     private int port;\r
351 \r
352     private Builder() {\r
353     }\r
354 \r
355     /**\r
356      * Fill a builder with attribute values from the provided {@code Service} instance.\r
357      * Regular attribute values will be replaced with those from the given instance.\r
358      * Absent optional values will not replace present values.\r
359      * Collection elements and entries will be added, not replaced.\r
360      * @param instance The instance from which to copy values\r
361      * @return {@code this} builder for use in a chained invocation\r
362      */\r
363     public final Builder from(Service instance) {\r
364       Preconditions.checkNotNull(instance, "instance");\r
365       id(instance.getId());\r
366       service(instance.getService());\r
367       addAllTags(instance.getTags());\r
368       address(instance.getAddress());\r
369       port(instance.getPort());\r
370       return this;\r
371     }\r
372 \r
373     /**\r
374      * Initializes the value for the {@link Service#getId() id} attribute.\r
375      * @param id The value for id \r
376      * @return {@code this} builder for use in a chained invocation\r
377      */\r
378     public final Builder id(String id) {\r
379       this.id = Preconditions.checkNotNull(id, "id");\r
380       initBits &= ~INIT_BIT_ID;\r
381       return this;\r
382     }\r
383 \r
384     /**\r
385      * Initializes the value for the {@link Service#getService() service} attribute.\r
386      * @param service The value for service \r
387      * @return {@code this} builder for use in a chained invocation\r
388      */\r
389     public final Builder service(String service) {\r
390       this.service = Preconditions.checkNotNull(service, "service");\r
391       initBits &= ~INIT_BIT_SERVICE;\r
392       return this;\r
393     }\r
394 \r
395     /**\r
396      * Adds one element to {@link Service#getTags() tags} list.\r
397      * @param element A tags element\r
398      * @return {@code this} builder for use in a chained invocation\r
399      */\r
400     public final Builder addTags(String element) {\r
401       tagsBuilder.add(element);\r
402       return this;\r
403     }\r
404 \r
405     /**\r
406      * Adds elements to {@link Service#getTags() tags} list.\r
407      * @param elements An array of tags elements\r
408      * @return {@code this} builder for use in a chained invocation\r
409      */\r
410     public final Builder addTags(String... elements) {\r
411       tagsBuilder.add(elements);\r
412       return this;\r
413     }\r
414 \r
415     /**\r
416      * Sets or replaces all elements for {@link Service#getTags() tags} list.\r
417      * @param elements An iterable of tags elements\r
418      * @return {@code this} builder for use in a chained invocation\r
419      */\r
420     public final Builder tags(Iterable<String> elements) {\r
421       tagsBuilder = ImmutableList.builder();\r
422       return addAllTags(elements);\r
423     }\r
424 \r
425     /**\r
426      * Adds elements to {@link Service#getTags() tags} list.\r
427      * @param elements An iterable of tags elements\r
428      * @return {@code this} builder for use in a chained invocation\r
429      */\r
430     public final Builder addAllTags(Iterable<String> elements) {\r
431       tagsBuilder.addAll(elements);\r
432       return this;\r
433     }\r
434 \r
435     /**\r
436      * Initializes the value for the {@link Service#getAddress() address} attribute.\r
437      * @param address The value for address \r
438      * @return {@code this} builder for use in a chained invocation\r
439      */\r
440     public final Builder address(String address) {\r
441       this.address = Preconditions.checkNotNull(address, "address");\r
442       initBits &= ~INIT_BIT_ADDRESS;\r
443       return this;\r
444     }\r
445 \r
446     /**\r
447      * Initializes the value for the {@link Service#getPort() port} attribute.\r
448      * @param port The value for port \r
449      * @return {@code this} builder for use in a chained invocation\r
450      */\r
451     public final Builder port(int port) {\r
452       this.port = port;\r
453       initBits &= ~INIT_BIT_PORT;\r
454       return this;\r
455     }\r
456 \r
457     /**\r
458      * Builds a new {@link ImmutableService ImmutableService}.\r
459      * @return An immutable instance of Service\r
460      * @throws java.lang.IllegalStateException if any required attributes are missing\r
461      */\r
462     public ImmutableService build() throws IllegalStateException {\r
463       if (initBits != 0) {\r
464         throw new IllegalStateException(formatRequiredAttributesMessage());\r
465       }\r
466       return new ImmutableService(id, service, tagsBuilder.build(), address, port);\r
467     }\r
468 \r
469     private String formatRequiredAttributesMessage() {\r
470       List<String> attributes = Lists.newArrayList();\r
471       if ((initBits & INIT_BIT_ID) != 0) attributes.add("id");\r
472       if ((initBits & INIT_BIT_SERVICE) != 0) attributes.add("service");\r
473       if ((initBits & INIT_BIT_ADDRESS) != 0) attributes.add("address");\r
474       if ((initBits & INIT_BIT_PORT) != 0) attributes.add("port");\r
475       return "Cannot build Service, some of required attributes are not set " + attributes;\r
476     }\r
477   }\r
478 }\r