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