2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.msb.sdclient.wrapper.consul.option;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
19 import javax.annotation.Generated;
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;
28 * Immutable implementation of {@link QueryOptions}.
30 * Use the builder to create immutable instances: {@code ImmutableQueryOptions.builder()}.
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;
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);
51 this.consistencyMode = initShim.getConsistencyMode();
52 this.isBlocking = initShim.isBlocking();
53 this.hasToken = initShim.hasToken();
57 private ImmutableQueryOptions(Optional<String> wait, Optional<String> token, Optional<BigInteger> index,
58 Optional<String> near, ConsistencyMode consistencyMode) {
63 this.consistencyMode = consistencyMode;
64 initShim.consistencyMode(consistencyMode);
65 this.isBlocking = initShim.isBlocking();
66 this.hasToken = initShim.hasToken();
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();
75 private final class InitShim {
76 private ConsistencyMode consistencyMode;
77 private byte consistencyModeStage;
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(),
86 consistencyModeStage = STAGE_INITIALIZED;
88 return consistencyMode;
91 ConsistencyMode consistencyMode(ConsistencyMode value) {
92 this.consistencyMode = value;
93 consistencyModeStage = STAGE_INITIALIZED;
97 private boolean isBlocking;
98 private byte isBlockingStage;
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;
111 private boolean hasToken;
112 private byte hasTokenStage;
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;
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;
138 * @return The value of the {@code wait} attribute
141 public Optional<String> getWait() {
146 * @return The value of the {@code token} attribute
149 public Optional<String> getToken() {
154 * @return The value of the {@code index} attribute
157 public Optional<BigInteger> getIndex() {
162 * @return The value of the {@code near} attribute
165 public Optional<String> getNear() {
170 * @return The value of the {@code consistencyMode} attribute
173 public ConsistencyMode getConsistencyMode() {
174 return initShim != null ? initShim.getConsistencyMode() : consistencyMode;
178 * @return The computed-at-construction value of the {@code isBlocking} attribute
181 public boolean isBlocking() {
182 return initShim != null ? initShim.isBlocking() : isBlocking;
186 * @return The computed-at-construction value of the {@code hasToken} attribute
189 public boolean hasToken() {
190 return initShim != null ? initShim.hasToken() : hasToken;
194 * Copy the current immutable object by setting a <i>present</i> value for the optional
195 * {@link QueryOptions#getWait() wait} attribute.
197 * @param value The value for wait
198 * @return A modified copy of {@code this} object
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));
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}.
210 * @param optional A value for wait
211 * @return A modified copy of {@code this} object
213 public final ImmutableQueryOptions withWait(Optional<String> optional) {
214 Optional<String> value = Preconditions.checkNotNull(optional, "wait");
215 if (this.wait == value)
217 return validate(new ImmutableQueryOptions(value, this.token, this.index, this.near, this.consistencyMode));
221 * Copy the current immutable object by setting a <i>present</i> value for the optional
222 * {@link QueryOptions#getToken() token} attribute.
224 * @param value The value for token
225 * @return A modified copy of {@code this} object
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));
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}.
237 * @param optional A value for token
238 * @return A modified copy of {@code this} object
240 public final ImmutableQueryOptions withToken(Optional<String> optional) {
241 Optional<String> value = Preconditions.checkNotNull(optional, "token");
242 if (this.token == value)
244 return validate(new ImmutableQueryOptions(this.wait, value, this.index, this.near, this.consistencyMode));
248 * Copy the current immutable object by setting a <i>present</i> value for the optional
249 * {@link QueryOptions#getIndex() index} attribute.
251 * @param value The value for index
252 * @return A modified copy of {@code this} object
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));
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}.
264 * @param optional A value for index
265 * @return A modified copy of {@code this} object
267 public final ImmutableQueryOptions withIndex(Optional<BigInteger> optional) {
268 Optional<BigInteger> value = Preconditions.checkNotNull(optional, "index");
269 if (this.index == value)
271 return validate(new ImmutableQueryOptions(this.wait, this.token, value, this.near, this.consistencyMode));
275 * Copy the current immutable object by setting a <i>present</i> value for the optional
276 * {@link QueryOptions#getNear() near} attribute.
278 * @param value The value for near
279 * @return A modified copy of {@code this} object
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));
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}.
291 * @param optional A value for near
292 * @return A modified copy of {@code this} object
294 public final ImmutableQueryOptions withNear(Optional<String> optional) {
295 Optional<String> value = Preconditions.checkNotNull(optional, "near");
296 if (this.near == value)
298 return validate(new ImmutableQueryOptions(this.wait, this.token, this.index, value, this.consistencyMode));
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}.
306 * @param value A new value for consistencyMode
307 * @return A modified copy of the {@code this} object
309 public final ImmutableQueryOptions withConsistencyMode(ConsistencyMode value) {
310 if (this.consistencyMode == value)
312 return validate(new ImmutableQueryOptions(this.wait, this.token, this.index, this.near,
313 Preconditions.checkNotNull(value, "consistencyMode")));
317 * This instance is equal to all instances of {@code ImmutableQueryOptions} that have equal
320 * @return {@code true} if {@code this} is equal to {@code another} instance
323 public boolean equals(Object another) {
326 return another instanceof ImmutableQueryOptions && equalTo((ImmutableQueryOptions) another);
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;
336 * Computes a hash code from attributes: {@code wait}, {@code token}, {@code index},
337 * {@code near}, {@code consistencyMode}, {@code isBlocking}, {@code hasToken}.
339 * @return hashCode value
342 public int hashCode() {
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);
355 * Prints the immutable value {@code QueryOptions...} with all non-generated and non-auxiliary
358 * @return A string representation of the value
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();
367 private static ImmutableQueryOptions validate(ImmutableQueryOptions instance) {
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
377 * @param instance The instance to copy
378 * @return A copied immutable QueryOptions instance
380 public static ImmutableQueryOptions copyOf(QueryOptions instance) {
381 if (instance instanceof ImmutableQueryOptions) {
382 return (ImmutableQueryOptions) instance;
384 return ImmutableQueryOptions.builder().from(instance).build();
388 * Creates a builder for {@link ImmutableQueryOptions ImmutableQueryOptions}.
390 * @return A new ImmutableQueryOptions builder
392 public static ImmutableQueryOptions.Builder builder() {
393 return new ImmutableQueryOptions.Builder();
397 * Builds instances of type {@link ImmutableQueryOptions ImmutableQueryOptions}. Initialize
398 * attributes and then invoke the {@link #build()} method to create an immutable instance.
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>
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;
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.
417 * @param instance The instance from which to copy values
418 * @return {@code this} builder for use in a chained invocation
420 public final Builder from(QueryOptions instance) {
421 Preconditions.checkNotNull(instance, "instance");
422 Optional<String> waitOptional = instance.getWait();
423 if (waitOptional.isPresent()) {
426 Optional<String> tokenOptional = instance.getToken();
427 if (tokenOptional.isPresent()) {
428 token(tokenOptional);
430 Optional<BigInteger> indexOptional = instance.getIndex();
431 if (indexOptional.isPresent()) {
432 index(indexOptional);
434 Optional<String> nearOptional = instance.getNear();
435 if (nearOptional.isPresent()) {
438 consistencyMode(instance.getConsistencyMode());
443 * Initializes the optional value {@link QueryOptions#getWait() wait} to wait.
445 * @param wait The value for wait
446 * @return {@code this} builder for chained invocation
448 public final Builder wait(String wait) {
449 this.wait = Optional.of(wait);
454 * Initializes the optional value {@link QueryOptions#getWait() wait} to wait.
456 * @param wait The value for wait
457 * @return {@code this} builder for use in a chained invocation
459 public final Builder wait(Optional<String> wait) {
460 this.wait = Preconditions.checkNotNull(wait, "wait");
465 * Initializes the optional value {@link QueryOptions#getToken() token} to token.
467 * @param token The value for token
468 * @return {@code this} builder for chained invocation
470 public final Builder token(String token) {
471 this.token = Optional.of(token);
476 * Initializes the optional value {@link QueryOptions#getToken() token} to token.
478 * @param token The value for token
479 * @return {@code this} builder for use in a chained invocation
481 public final Builder token(Optional<String> token) {
482 this.token = Preconditions.checkNotNull(token, "token");
487 * Initializes the optional value {@link QueryOptions#getIndex() index} to index.
489 * @param index The value for index
490 * @return {@code this} builder for chained invocation
492 public final Builder index(BigInteger index) {
493 this.index = Optional.of(index);
498 * Initializes the optional value {@link QueryOptions#getIndex() index} to index.
500 * @param index The value for index
501 * @return {@code this} builder for use in a chained invocation
503 public final Builder index(Optional<BigInteger> index) {
504 this.index = Preconditions.checkNotNull(index, "index");
509 * Initializes the optional value {@link QueryOptions#getNear() near} to near.
511 * @param near The value for near
512 * @return {@code this} builder for chained invocation
514 public final Builder near(String near) {
515 this.near = Optional.of(near);
520 * Initializes the optional value {@link QueryOptions#getNear() near} to near.
522 * @param near The value for near
523 * @return {@code this} builder for use in a chained invocation
525 public final Builder near(Optional<String> near) {
526 this.near = Preconditions.checkNotNull(near, "near");
531 * Initializes the value for the {@link QueryOptions#getConsistencyMode() consistencyMode}
534 * <em>If not set, this attribute will have a default value as returned by the initializer
535 * of {@link QueryOptions#getConsistencyMode() consistencyMode}.</em>
537 * @param consistencyMode The value for consistencyMode
538 * @return {@code this} builder for use in a chained invocation
540 public final Builder consistencyMode(ConsistencyMode consistencyMode) {
541 this.consistencyMode = Preconditions.checkNotNull(consistencyMode, "consistencyMode");
546 * Builds a new {@link ImmutableQueryOptions ImmutableQueryOptions}.
548 * @return An immutable instance of QueryOptions
549 * @throws java.lang.IllegalStateException if any required attributes are missing
551 public ImmutableQueryOptions build() throws IllegalStateException {
552 return ImmutableQueryOptions.validate(new ImmutableQueryOptions(this));