Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / option / ImmutableQueryOptions.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.option;\r
18 \r
19 import com.google.common.base.MoreObjects;\r
20 import com.google.common.base.Optional;\r
21 import com.google.common.base.Preconditions;\r
22 import com.google.common.collect.Lists;\r
23 import com.google.common.primitives.Booleans;\r
24 import java.math.BigInteger;\r
25 import java.util.ArrayList;\r
26 import javax.annotation.Generated;\r
27 \r
28 /**\r
29  * Immutable implementation of {@link QueryOptions}.\r
30  * <p>\r
31  * Use the builder to create immutable instances:\r
32  * {@code ImmutableQueryOptions.builder()}.\r
33  */\r
34 @SuppressWarnings("all")\r
35 @Generated({"Immutables.generator", "QueryOptions"})\r
36 public final class ImmutableQueryOptions extends QueryOptions {\r
37   private final Optional<String> wait;\r
38   private final Optional<String> token;\r
39   private final Optional<BigInteger> index;\r
40   private final Optional<String> near;\r
41   private final ConsistencyMode consistencyMode;\r
42   private final boolean isBlocking;\r
43   private final boolean hasToken;\r
44 \r
45   private ImmutableQueryOptions(ImmutableQueryOptions.Builder builder) {\r
46     this.wait = builder.wait;\r
47     this.token = builder.token;\r
48     this.index = builder.index;\r
49     this.near = builder.near;\r
50     if (builder.consistencyMode != null) {\r
51       initShim.consistencyMode(builder.consistencyMode);\r
52     }\r
53     this.consistencyMode = initShim.getConsistencyMode();\r
54     this.isBlocking = initShim.isBlocking();\r
55     this.hasToken = initShim.hasToken();\r
56     this.initShim = null;\r
57   }\r
58 \r
59   private ImmutableQueryOptions(\r
60       Optional<String> wait,\r
61       Optional<String> token,\r
62       Optional<BigInteger> index,\r
63       Optional<String> near,\r
64       ConsistencyMode consistencyMode) {\r
65     this.wait = wait;\r
66     this.token = token;\r
67     this.index = index;\r
68     this.near = near;\r
69     this.consistencyMode = consistencyMode;\r
70     initShim.consistencyMode(consistencyMode);\r
71     this.isBlocking = initShim.isBlocking();\r
72     this.hasToken = initShim.hasToken();\r
73     this.initShim = null;\r
74   }\r
75 \r
76   private static final int STAGE_INITIALIZING = -1;\r
77   private static final int STAGE_UNINITIALIZED = 0;\r
78   private static final int STAGE_INITIALIZED = 1;\r
79   private volatile InitShim initShim = new InitShim();\r
80 \r
81   private final class InitShim {\r
82     private ConsistencyMode consistencyMode;\r
83     private byte consistencyModeStage;\r
84 \r
85     ConsistencyMode getConsistencyMode() {\r
86       if (consistencyModeStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());\r
87       if (consistencyModeStage == STAGE_UNINITIALIZED) {\r
88         consistencyModeStage = STAGE_INITIALIZING;\r
89         this.consistencyMode = Preconditions.checkNotNull(ImmutableQueryOptions.super.getConsistencyMode(), "consistencyMode");\r
90         consistencyModeStage = STAGE_INITIALIZED;\r
91       }\r
92       return consistencyMode;\r
93     }\r
94 \r
95     ConsistencyMode consistencyMode(ConsistencyMode value) {\r
96       this.consistencyMode = value;\r
97       consistencyModeStage = STAGE_INITIALIZED;\r
98       return value;\r
99     }\r
100     private boolean isBlocking;\r
101     private byte isBlockingStage;\r
102 \r
103     boolean isBlocking() {\r
104       if (isBlockingStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());\r
105       if (isBlockingStage == STAGE_UNINITIALIZED) {\r
106         isBlockingStage = STAGE_INITIALIZING;\r
107         this.isBlocking = ImmutableQueryOptions.super.isBlocking();\r
108         isBlockingStage = STAGE_INITIALIZED;\r
109       }\r
110       return isBlocking;\r
111     }\r
112     private boolean hasToken;\r
113     private byte hasTokenStage;\r
114 \r
115     boolean hasToken() {\r
116       if (hasTokenStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());\r
117       if (hasTokenStage == STAGE_UNINITIALIZED) {\r
118         hasTokenStage = STAGE_INITIALIZING;\r
119         this.hasToken = ImmutableQueryOptions.super.hasToken();\r
120         hasTokenStage = STAGE_INITIALIZED;\r
121       }\r
122       return hasToken;\r
123     }\r
124 \r
125     private String formatInitCycleMessage() {\r
126       ArrayList<String> attributes = Lists.newArrayList();\r
127       if (consistencyModeStage == STAGE_INITIALIZING) attributes.add("consistencyMode");\r
128       if (isBlockingStage == STAGE_INITIALIZING) attributes.add("isBlocking");\r
129       if (hasTokenStage == STAGE_INITIALIZING) attributes.add("hasToken");\r
130       return "Cannot build QueryOptions, attribute initializers form cycle" + attributes;\r
131     }\r
132   }\r
133 \r
134   /**\r
135    * @return The value of the {@code wait} attribute\r
136    */\r
137   @Override\r
138   public Optional<String> getWait() {\r
139     return wait;\r
140   }\r
141 \r
142   /**\r
143    * @return The value of the {@code token} attribute\r
144    */\r
145   @Override\r
146   public Optional<String> getToken() {\r
147     return token;\r
148   }\r
149 \r
150   /**\r
151    * @return The value of the {@code index} attribute\r
152    */\r
153   @Override\r
154   public Optional<BigInteger> getIndex() {\r
155     return index;\r
156   }\r
157 \r
158   /**\r
159    * @return The value of the {@code near} attribute\r
160    */\r
161   @Override\r
162   public Optional<String> getNear() {\r
163     return near;\r
164   }\r
165 \r
166   /**\r
167    * @return The value of the {@code consistencyMode} attribute\r
168    */\r
169   @Override\r
170   public ConsistencyMode getConsistencyMode() {\r
171     return initShim != null\r
172         ? initShim.getConsistencyMode()\r
173         : consistencyMode;\r
174   }\r
175 \r
176   /**\r
177    * @return The computed-at-construction value of the {@code isBlocking} attribute\r
178    */\r
179   @Override\r
180   public boolean isBlocking() {\r
181     return initShim != null\r
182         ? initShim.isBlocking()\r
183         : isBlocking;\r
184   }\r
185 \r
186   /**\r
187    * @return The computed-at-construction value of the {@code hasToken} attribute\r
188    */\r
189   @Override\r
190   public boolean hasToken() {\r
191     return initShim != null\r
192         ? initShim.hasToken()\r
193         : hasToken;\r
194   }\r
195 \r
196   /**\r
197    * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getWait() wait} attribute.\r
198    * @param value The value for wait\r
199    * @return A modified copy of {@code this} object\r
200    */\r
201   public final ImmutableQueryOptions withWait(String value) {\r
202     Optional<String> newValue = Optional.of(value);\r
203     return validate(new ImmutableQueryOptions(newValue, this.token, this.index, this.near, this.consistencyMode));\r
204   }\r
205 \r
206   /**\r
207    * Copy the current immutable object by setting an optional value for the {@link QueryOptions#getWait() wait} attribute.\r
208    * A shallow reference equality check on the optional value is used to prevent copying of the same value by returning {@code this}.\r
209    * @param optional A value for wait\r
210    * @return A modified copy of {@code this} object\r
211    */\r
212   public final ImmutableQueryOptions withWait(Optional<String> optional) {\r
213     Optional<String> value = Preconditions.checkNotNull(optional, "wait");\r
214     if (this.wait == value) return this;\r
215     return validate(new ImmutableQueryOptions(value, this.token, this.index, this.near, this.consistencyMode));\r
216   }\r
217 \r
218   /**\r
219    * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getToken() token} attribute.\r
220    * @param value The value for token\r
221    * @return A modified copy of {@code this} object\r
222    */\r
223   public final ImmutableQueryOptions withToken(String value) {\r
224     Optional<String> newValue = Optional.of(value);\r
225     return validate(new ImmutableQueryOptions(this.wait, newValue, this.index, this.near, this.consistencyMode));\r
226   }\r
227 \r
228   /**\r
229    * Copy the current immutable object by setting an optional value for the {@link QueryOptions#getToken() token} attribute.\r
230    * A shallow reference equality check on the optional value is used to prevent copying of the same value by returning {@code this}.\r
231    * @param optional A value for token\r
232    * @return A modified copy of {@code this} object\r
233    */\r
234   public final ImmutableQueryOptions withToken(Optional<String> optional) {\r
235     Optional<String> value = Preconditions.checkNotNull(optional, "token");\r
236     if (this.token == value) return this;\r
237     return validate(new ImmutableQueryOptions(this.wait, value, this.index, this.near, this.consistencyMode));\r
238   }\r
239 \r
240   /**\r
241    * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getIndex() index} attribute.\r
242    * @param value The value for index\r
243    * @return A modified copy of {@code this} object\r
244    */\r
245   public final ImmutableQueryOptions withIndex(BigInteger value) {\r
246     Optional<BigInteger> newValue = Optional.of(value);\r
247     return validate(new ImmutableQueryOptions(this.wait, this.token, newValue, this.near, this.consistencyMode));\r
248   }\r
249 \r
250   /**\r
251    * Copy the current immutable object by setting an optional value for the {@link QueryOptions#getIndex() index} attribute.\r
252    * A shallow reference equality check on the optional value is used to prevent copying of the same value by returning {@code this}.\r
253    * @param optional A value for index\r
254    * @return A modified copy of {@code this} object\r
255    */\r
256   public final ImmutableQueryOptions withIndex(Optional<BigInteger> optional) {\r
257     Optional<BigInteger> value = Preconditions.checkNotNull(optional, "index");\r
258     if (this.index == value) return this;\r
259     return validate(new ImmutableQueryOptions(this.wait, this.token, value, this.near, this.consistencyMode));\r
260   }\r
261 \r
262   /**\r
263    * Copy the current immutable object by setting a <i>present</i> value for the optional {@link QueryOptions#getNear() near} attribute.\r
264    * @param value The value for near\r
265    * @return A modified copy of {@code this} object\r
266    */\r
267   public final ImmutableQueryOptions withNear(String value) {\r
268     Optional<String> newValue = Optional.of(value);\r
269     return validate(new ImmutableQueryOptions(this.wait, this.token, this.index, newValue, this.consistencyMode));\r
270   }\r
271 \r
272   /**\r
273    * Copy the current immutable object by setting an optional value for the {@link QueryOptions#getNear() near} attribute.\r
274    * A shallow reference equality check on the optional value is used to prevent copying of the same value by returning {@code this}.\r
275    * @param optional A value for near\r
276    * @return A modified copy of {@code this} object\r
277    */\r
278   public final ImmutableQueryOptions withNear(Optional<String> optional) {\r
279     Optional<String> value = Preconditions.checkNotNull(optional, "near");\r
280     if (this.near == value) return this;\r
281     return validate(new ImmutableQueryOptions(this.wait, this.token, this.index, value, this.consistencyMode));\r
282   }\r
283 \r
284   /**\r
285    * Copy the current immutable object by setting a value for the {@link QueryOptions#getConsistencyMode() consistencyMode} attribute.\r
286    * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.\r
287    * @param value A new value for consistencyMode\r
288    * @return A modified copy of the {@code this} object\r
289    */\r
290   public final ImmutableQueryOptions withConsistencyMode(ConsistencyMode value) {\r
291     if (this.consistencyMode == value) return this;\r
292     return validate(new ImmutableQueryOptions(\r
293         this.wait,\r
294         this.token,\r
295         this.index,\r
296         this.near,\r
297         Preconditions.checkNotNull(value, "consistencyMode")));\r
298   }\r
299 \r
300   /**\r
301    * This instance is equal to all instances of {@code ImmutableQueryOptions} that have equal attribute values.\r
302    * @return {@code true} if {@code this} is equal to {@code another} instance\r
303    */\r
304   @Override\r
305   public boolean equals(Object another) {\r
306     if (this == another) return true;\r
307     return another instanceof ImmutableQueryOptions\r
308         && equalTo((ImmutableQueryOptions) another);\r
309   }\r
310 \r
311   private boolean equalTo(ImmutableQueryOptions another) {\r
312     return wait.equals(another.wait)\r
313         && token.equals(another.token)\r
314         && index.equals(another.index)\r
315         && near.equals(another.near)\r
316         && consistencyMode.equals(another.consistencyMode)\r
317         && isBlocking == another.isBlocking\r
318         && hasToken == another.hasToken;\r
319   }\r
320 \r
321   /**\r
322    * Computes a hash code from attributes: {@code wait}, {@code token}, {@code index}, {@code near}, {@code consistencyMode}, {@code isBlocking}, {@code hasToken}.\r
323    * @return hashCode value\r
324    */\r
325   @Override\r
326   public int hashCode() {\r
327     int h = 31;\r
328     h = h * 17 + wait.hashCode();\r
329     h = h * 17 + token.hashCode();\r
330     h = h * 17 + index.hashCode();\r
331     h = h * 17 + near.hashCode();\r
332     h = h * 17 + consistencyMode.hashCode();\r
333     h = h * 17 + Booleans.hashCode(isBlocking);\r
334     h = h * 17 + Booleans.hashCode(hasToken);\r
335     return h;\r
336   }\r
337 \r
338   /**\r
339    * Prints the immutable value {@code QueryOptions...} with all non-generated\r
340    * and non-auxiliary attribute values.\r
341    * @return A string representation of the value\r
342    */\r
343   @Override\r
344   public String toString() {\r
345     return MoreObjects.toStringHelper("QueryOptions")\r
346         .add("wait", wait)\r
347         .add("token", token)\r
348         .add("index", index)\r
349         .add("near", near)\r
350         .add("consistencyMode", consistencyMode)\r
351         .add("isBlocking", isBlocking)\r
352         .add("hasToken", hasToken)\r
353         .toString();\r
354   }\r
355 \r
356   private static ImmutableQueryOptions validate(ImmutableQueryOptions instance) {\r
357     instance.validate();\r
358     return instance;\r
359   }\r
360 \r
361   /**\r
362    * Creates an immutable copy of a {@link QueryOptions} value.\r
363    * Uses accessors to get values to initialize the new immutable instance.\r
364    * If an instance is already immutable, it is returned as is.\r
365    * @param instance The instance to copy\r
366    * @return A copied immutable QueryOptions instance\r
367    */\r
368   public static ImmutableQueryOptions copyOf(QueryOptions instance) {\r
369     if (instance instanceof ImmutableQueryOptions) {\r
370       return (ImmutableQueryOptions) instance;\r
371     }\r
372     return ImmutableQueryOptions.builder()\r
373         .from(instance)\r
374         .build();\r
375   }\r
376 \r
377   /**\r
378    * Creates a builder for {@link ImmutableQueryOptions ImmutableQueryOptions}.\r
379    * @return A new ImmutableQueryOptions builder\r
380    */\r
381   public static ImmutableQueryOptions.Builder builder() {\r
382     return new ImmutableQueryOptions.Builder();\r
383   }\r
384 \r
385   /**\r
386    * Builds instances of type {@link ImmutableQueryOptions ImmutableQueryOptions}.\r
387    * Initialize attributes and then invoke the {@link #build()} method to create an\r
388    * immutable instance.\r
389    * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,\r
390    * but instead used immediately to create instances.</em>\r
391    */\r
392   public static final class Builder {\r
393     private Optional<String> wait = Optional.absent();\r
394     private Optional<String> token = Optional.absent();\r
395     private Optional<BigInteger> index = Optional.absent();\r
396     private Optional<String> near = Optional.absent();\r
397     private ConsistencyMode consistencyMode;\r
398 \r
399     private Builder() {\r
400     }\r
401 \r
402     /**\r
403      * Fill a builder with attribute values from the provided {@code QueryOptions} instance.\r
404      * Regular attribute values will be replaced with those from the given instance.\r
405      * Absent optional values will not replace present values.\r
406      * @param instance The instance from which to copy values\r
407      * @return {@code this} builder for use in a chained invocation\r
408      */\r
409     public final Builder from(QueryOptions instance) {\r
410       Preconditions.checkNotNull(instance, "instance");\r
411       Optional<String> waitOptional = instance.getWait();\r
412       if (waitOptional.isPresent()) {\r
413         wait(waitOptional);\r
414       }\r
415       Optional<String> tokenOptional = instance.getToken();\r
416       if (tokenOptional.isPresent()) {\r
417         token(tokenOptional);\r
418       }\r
419       Optional<BigInteger> indexOptional = instance.getIndex();\r
420       if (indexOptional.isPresent()) {\r
421         index(indexOptional);\r
422       }\r
423       Optional<String> nearOptional = instance.getNear();\r
424       if (nearOptional.isPresent()) {\r
425         near(nearOptional);\r
426       }\r
427       consistencyMode(instance.getConsistencyMode());\r
428       return this;\r
429     }\r
430 \r
431     /**\r
432      * Initializes the optional value {@link QueryOptions#getWait() wait} to wait.\r
433      * @param wait The value for wait\r
434      * @return {@code this} builder for chained invocation\r
435      */\r
436     public final Builder wait(String wait) {\r
437       this.wait = Optional.of(wait);\r
438       return this;\r
439     }\r
440 \r
441     /**\r
442      * Initializes the optional value {@link QueryOptions#getWait() wait} to wait.\r
443      * @param wait The value for wait\r
444      * @return {@code this} builder for use in a chained invocation\r
445      */\r
446     public final Builder wait(Optional<String> wait) {\r
447       this.wait = Preconditions.checkNotNull(wait, "wait");\r
448       return this;\r
449     }\r
450 \r
451     /**\r
452      * Initializes the optional value {@link QueryOptions#getToken() token} to token.\r
453      * @param token The value for token\r
454      * @return {@code this} builder for chained invocation\r
455      */\r
456     public final Builder token(String token) {\r
457       this.token = Optional.of(token);\r
458       return this;\r
459     }\r
460 \r
461     /**\r
462      * Initializes the optional value {@link QueryOptions#getToken() token} to token.\r
463      * @param token The value for token\r
464      * @return {@code this} builder for use in a chained invocation\r
465      */\r
466     public final Builder token(Optional<String> token) {\r
467       this.token = Preconditions.checkNotNull(token, "token");\r
468       return this;\r
469     }\r
470 \r
471     /**\r
472      * Initializes the optional value {@link QueryOptions#getIndex() index} to index.\r
473      * @param index The value for index\r
474      * @return {@code this} builder for chained invocation\r
475      */\r
476     public final Builder index(BigInteger index) {\r
477       this.index = Optional.of(index);\r
478       return this;\r
479     }\r
480 \r
481     /**\r
482      * Initializes the optional value {@link QueryOptions#getIndex() index} to index.\r
483      * @param index The value for index\r
484      * @return {@code this} builder for use in a chained invocation\r
485      */\r
486     public final Builder index(Optional<BigInteger> index) {\r
487       this.index = Preconditions.checkNotNull(index, "index");\r
488       return this;\r
489     }\r
490 \r
491     /**\r
492      * Initializes the optional value {@link QueryOptions#getNear() near} to near.\r
493      * @param near The value for near\r
494      * @return {@code this} builder for chained invocation\r
495      */\r
496     public final Builder near(String near) {\r
497       this.near = Optional.of(near);\r
498       return this;\r
499     }\r
500 \r
501     /**\r
502      * Initializes the optional value {@link QueryOptions#getNear() near} to near.\r
503      * @param near The value for near\r
504      * @return {@code this} builder for use in a chained invocation\r
505      */\r
506     public final Builder near(Optional<String> near) {\r
507       this.near = Preconditions.checkNotNull(near, "near");\r
508       return this;\r
509     }\r
510 \r
511     /**\r
512      * Initializes the value for the {@link QueryOptions#getConsistencyMode() consistencyMode} attribute.\r
513      * <p><em>If not set, this attribute will have a default value as returned by the initializer of {@link QueryOptions#getConsistencyMode() consistencyMode}.</em>\r
514      * @param consistencyMode The value for consistencyMode \r
515      * @return {@code this} builder for use in a chained invocation\r
516      */\r
517     public final Builder consistencyMode(ConsistencyMode consistencyMode) {\r
518       this.consistencyMode = Preconditions.checkNotNull(consistencyMode, "consistencyMode");\r
519       return this;\r
520     }\r
521 \r
522     /**\r
523      * Builds a new {@link ImmutableQueryOptions ImmutableQueryOptions}.\r
524      * @return An immutable instance of QueryOptions\r
525      * @throws java.lang.IllegalStateException if any required attributes are missing\r
526      */\r
527     public ImmutableQueryOptions build() throws IllegalStateException {\r
528       return ImmutableQueryOptions.validate(new ImmutableQueryOptions(this));\r
529     }\r
530   }\r
531 }\r