-
Notifications
You must be signed in to change notification settings - Fork 251
/
Template.java
281 lines (257 loc) · 9.06 KB
/
Template.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.remoteconfig;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.json.JsonFactory;
import com.google.common.base.Strings;
import com.google.firebase.ErrorCode;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.NonNull;
import com.google.firebase.remoteconfig.internal.TemplateResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Represents a Remote Config template.
*/
public final class Template {
private String etag;
private Map<String, Parameter> parameters;
private List<Condition> conditions;
private Map<String, ParameterGroup> parameterGroups;
private Version version;
/**
* Creates a new {@link Template}.
*
* @param etag The ETag of this template.
*/
public Template(String etag) {
this.parameters = new HashMap<>();
this.conditions = new ArrayList<>();
this.parameterGroups = new HashMap<>();
this.etag = etag;
}
Template() {
this((String) null);
}
Template(@NonNull TemplateResponse templateResponse) {
checkNotNull(templateResponse);
this.parameters = new HashMap<>();
this.conditions = new ArrayList<>();
this.parameterGroups = new HashMap<>();
if (templateResponse.getParameters() != null) {
for (Map.Entry<String, TemplateResponse.ParameterResponse> entry
: templateResponse.getParameters().entrySet()) {
this.parameters.put(entry.getKey(), new Parameter(entry.getValue()));
}
}
if (templateResponse.getConditions() != null) {
for (TemplateResponse.ConditionResponse conditionResponse
: templateResponse.getConditions()) {
this.conditions.add(new Condition(conditionResponse));
}
}
if (templateResponse.getParameterGroups() != null) {
for (Map.Entry<String, TemplateResponse.ParameterGroupResponse> entry
: templateResponse.getParameterGroups().entrySet()) {
this.parameterGroups.put(entry.getKey(), new ParameterGroup(entry.getValue()));
}
}
if (templateResponse.getVersion() != null) {
this.version = new Version(templateResponse.getVersion());
}
this.etag = templateResponse.getEtag();
}
/**
* Creates and returns a new Remote Config template from a JSON string.
* Input JSON string must contain an {@code etag} property to create a valid template.
*
* @param json A non-null JSON string to populate a Remote Config template.
* @return A new {@link Template} instance.
* @throws FirebaseRemoteConfigException If the input JSON string is not parsable.
*/
public static Template fromJSON(@NonNull String json) throws FirebaseRemoteConfigException {
checkArgument(!Strings.isNullOrEmpty(json), "JSON String must not be null or empty.");
// using the default json factory as no rpc calls are made here
JsonFactory jsonFactory = ApiClientUtils.getDefaultJsonFactory();
try {
TemplateResponse templateResponse = jsonFactory.createJsonParser(json)
.parseAndClose(TemplateResponse.class);
return new Template(templateResponse);
} catch (IOException e) {
throw new FirebaseRemoteConfigException(ErrorCode.INVALID_ARGUMENT,
"Unable to parse JSON string.");
}
}
/**
* Gets the ETag of the template.
*
* @return The ETag of the template.
*/
public String getETag() {
return this.etag;
}
/**
* Gets the map of parameters of the template.
*
* @return A non-null map of parameter keys to their optional default values and optional
* conditional values.
*/
@NonNull
public Map<String, Parameter> getParameters() {
return this.parameters;
}
/**
* Gets the list of conditions of the template.
*
* @return A non-null list of conditions.
*/
@NonNull
public List<Condition> getConditions() {
return conditions;
}
/**
* Gets the map of parameter groups of the template.
*
* @return A non-null map of parameter group names to their parameter group instances.
*/
@NonNull
public Map<String, ParameterGroup> getParameterGroups() {
return parameterGroups;
}
/**
* Gets the version information of the template.
*
* @return The version information of the template.
*/
public Version getVersion() {
return version;
}
/**
* Sets the map of parameters of the template.
*
* @param parameters A non-null map of parameter keys to their optional default values and
* optional conditional values.
* @return This {@link Template} instance.
*/
public Template setParameters(
@NonNull Map<String, Parameter> parameters) {
checkNotNull(parameters, "parameters must not be null.");
this.parameters = parameters;
return this;
}
/**
* Sets the list of conditions of the template.
*
* @param conditions A non-null list of conditions in descending order by priority.
* @return This {@link Template} instance.
*/
public Template setConditions(
@NonNull List<Condition> conditions) {
checkNotNull(conditions, "conditions must not be null.");
this.conditions = conditions;
return this;
}
/**
* Sets the map of parameter groups of the template.
*
* @param parameterGroups A non-null map of parameter group names to their
* parameter group instances.
* @return This {@link Template} instance.
*/
public Template setParameterGroups(
@NonNull Map<String, ParameterGroup> parameterGroups) {
checkNotNull(parameterGroups, "parameter groups must not be null.");
this.parameterGroups = parameterGroups;
return this;
}
/**
* Sets the version information of the template.
* Only the version's description field can be specified here.
*
* @param version A {@link Version} instance.
* @return This {@link Template} instance.
*/
public Template setVersion(Version version) {
this.version = version;
return this;
}
/**
* Gets the JSON-serializable representation of this template.
*
* @return A JSON-serializable representation of this {@link Template} instance.
*/
public String toJSON() {
JsonFactory jsonFactory = ApiClientUtils.getDefaultJsonFactory();
try {
return jsonFactory.toString(this.toTemplateResponse(true));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Template setETag(String etag) {
this.etag = etag;
return this;
}
TemplateResponse toTemplateResponse(boolean includeAll) {
Map<String, TemplateResponse.ParameterResponse> parameterResponses = new HashMap<>();
for (Map.Entry<String, Parameter> entry : this.parameters.entrySet()) {
parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse());
}
List<TemplateResponse.ConditionResponse> conditionResponses = new ArrayList<>();
for (Condition condition : this.conditions) {
conditionResponses.add(condition.toConditionResponse());
}
Map<String, TemplateResponse.ParameterGroupResponse> parameterGroupResponse = new HashMap<>();
for (Map.Entry<String, ParameterGroup> entry : this.parameterGroups.entrySet()) {
parameterGroupResponse.put(entry.getKey(), entry.getValue().toParameterGroupResponse());
}
TemplateResponse.VersionResponse versionResponse = (this.version == null) ? null
: this.version.toVersionResponse(includeAll);
TemplateResponse templateResponse = new TemplateResponse()
.setParameters(parameterResponses)
.setConditions(conditionResponses)
.setParameterGroups(parameterGroupResponse)
.setVersion(versionResponse);
if (includeAll) {
return templateResponse.setEtag(this.etag);
}
return templateResponse;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Template template = (Template) o;
return Objects.equals(etag, template.etag)
&& Objects.equals(parameters, template.parameters)
&& Objects.equals(conditions, template.conditions)
&& Objects.equals(parameterGroups, template.parameterGroups)
&& Objects.equals(version, template.version);
}
@Override
public int hashCode() {
return Objects.hash(etag, parameters, conditions, parameterGroups, version);
}
}