Skip to content

Commit 01d0c98

Browse files
Remote Config Refactor Public and Response types (#481)
* Refactor Public and Response types * reformat code * Clean up code style * Remove getUseInAppDefault and use the value type in setter
1 parent 150b121 commit 01d0c98

File tree

5 files changed

+88
-66
lines changed

5 files changed

+88
-66
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException {
9696
.addAllHeaders(COMMON_HEADERS);
9797
IncomingHttpResponse response = httpClient.send(request);
9898
TemplateResponse templateResponse = httpClient.parse(response, TemplateResponse.class);
99-
RemoteConfigTemplate template = templateResponse.toRemoteConfigTemplate();
99+
RemoteConfigTemplate template = new RemoteConfigTemplate(templateResponse);
100100
return template.setETag(getETag(response));
101101
}
102102

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.firebase.remoteconfig;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
2019
import static com.google.common.base.Preconditions.checkNotNull;
2120

2221
import com.google.firebase.internal.NonNull;
@@ -45,6 +44,22 @@ public RemoteConfigParameter() {
4544
conditionalValues = new HashMap<>();
4645
}
4746

47+
RemoteConfigParameter(@NonNull ParameterResponse parameterResponse) {
48+
checkNotNull(parameterResponse);
49+
this.conditionalValues = new HashMap<>();
50+
if (parameterResponse.getConditionalValues() != null) {
51+
for (Map.Entry<String, ParameterValueResponse> entry
52+
: parameterResponse.getConditionalValues().entrySet()) {
53+
this.conditionalValues.put(entry.getKey(),
54+
RemoteConfigParameterValue.fromParameterValueResponse(entry.getValue()));
55+
}
56+
}
57+
ParameterValueResponse responseDefaultValue = parameterResponse.getDefaultValue();
58+
this.defaultValue = (responseDefaultValue == null) ? null
59+
: RemoteConfigParameterValue.fromParameterValueResponse(responseDefaultValue);
60+
this.description = parameterResponse.getDescription();
61+
}
62+
4863
/**
4964
* Gets the default value of the parameter.
5065
*
@@ -122,9 +137,11 @@ ParameterResponse toParameterResponse() {
122137
for (Map.Entry<String, RemoteConfigParameterValue> entry : conditionalValues.entrySet()) {
123138
conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse());
124139
}
125-
ParameterValueResponse parameterValueResponse = (defaultValue == null) ? null : defaultValue
126-
.toParameterValueResponse();
127-
return new ParameterResponse(parameterValueResponse, description,
128-
conditionalResponseValues);
140+
ParameterValueResponse defaultValueResponse = (defaultValue == null) ? null
141+
: defaultValue.toParameterValueResponse();
142+
return new ParameterResponse()
143+
.setDefaultValue(defaultValueResponse)
144+
.setDescription(description)
145+
.setConditionalValues(conditionalResponseValues);
129146
}
130147
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.firebase.remoteconfig;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.firebase.internal.NonNull;
1922
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse;
2023

2124
/**
@@ -44,6 +47,15 @@ public static InAppDefault inAppDefault() {
4447

4548
abstract ParameterValueResponse toParameterValueResponse();
4649

50+
static RemoteConfigParameterValue fromParameterValueResponse(
51+
@NonNull ParameterValueResponse parameterValueResponse) {
52+
checkNotNull(parameterValueResponse);
53+
if (parameterValueResponse.isUseInAppDefault()) {
54+
return RemoteConfigParameterValue.inAppDefault();
55+
}
56+
return RemoteConfigParameterValue.of(parameterValueResponse.getValue());
57+
}
58+
4759
/**
4860
* Represents an explicit Remote Config parameter value with a {@link String} value that the
4961
* parameter is set to.
@@ -67,7 +79,8 @@ public String getValue() {
6779

6880
@Override
6981
ParameterValueResponse toParameterValueResponse() {
70-
return ParameterValueResponse.ofValue(this.value);
82+
return new ParameterValueResponse()
83+
.setValue(this.value);
7184
}
7285
}
7386

@@ -78,7 +91,7 @@ public static final class InAppDefault extends RemoteConfigParameterValue {
7891

7992
@Override
8093
ParameterValueResponse toParameterValueResponse() {
81-
return ParameterValueResponse.ofInAppDefaultValue();
94+
return new ParameterValueResponse().setUseInAppDefault(true);
8295
}
8396
}
8497
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public RemoteConfigTemplate() {
3939
parameters = new HashMap<>();
4040
}
4141

42+
RemoteConfigTemplate(@NonNull TemplateResponse templateResponse) {
43+
checkNotNull(templateResponse);
44+
this.parameters = new HashMap<>();
45+
if (templateResponse.getParameters() != null) {
46+
for (Map.Entry<String, TemplateResponse.ParameterResponse> entry
47+
: templateResponse.getParameters().entrySet()) {
48+
this.parameters.put(entry.getKey(), new RemoteConfigParameter(entry.getValue()));
49+
}
50+
}
51+
}
52+
4253
/**
4354
* Gets the ETag of the template.
4455
*
@@ -83,6 +94,6 @@ TemplateResponse toTemplateResponse() {
8394
for (Map.Entry<String, RemoteConfigParameter> entry : parameters.entrySet()) {
8495
parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse());
8596
}
86-
return new TemplateResponse(parameterResponses);
97+
return new TemplateResponse().setParameters(parameterResponses);
8798
}
8899
}

src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@
1616

1717
package com.google.firebase.remoteconfig.internal;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
20-
import static com.google.common.base.Preconditions.checkNotNull;
21-
2219
import com.google.api.client.util.Key;
23-
import com.google.firebase.database.annotations.Nullable;
24-
import com.google.firebase.internal.NonNull;
25-
import com.google.firebase.remoteconfig.RemoteConfigParameter;
26-
import com.google.firebase.remoteconfig.RemoteConfigParameterValue;
27-
import com.google.firebase.remoteconfig.RemoteConfigTemplate;
28-
29-
import java.util.Collections;
30-
import java.util.HashMap;
20+
3121
import java.util.Map;
3222

3323
/**
@@ -39,21 +29,14 @@ public final class TemplateResponse {
3929
@Key("parameters")
4030
private Map<String, ParameterResponse> parameters;
4131

42-
public TemplateResponse() {
43-
parameters = Collections.emptyMap();
32+
public Map<String, ParameterResponse> getParameters() {
33+
return parameters;
4434
}
4535

46-
public TemplateResponse(@NonNull Map<String, ParameterResponse> parameters) {
47-
checkNotNull(parameters, "parameters must not be null.");
36+
public TemplateResponse setParameters(
37+
Map<String, ParameterResponse> parameters) {
4838
this.parameters = parameters;
49-
}
50-
51-
public RemoteConfigTemplate toRemoteConfigTemplate() {
52-
Map<String, RemoteConfigParameter> parameterPublicTypes = new HashMap<>();
53-
for (Map.Entry<String, ParameterResponse> entry : parameters.entrySet()) {
54-
parameterPublicTypes.put(entry.getKey(), entry.getValue().toRemoteConfigParameter());
55-
}
56-
return new RemoteConfigTemplate().setParameters(parameterPublicTypes);
39+
return this;
5740
}
5841

5942
/**
@@ -71,30 +54,33 @@ public static final class ParameterResponse {
7154
@Key("conditionalValues")
7255
private Map<String, ParameterValueResponse> conditionalValues;
7356

74-
public ParameterResponse() {
75-
conditionalValues = Collections.emptyMap();
57+
public ParameterValueResponse getDefaultValue() {
58+
return defaultValue;
59+
}
60+
61+
public String getDescription() {
62+
return description;
7663
}
7764

78-
public ParameterResponse(@Nullable ParameterValueResponse defaultValue,
79-
@Nullable String description,
80-
@NonNull Map<String, ParameterValueResponse> conditionalValues) {
65+
public Map<String, ParameterValueResponse> getConditionalValues() {
66+
return conditionalValues;
67+
}
68+
69+
public ParameterResponse setDefaultValue(
70+
ParameterValueResponse defaultValue) {
8171
this.defaultValue = defaultValue;
72+
return this;
73+
}
74+
75+
public ParameterResponse setDescription(String description) {
8276
this.description = description;
83-
this.conditionalValues = checkNotNull(conditionalValues);
77+
return this;
8478
}
8579

86-
public RemoteConfigParameter toRemoteConfigParameter() {
87-
Map<String, RemoteConfigParameterValue> conditionalPublicValues = new HashMap<>();
88-
for (Map.Entry<String, ParameterValueResponse> entry : conditionalValues.entrySet()) {
89-
conditionalPublicValues
90-
.put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue());
91-
}
92-
RemoteConfigParameterValue remoteConfigParameterValue =
93-
(defaultValue == null) ? null : defaultValue.toRemoteConfigParameterValue();
94-
return new RemoteConfigParameter()
95-
.setDefaultValue(remoteConfigParameterValue)
96-
.setDescription(description)
97-
.setConditionalValues(conditionalPublicValues);
80+
public ParameterResponse setConditionalValues(
81+
Map<String, ParameterValueResponse> conditionalValues) {
82+
this.conditionalValues = conditionalValues;
83+
return this;
9884
}
9985
}
10086

@@ -108,29 +94,24 @@ public static final class ParameterValueResponse {
10894
private String value;
10995

11096
@Key("useInAppDefault")
111-
private Boolean inAppDefaultValue;
97+
private Boolean useInAppDefault;
11298

113-
public ParameterValueResponse() {
99+
public String getValue() {
100+
return value;
114101
}
115102

116-
private ParameterValueResponse(String value, Boolean inAppDefaultValue) {
117-
this.value = value;
118-
this.inAppDefaultValue = inAppDefaultValue;
103+
public boolean isUseInAppDefault() {
104+
return Boolean.TRUE.equals(this.useInAppDefault);
119105
}
120106

121-
public static ParameterValueResponse ofValue(String value) {
122-
return new ParameterValueResponse(value, null);
123-
}
124-
125-
public static ParameterValueResponse ofInAppDefaultValue() {
126-
return new ParameterValueResponse(null, true);
107+
public ParameterValueResponse setValue(String value) {
108+
this.value = value;
109+
return this;
127110
}
128111

129-
public RemoteConfigParameterValue toRemoteConfigParameterValue() {
130-
if (this.inAppDefaultValue != null && this.inAppDefaultValue) {
131-
return RemoteConfigParameterValue.inAppDefault();
132-
}
133-
return RemoteConfigParameterValue.of(this.value);
112+
public ParameterValueResponse setUseInAppDefault(boolean useInAppDefault) {
113+
this.useInAppDefault = useInAppDefault;
114+
return this;
134115
}
135116
}
136117
}

0 commit comments

Comments
 (0)