2 * Copyright 2016 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.msb.sdclient.wrapper.consul.option;
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;
28 * Immutable implementation of {@link QueryOptions}.
30 * Use the builder to create immutable instances:
31 * {@code ImmutableQueryOptions.builder()}.
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;
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);
52 this.consistencyMode = initShim.getConsistencyMode();
53 this.isBlocking = initShim.isBlocking();
54 this.hasToken = initShim.hasToken();
58 private ImmutableQueryOptions(
59 Optional<String> wait,
60 Optional<String> token,
61 Optional<BigInteger> index,
62 Optional<String> near,
63 ConsistencyMode consistencyMode) {
68 this.consistencyMode = consistencyMode;
69 initShim.consistencyMode(consistencyMode);
70 this.isBlocking = initShim.isBlocking();
71 this.hasToken = initShim.hasToken();
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();
80 private final class InitShim {
81 private ConsistencyMode consistencyMode;
82 private byte consistencyModeStage;
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;
91 return consistencyMode;
94 ConsistencyMode consistencyMode(ConsistencyMode value) {
95 this.consistencyMode = value;
96 consistencyModeStage = STAGE_INITIALIZED;
99 private boolean isBlocking;
100 private byte isBlockingStage;
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;
111 private boolean hasToken;
112 private byte hasTokenStage;
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;
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;
134 * @return The value of the {@code wait} attribute
137 public Optional<String> getWait() {
142 * @return The value of the {@code token} attribute
145 public Optional<String> getToken() {
150 * @return The value of the {@code index} attribute
153 public Optional<BigInteger> getIndex() {
158 * @return The value of the {@code near} attribute
161 public Optional<String> getNear() {
166 * @return The value of the {@code consistencyMode} attribute
169 public ConsistencyMode getConsistencyMode() {
170 return initShim != null
171 ? initShim.getConsistencyMode()
176 * @return The computed-at-construction value of the {@code isBlocking} attribute
179 public boolean isBlocking() {
180 return initShim != null
181 ? initShim.isBlocking()
186 * @return The computed-at-construction value of the {@code hasToken} attribute
189 public boolean hasToken() {
190 return initShim != null
191 ? initShim.hasToken()
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
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 {@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
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));
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
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));
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
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));
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
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));
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
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));
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
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));
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
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));
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
289 public final ImmutableQueryOptions withConsistencyMode(ConsistencyMode value) {
290 if (this.consistencyMode == value) return this;
291 return validate(new ImmutableQueryOptions(
296 Preconditions.checkNotNull(value, "consistencyMode")));
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
304 public boolean equals(Object another) {
305 if (this == another) return true;
306 return another instanceof ImmutableQueryOptions
307 && equalTo((ImmutableQueryOptions) another);
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;
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
325 public int hashCode() {
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);
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
343 public String toString() {
344 return MoreObjects.toStringHelper("QueryOptions")
349 .add("consistencyMode", consistencyMode)
350 .add("isBlocking", isBlocking)
351 .add("hasToken", hasToken)
355 private static ImmutableQueryOptions validate(ImmutableQueryOptions instance) {
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
367 public static ImmutableQueryOptions copyOf(QueryOptions instance) {
368 if (instance instanceof ImmutableQueryOptions) {
369 return (ImmutableQueryOptions) instance;
371 return ImmutableQueryOptions.builder()
377 * Creates a builder for {@link ImmutableQueryOptions ImmutableQueryOptions}.
378 * @return A new ImmutableQueryOptions builder
380 public static ImmutableQueryOptions.Builder builder() {
381 return new ImmutableQueryOptions.Builder();
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>
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;
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
408 public final Builder from(QueryOptions instance) {
409 Preconditions.checkNotNull(instance, "instance");
410 Optional<String> waitOptional = instance.getWait();
411 if (waitOptional.isPresent()) {
414 Optional<String> tokenOptional = instance.getToken();
415 if (tokenOptional.isPresent()) {
416 token(tokenOptional);
418 Optional<BigInteger> indexOptional = instance.getIndex();
419 if (indexOptional.isPresent()) {
420 index(indexOptional);
422 Optional<String> nearOptional = instance.getNear();
423 if (nearOptional.isPresent()) {
426 consistencyMode(instance.getConsistencyMode());
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
435 public final Builder wait(String wait) {
436 this.wait = Optional.of(wait);
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
445 public final Builder wait(Optional<String> wait) {
446 this.wait = Preconditions.checkNotNull(wait, "wait");
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
455 public final Builder token(String token) {
456 this.token = Optional.of(token);
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
465 public final Builder token(Optional<String> token) {
466 this.token = Preconditions.checkNotNull(token, "token");
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
475 public final Builder index(BigInteger index) {
476 this.index = Optional.of(index);
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
485 public final Builder index(Optional<BigInteger> index) {
486 this.index = Preconditions.checkNotNull(index, "index");
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
495 public final Builder near(String near) {
496 this.near = Optional.of(near);
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
505 public final Builder near(Optional<String> near) {
506 this.near = Preconditions.checkNotNull(near, "near");
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
516 public final Builder consistencyMode(ConsistencyMode consistencyMode) {
517 this.consistencyMode = Preconditions.checkNotNull(consistencyMode, "consistencyMode");
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
526 public ImmutableQueryOptions build() throws IllegalStateException {
527 return ImmutableQueryOptions.validate(new ImmutableQueryOptions(this));