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