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