Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / consul / option / QueryOptions.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 static com.google.common.base.Preconditions.checkArgument;
17 import static org.onap.msb.sdclient.wrapper.consul.option.Options.optionallyAdd;
18
19 import java.math.BigInteger;
20
21 import javax.ws.rs.client.WebTarget;
22
23 import com.google.common.base.Optional;
24
25 /**
26  * Container for common query options used by the Consul API.
27  */
28
29 public abstract class QueryOptions implements ParamAdder {
30
31     public static final QueryOptions BLANK = ImmutableQueryOptions.builder().build();
32
33     public abstract Optional<String> getWait();
34
35     public abstract Optional<String> getToken();
36
37     public abstract Optional<BigInteger> getIndex();
38
39     public abstract Optional<String> getNear();
40
41
42     public ConsistencyMode getConsistencyMode() {
43         return ConsistencyMode.DEFAULT;
44     }
45
46
47     public boolean isBlocking() {
48         return getWait().isPresent();
49     }
50
51
52     public boolean hasToken() {
53         return getToken().isPresent();
54     }
55
56
57     void validate() {
58         if (isBlocking()) {
59             checkArgument(getIndex().isPresent(), "If wait is specified, index must also be specified");
60         }
61     }
62
63     public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) {
64         return blockBuilder("s", seconds, index);
65     }
66
67     public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) {
68         return blockBuilder("m", minutes, index);
69     }
70
71     private static ImmutableQueryOptions.Builder blockBuilder(String identifier, int qty, BigInteger index) {
72         return ImmutableQueryOptions.builder().wait(String.format("%s%s", qty, identifier)).index(index);
73     }
74
75     @Override
76     public WebTarget apply(WebTarget input) {
77
78         WebTarget added = input;
79         switch (getConsistencyMode()) {
80             case CONSISTENT:
81                 added = added.queryParam("consistent", "");
82                 break;
83             case STALE:
84                 added = added.queryParam("stale", "");
85                 break;
86         }
87
88         if (isBlocking()) {
89             added = added.queryParam("wait", getWait().get()).queryParam("index", String.valueOf(getIndex().get()));
90         }
91
92         added = optionallyAdd(added, "token", getToken());
93         added = optionallyAdd(added, "near", getToken());
94
95         return added;
96     }
97 }