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