Skip to content

Commit e4bde99

Browse files
Add Remote Config conditions to template (#489)
* Add Remote Config conditions to template
1 parent f2c5e81 commit e4bde99

File tree

7 files changed

+371
-4
lines changed

7 files changed

+371
-4
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
package com.google.firebase.remoteconfig;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
22+
import com.google.common.base.Strings;
23+
import com.google.firebase.internal.NonNull;
24+
import com.google.firebase.internal.Nullable;
25+
import com.google.firebase.remoteconfig.internal.TemplateResponse.ConditionResponse;
26+
27+
import java.util.Objects;
28+
29+
/**
30+
* Represents a Remote Config condition that can be included in a {@link Template}.
31+
* A condition targets a specific group of users. A list of these conditions make up
32+
* part of a Remote Config template.
33+
*/
34+
public final class Condition {
35+
36+
private String name;
37+
private String expression;
38+
private TagColor tagColor;
39+
40+
/**
41+
* Creates a new {@link Condition}.
42+
*
43+
* @param name A non-null, non-empty, and unique name of this condition.
44+
* @param expression A non-null and non-empty expression of this condition.
45+
*/
46+
public Condition(@NonNull String name, @NonNull String expression) {
47+
this(name, expression, null);
48+
}
49+
50+
/**
51+
* Creates a new {@link Condition}.
52+
*
53+
* @param name A non-null, non-empty, and unique name of this condition.
54+
* @param expression A non-null and non-empty expression of this condition.
55+
* @param tagColor A color associated with this condition for display purposes in the
56+
* Firebase Console. Not specifying this value results in the console picking an
57+
* arbitrary color to associate with the condition.
58+
*/
59+
public Condition(@NonNull String name, @NonNull String expression, @Nullable TagColor tagColor) {
60+
checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty");
61+
checkArgument(!Strings.isNullOrEmpty(expression),
62+
"condition expression must not be null or empty");
63+
this.name = name;
64+
this.expression = expression;
65+
this.tagColor = tagColor;
66+
}
67+
68+
Condition(@NonNull ConditionResponse conditionResponse) {
69+
checkNotNull(conditionResponse);
70+
this.name = conditionResponse.getName();
71+
this.expression = conditionResponse.getExpression();
72+
if (conditionResponse.getTagColor() == null) {
73+
this.tagColor = TagColor.UNSPECIFIED;
74+
} else {
75+
this.tagColor = TagColor.valueOf(conditionResponse.getTagColor());
76+
}
77+
}
78+
79+
/**
80+
* Gets the name of the condition.
81+
*
82+
* @return The name of the condition.
83+
*/
84+
@NonNull
85+
public String getName() {
86+
return name;
87+
}
88+
89+
/**
90+
* Gets the expression of the condition.
91+
*
92+
* @return The expression of the condition.
93+
*/
94+
@NonNull
95+
public String getExpression() {
96+
return expression;
97+
}
98+
99+
/**
100+
* Gets the tag color of the condition used for display purposes in the Firebase Console.
101+
*
102+
* @return The tag color of the condition.
103+
*/
104+
@NonNull
105+
public TagColor getTagColor() {
106+
return tagColor;
107+
}
108+
109+
/**
110+
* Sets the name of the condition.
111+
*
112+
* @param name A non-empty and unique name of this condition.
113+
* @return This {@link Condition}.
114+
*/
115+
public Condition setName(@NonNull String name) {
116+
checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty");
117+
this.name = name;
118+
return this;
119+
}
120+
121+
/**
122+
* Sets the expression of the condition.
123+
*
124+
* <p>See <a href="https://firebase.google.com/docs/remote-config/condition-reference">
125+
* condition expressions</a> for the expected syntax of this field.
126+
*
127+
* @param expression The logic of this condition.
128+
* @return This {@link Condition}.
129+
*/
130+
public Condition setExpression(@NonNull String expression) {
131+
checkArgument(!Strings.isNullOrEmpty(expression),
132+
"condition expression must not be null or empty");
133+
this.expression = expression;
134+
return this;
135+
}
136+
137+
/**
138+
* Sets the tag color of the condition.
139+
*
140+
* <p>The color associated with this condition for display purposes in the Firebase Console.
141+
* Not specifying this value results in the console picking an arbitrary color to associate
142+
* with the condition.
143+
*
144+
* @param tagColor The tag color of this condition.
145+
* @return This {@link Condition}.
146+
*/
147+
public Condition setTagColor(@Nullable TagColor tagColor) {
148+
this.tagColor = tagColor;
149+
return this;
150+
}
151+
152+
ConditionResponse toConditionResponse() {
153+
return new ConditionResponse()
154+
.setName(this.name)
155+
.setExpression(this.expression)
156+
.setTagColor(this.tagColor == null ? null : this.tagColor.getColor());
157+
}
158+
159+
@Override
160+
public boolean equals(Object o) {
161+
if (this == o) {
162+
return true;
163+
}
164+
if (o == null || getClass() != o.getClass()) {
165+
return false;
166+
}
167+
Condition condition = (Condition) o;
168+
return Objects.equals(name, condition.name)
169+
&& Objects.equals(expression, condition.expression) && tagColor == condition.tagColor;
170+
}
171+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
package com.google.firebase.remoteconfig;
18+
19+
/**
20+
* Colors that are associated with conditions for display purposes in the Firebase Console.
21+
*/
22+
public enum TagColor {
23+
BLUE("BLUE"),
24+
BROWN("BROWN"),
25+
CYAN("CYAN"),
26+
DEEP_ORANGE("DEEP_ORANGE"),
27+
GREEN("GREEN"),
28+
INDIGO("INDIGO"),
29+
LIME("LIME"),
30+
ORANGE("ORANGE"),
31+
PINK("PINK"),
32+
PURPLE("PURPLE"),
33+
TEAL("TEAL"),
34+
UNSPECIFIED("CONDITION_DISPLAY_COLOR_UNSPECIFIED");
35+
36+
private final String color;
37+
38+
TagColor(String color) {
39+
this.color = color;
40+
}
41+
42+
public String getColor() {
43+
return color;
44+
}
45+
}

src/main/java/com/google/firebase/remoteconfig/Template.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.google.firebase.internal.NonNull;
2222
import com.google.firebase.remoteconfig.internal.TemplateResponse;
2323

24+
import java.util.ArrayList;
2425
import java.util.HashMap;
26+
import java.util.List;
2527
import java.util.Map;
2628

2729
/**
@@ -31,23 +33,32 @@ public final class Template {
3133

3234
private String etag;
3335
private Map<String, Parameter> parameters;
36+
private List<Condition> conditions;
3437

3538
/**
3639
* Creates a new {@link Template}.
3740
*/
3841
public Template() {
3942
parameters = new HashMap<>();
43+
conditions = new ArrayList<>();
4044
}
4145

4246
Template(@NonNull TemplateResponse templateResponse) {
4347
checkNotNull(templateResponse);
4448
this.parameters = new HashMap<>();
49+
this.conditions = new ArrayList<>();
4550
if (templateResponse.getParameters() != null) {
4651
for (Map.Entry<String, TemplateResponse.ParameterResponse> entry
4752
: templateResponse.getParameters().entrySet()) {
4853
this.parameters.put(entry.getKey(), new Parameter(entry.getValue()));
4954
}
5055
}
56+
if (templateResponse.getConditions() != null) {
57+
for (TemplateResponse.ConditionResponse conditionResponse
58+
: templateResponse.getConditions()) {
59+
this.conditions.add(new Condition(conditionResponse));
60+
}
61+
}
5162
}
5263

5364
/**
@@ -70,6 +81,16 @@ public Map<String, Parameter> getParameters() {
7081
return this.parameters;
7182
}
7283

84+
/**
85+
* Gets the list of conditions of the template.
86+
*
87+
* @return A non-null list of conditions
88+
*/
89+
@NonNull
90+
public List<Condition> getConditions() {
91+
return conditions;
92+
}
93+
7394
/**
7495
* Sets the map of parameters of the template.
7596
*
@@ -84,16 +105,35 @@ public Template setParameters(
84105
return this;
85106
}
86107

108+
/**
109+
* Sets the list of conditions of the template.
110+
*
111+
* @param conditions A non-null list of conditions in descending order by priority.
112+
* @return This {@link Template} instance.
113+
*/
114+
public Template setConditions(
115+
@NonNull List<Condition> conditions) {
116+
checkNotNull(conditions, "conditions must not be null.");
117+
this.conditions = conditions;
118+
return this;
119+
}
120+
87121
Template setETag(String etag) {
88122
this.etag = etag;
89123
return this;
90124
}
91125

92126
TemplateResponse toTemplateResponse() {
93127
Map<String, TemplateResponse.ParameterResponse> parameterResponses = new HashMap<>();
94-
for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
128+
for (Map.Entry<String, Parameter> entry : this.parameters.entrySet()) {
95129
parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse());
96130
}
97-
return new TemplateResponse().setParameters(parameterResponses);
131+
List<TemplateResponse.ConditionResponse> conditionResponses = new ArrayList<>();
132+
for (Condition condition : this.conditions) {
133+
conditionResponses.add(condition.toConditionResponse());
134+
}
135+
return new TemplateResponse()
136+
.setParameters(parameterResponses)
137+
.setConditions(conditionResponses);
98138
}
99139
}

0 commit comments

Comments
 (0)