2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.openo.msb.wrapper.consul.option;
\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
29 * Immutable implementation of {@link QueryOptions}.
\r
31 * Use the builder to create immutable instances:
\r
32 * {@code ImmutableQueryOptions.builder()}.
\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
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
53 this.consistencyMode = initShim.getConsistencyMode();
\r
54 this.isBlocking = initShim.isBlocking();
\r
55 this.hasToken = initShim.hasToken();
\r
56 this.initShim = null;
\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
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
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
81 private final class InitShim {
\r
82 private ConsistencyMode consistencyMode;
\r
83 private byte consistencyModeStage;
\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
92 return consistencyMode;
\r
95 ConsistencyMode consistencyMode(ConsistencyMode value) {
\r
96 this.consistencyMode = value;
\r
97 consistencyModeStage = STAGE_INITIALIZED;
\r
100 private boolean isBlocking;
\r
101 private byte isBlockingStage;
\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
112 private boolean hasToken;
\r
113 private byte hasTokenStage;
\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
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
135 * @return The value of the {@code wait} attribute
\r
138 public Optional<String> getWait() {
\r
143 * @return The value of the {@code token} attribute
\r
146 public Optional<String> getToken() {
\r
151 * @return The value of the {@code index} attribute
\r
154 public Optional<BigInteger> getIndex() {
\r
159 * @return The value of the {@code near} attribute
\r
162 public Optional<String> getNear() {
\r
167 * @return The value of the {@code consistencyMode} attribute
\r
170 public ConsistencyMode getConsistencyMode() {
\r
171 return initShim != null
\r
172 ? initShim.getConsistencyMode()
\r
177 * @return The computed-at-construction value of the {@code isBlocking} attribute
\r
180 public boolean isBlocking() {
\r
181 return initShim != null
\r
182 ? initShim.isBlocking()
\r
187 * @return The computed-at-construction value of the {@code hasToken} attribute
\r
190 public boolean hasToken() {
\r
191 return initShim != null
\r
192 ? initShim.hasToken()
\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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
290 public final ImmutableQueryOptions withConsistencyMode(ConsistencyMode value) {
\r
291 if (this.consistencyMode == value) return this;
\r
292 return validate(new ImmutableQueryOptions(
\r
297 Preconditions.checkNotNull(value, "consistencyMode")));
\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
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
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
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
326 public int hashCode() {
\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
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
344 public String toString() {
\r
345 return MoreObjects.toStringHelper("QueryOptions")
\r
347 .add("token", token)
\r
348 .add("index", index)
\r
350 .add("consistencyMode", consistencyMode)
\r
351 .add("isBlocking", isBlocking)
\r
352 .add("hasToken", hasToken)
\r
356 private static ImmutableQueryOptions validate(ImmutableQueryOptions instance) {
\r
357 instance.validate();
\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
368 public static ImmutableQueryOptions copyOf(QueryOptions instance) {
\r
369 if (instance instanceof ImmutableQueryOptions) {
\r
370 return (ImmutableQueryOptions) instance;
\r
372 return ImmutableQueryOptions.builder()
\r
378 * Creates a builder for {@link ImmutableQueryOptions ImmutableQueryOptions}.
\r
379 * @return A new ImmutableQueryOptions builder
\r
381 public static ImmutableQueryOptions.Builder builder() {
\r
382 return new ImmutableQueryOptions.Builder();
\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
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
399 private Builder() {
\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
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
415 Optional<String> tokenOptional = instance.getToken();
\r
416 if (tokenOptional.isPresent()) {
\r
417 token(tokenOptional);
\r
419 Optional<BigInteger> indexOptional = instance.getIndex();
\r
420 if (indexOptional.isPresent()) {
\r
421 index(indexOptional);
\r
423 Optional<String> nearOptional = instance.getNear();
\r
424 if (nearOptional.isPresent()) {
\r
425 near(nearOptional);
\r
427 consistencyMode(instance.getConsistencyMode());
\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
436 public final Builder wait(String wait) {
\r
437 this.wait = Optional.of(wait);
\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
446 public final Builder wait(Optional<String> wait) {
\r
447 this.wait = Preconditions.checkNotNull(wait, "wait");
\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
456 public final Builder token(String token) {
\r
457 this.token = Optional.of(token);
\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
466 public final Builder token(Optional<String> token) {
\r
467 this.token = Preconditions.checkNotNull(token, "token");
\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
476 public final Builder index(BigInteger index) {
\r
477 this.index = Optional.of(index);
\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
486 public final Builder index(Optional<BigInteger> index) {
\r
487 this.index = Preconditions.checkNotNull(index, "index");
\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
496 public final Builder near(String near) {
\r
497 this.near = Optional.of(near);
\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
506 public final Builder near(Optional<String> near) {
\r
507 this.near = Preconditions.checkNotNull(near, "near");
\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
517 public final Builder consistencyMode(ConsistencyMode consistencyMode) {
\r
518 this.consistencyMode = Preconditions.checkNotNull(consistencyMode, "consistencyMode");
\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
527 public ImmutableQueryOptions build() throws IllegalStateException {
\r
528 return ImmutableQueryOptions.validate(new ImmutableQueryOptions(this));
\r