Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / consul / model / health / ImmutableNode.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.msb.sdclient.wrapper.consul.model.health;
15
16 import java.util.List;
17
18 import javax.annotation.Generated;
19
20 import com.fasterxml.jackson.annotation.JsonCreator;
21 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22 import com.fasterxml.jackson.annotation.JsonProperty;
23 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24 import com.google.common.base.MoreObjects;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Lists;
27
28 /**
29  * Immutable implementation of {@link Node}.
30  * <p>
31  * Use the builder to create immutable instances: {@code ImmutableNode.builder()}.
32  */
33 @SuppressWarnings("all")
34 @Generated({"Immutables.generator", "Node"})
35 @JsonIgnoreProperties(ignoreUnknown = true)
36 public final class ImmutableNode extends Node {
37     private final String node;
38     private final String address;
39
40     private ImmutableNode(String node, String address) {
41         this.node = node;
42         this.address = address;
43     }
44
45     /**
46      * @return The value of the {@code node} attribute
47      */
48     @JsonProperty(value = "Node")
49     @Override
50     public String getNode() {
51         return node;
52     }
53
54     /**
55      * @return The value of the {@code address} attribute
56      */
57     @JsonProperty(value = "Address")
58     @Override
59     public String getAddress() {
60         return address;
61     }
62
63     /**
64      * Copy the current immutable object by setting a value for the {@link Node#getNode() node}
65      * attribute. An equals check used to prevent copying of the same value by returning
66      * {@code this}.
67      * 
68      * @param value A new value for node
69      * @return A modified copy of the {@code this} object
70      */
71     public final ImmutableNode withNode(String value) {
72         if (this.node.equals(value))
73             return this;
74         return new ImmutableNode(Preconditions.checkNotNull(value, "node"), this.address);
75     }
76
77     /**
78      * Copy the current immutable object by setting a value for the {@link Node#getAddress()
79      * address} attribute. An equals check used to prevent copying of the same value by returning
80      * {@code this}.
81      * 
82      * @param value A new value for address
83      * @return A modified copy of the {@code this} object
84      */
85     public final ImmutableNode withAddress(String value) {
86         if (this.address.equals(value))
87             return this;
88         return new ImmutableNode(this.node, Preconditions.checkNotNull(value, "address"));
89     }
90
91     /**
92      * This instance is equal to all instances of {@code ImmutableNode} that have equal attribute
93      * values.
94      * 
95      * @return {@code true} if {@code this} is equal to {@code another} instance
96      */
97     @Override
98     public boolean equals(Object another) {
99         if (this == another)
100             return true;
101         return another instanceof ImmutableNode && equalTo((ImmutableNode) another);
102     }
103
104     private boolean equalTo(ImmutableNode another) {
105         return node.equals(another.node) && address.equals(another.address);
106     }
107
108     /**
109      * Computes a hash code from attributes: {@code node}, {@code address}.
110      * 
111      * @return hashCode value
112      */
113     @Override
114     public int hashCode() {
115         int h = 31;
116         h = h * 17 + node.hashCode();
117         h = h * 17 + address.hashCode();
118         return h;
119     }
120
121     /**
122      * Prints the immutable value {@code Node...} with all non-generated and non-auxiliary attribute
123      * values.
124      * 
125      * @return A string representation of the value
126      */
127     @Override
128     public String toString() {
129         return MoreObjects.toStringHelper("Node").add("node", node).add("address", address).toString();
130     }
131
132     /**
133      * Utility type used to correctly read immutable object from JSON representation.
134      * 
135      * @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding
136      *             infrastructure
137      */
138     @Deprecated
139     @JsonDeserialize
140     static final class Json extends Node {
141         String node;
142         String address;
143
144         @JsonProperty(value = "Node")
145         public void setNode(String node) {
146             this.node = node;
147         }
148
149         @JsonProperty(value = "Address")
150         public void setAddress(String address) {
151             this.address = address;
152         }
153
154         @Override
155         public String getNode() {
156             throw new UnsupportedOperationException();
157         }
158
159         @Override
160         public String getAddress() {
161             throw new UnsupportedOperationException();
162         }
163     }
164
165     /**
166      * @param json A JSON-bindable data structure
167      * @return An immutable value type
168      * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding
169      *             infrastructure
170      */
171     @Deprecated
172     @JsonCreator
173     static ImmutableNode fromJson(Json json) {
174         ImmutableNode.Builder builder = ImmutableNode.builder();
175         if (json.node != null) {
176             builder.node(json.node);
177         }
178         if (json.address != null) {
179             builder.address(json.address);
180         }
181         return builder.build();
182     }
183
184     /**
185      * Creates an immutable copy of a {@link Node} value. Uses accessors to get values to initialize
186      * the new immutable instance. If an instance is already immutable, it is returned as is.
187      * 
188      * @param instance The instance to copy
189      * @return A copied immutable Node instance
190      */
191     public static ImmutableNode copyOf(Node instance) {
192         if (instance instanceof ImmutableNode) {
193             return (ImmutableNode) instance;
194         }
195         return ImmutableNode.builder().from(instance).build();
196     }
197
198     /**
199      * Creates a builder for {@link ImmutableNode ImmutableNode}.
200      * 
201      * @return A new ImmutableNode builder
202      */
203     public static ImmutableNode.Builder builder() {
204         return new ImmutableNode.Builder();
205     }
206
207     /**
208      * Builds instances of type {@link ImmutableNode ImmutableNode}. Initialize attributes and then
209      * invoke the {@link #build()} method to create an immutable instance.
210      * <p>
211      * <em>{@code Builder} is not thread-safe and generally should not be stored in a field or
212      * collection, but instead used immediately to create instances.</em>
213      */
214     public static final class Builder {
215         private static final long INIT_BIT_NODE = 0x1L;
216         private static final long INIT_BIT_ADDRESS = 0x2L;
217         private long initBits = 0x3;
218
219         private String node;
220         private String address;
221
222         private Builder() {}
223
224         /**
225          * Fill a builder with attribute values from the provided {@code Node} instance. Regular
226          * attribute values will be replaced with those from the given instance. Absent optional
227          * values will not replace present values.
228          * 
229          * @param instance The instance from which to copy values
230          * @return {@code this} builder for use in a chained invocation
231          */
232         public final Builder from(Node instance) {
233             Preconditions.checkNotNull(instance, "instance");
234             node(instance.getNode());
235             address(instance.getAddress());
236             return this;
237         }
238
239         /**
240          * Initializes the value for the {@link Node#getNode() node} attribute.
241          * 
242          * @param node The value for node
243          * @return {@code this} builder for use in a chained invocation
244          */
245         public final Builder node(String node) {
246             this.node = Preconditions.checkNotNull(node, "node");
247             initBits &= ~INIT_BIT_NODE;
248             return this;
249         }
250
251         /**
252          * Initializes the value for the {@link Node#getAddress() address} attribute.
253          * 
254          * @param address The value for address
255          * @return {@code this} builder for use in a chained invocation
256          */
257         public final Builder address(String address) {
258             this.address = Preconditions.checkNotNull(address, "address");
259             initBits &= ~INIT_BIT_ADDRESS;
260             return this;
261         }
262
263         /**
264          * Builds a new {@link ImmutableNode ImmutableNode}.
265          * 
266          * @return An immutable instance of Node
267          * @throws java.lang.IllegalStateException if any required attributes are missing
268          */
269         public ImmutableNode build() throws IllegalStateException {
270             if (initBits != 0) {
271                 throw new IllegalStateException(formatRequiredAttributesMessage());
272             }
273             return new ImmutableNode(node, address);
274         }
275
276         private String formatRequiredAttributesMessage() {
277             List<String> attributes = Lists.newArrayList();
278             if ((initBits & INIT_BIT_NODE) != 0)
279                 attributes.add("node");
280             if ((initBits & INIT_BIT_ADDRESS) != 0)
281                 attributes.add("address");
282             return "Cannot build Node, some of required attributes are not set " + attributes;
283         }
284     }
285 }