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