From fa23a4dd9eb52da53d08301dca42f7e2124f083d Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 24 Sep 2020 12:26:10 -0400 Subject: [PATCH 01/11] Add parameters to template --- .../remoteconfig/ExplicitParameterValue.java | 41 +++++++++++ .../remoteconfig/InAppDefaultValue.java | 37 ++++++++++ .../remoteconfig/RemoteConfigParameter.java | 71 +++++++++++++++++++ .../RemoteConfigParameterValue.java | 23 ++++++ .../remoteconfig/RemoteConfigTemplate.java | 21 +++++- .../internal/ParameterValueResponse.java | 54 ++++++++++++++ .../FirebaseRemoteConfigClientImplTest.java | 33 ++++++++- 7 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java diff --git a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java new file mode 100644 index 000000000..2f95c8d60 --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java @@ -0,0 +1,41 @@ +/* + * 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 com.google.firebase.remoteconfig.internal.ParameterValueResponse; + +public final class ExplicitParameterValue extends RemoteConfigParameterValue { + + private final String value; + + private ExplicitParameterValue(String value) { + this.value = value; + } + + public static ExplicitParameterValue of(String value) { + return new ExplicitParameterValue(value); + } + + public String getValue() { + return this.value; + } + + @Override + public ParameterValueResponse toResponse() { + return ParameterValueResponse.ofValue(this.value); + } +} diff --git a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java new file mode 100644 index 000000000..5998f0b8c --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java @@ -0,0 +1,37 @@ +/* + * 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 com.google.firebase.remoteconfig.internal.ParameterValueResponse; + +public final class InAppDefaultValue extends RemoteConfigParameterValue { + + private final boolean inAppDefaultValue; + + private InAppDefaultValue(boolean inAppDefaultValue) { + this.inAppDefaultValue = inAppDefaultValue; + } + + public static InAppDefaultValue getInstance() { + return new InAppDefaultValue(true); + } + + @Override + public ParameterValueResponse toResponse() { + return ParameterValueResponse.ofInAppDefaultValue(); + } +} diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java new file mode 100644 index 000000000..e509a192b --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -0,0 +1,71 @@ +/* + * 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 com.google.api.client.util.Key; +import com.google.firebase.remoteconfig.internal.ParameterValueResponse; + +import java.util.HashMap; +import java.util.Map; + +public final class RemoteConfigParameter { + + @Key("defaultValue") + private ParameterValueResponse defaultValue; + + @Key("description") + private String description; + + @Key("conditionalValues") + private Map conditionalValues; + + public RemoteConfigParameterValue getDefaultValue() { + return defaultValue.toValue(); + } + + public String getDescription() { + return this.description; + } + + public Map getConditionalValues() { + Map convertedMap = new HashMap<>(); + for (Map.Entry entry : this.conditionalValues.entrySet()) { + convertedMap.put(entry.getKey(), entry.getValue().toValue()); + } + return convertedMap; + } + + public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { + this.defaultValue = value.toResponse(); + return this; + } + + public RemoteConfigParameter setDescription(String description) { + this.description = description; + return this; + } + + public RemoteConfigParameter setConditionalValues( + Map conditionalValues) { + Map convertedMap = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + convertedMap.put(entry.getKey(), entry.getValue().toResponse()); + } + this.conditionalValues = convertedMap; + return this; + } +} diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java new file mode 100644 index 000000000..67bfa40f5 --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java @@ -0,0 +1,23 @@ +/* + * 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 com.google.firebase.remoteconfig.internal.ParameterValueResponse; + +public abstract class RemoteConfigParameterValue { + public abstract ParameterValueResponse toResponse(); +} diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java index 632fb7777..4bff9456e 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java @@ -17,18 +17,35 @@ package com.google.firebase.remoteconfig; import com.google.api.client.util.Key; +import com.google.common.collect.ImmutableMap; +import com.google.firebase.database.util.JsonMapper; +import com.google.firebase.internal.HttpRequestInfo; + +import java.io.IOException; +import java.util.Map; public final class RemoteConfigTemplate { - @Key("etag") private String etag; + @Key("parameters") + private Map parameters; + public String getETag() { return this.etag; } - RemoteConfigTemplate setETag(String etag) { + public Map getParameters() { + return this.parameters; + } + + public RemoteConfigTemplate setETag(String etag) { this.etag = etag; return this; } + + public RemoteConfigTemplate setParameters(Map parameters) { + this.parameters = parameters; + return this; + } } diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java new file mode 100644 index 000000000..ff05b6f44 --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java @@ -0,0 +1,54 @@ +/* + * 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.internal; + +import com.google.api.client.util.Key; +import com.google.firebase.remoteconfig.ExplicitParameterValue; +import com.google.firebase.remoteconfig.InAppDefaultValue; +import com.google.firebase.remoteconfig.RemoteConfigParameterValue; + +public final class ParameterValueResponse { + + @Key("value") + private String value; + + @Key("useInAppDefault") + private Boolean inAppDefaultValue; + + public ParameterValueResponse() { + } + + private ParameterValueResponse(String value, Boolean inAppDefaultValue) { + this.value = value; + this.inAppDefaultValue = inAppDefaultValue; + } + + public static ParameterValueResponse ofValue(String value) { + return new ParameterValueResponse(value, null); + } + + public static ParameterValueResponse ofInAppDefaultValue() { + return new ParameterValueResponse(null, true); + } + + public RemoteConfigParameterValue toValue() { + if (this.inAppDefaultValue) { + return InAppDefaultValue.getInstance(); + } + return ExplicitParameterValue.of(this.value); + } +} diff --git a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java index 8b6e94ce1..e672610e0 100644 --- a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java +++ b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java @@ -45,6 +45,7 @@ import com.google.firebase.testing.TestUtils; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -63,7 +64,33 @@ public class FirebaseRemoteConfigClientImplTest { 404, ErrorCode.NOT_FOUND, 500, ErrorCode.INTERNAL); - private static final String MOCK_TEMPLATE_RESPONSE = "{\"conditions\": [], \"parameters\": {}}"; + private static final String MOCK_TEMPLATE_RESPONSE = "{" + + " \"conditions\": [\n" + + " {\n" + + " \"name\": \"ios_en\",\n" + + " \"expression\": \"device.os == 'ios' && device.country in ['us', 'uk']\",\n" + + " \"tagColor\": \"INDIGO\"\n" + + " }\n" + + " ],\n" + + " \"parameters\": {\n" + + " \"welcome_message_text\": {\n" + + " \"defaultValue\": {\n" + + " \"value\": \"welcome to app\"\n" + + " },\n" + + " \"conditionalValues\": {\n" + + " \"ios_en\": {\n" + + " \"value\": \"welcome to app en\"\n" + + " }\n" + + " },\n" + + " \"description\": \"Text for welcome message!\"\n" + + " },\n" + + " \"header_text\": {\n" + + " \"defaultValue\": {\n" + + " \"useInAppDefault\": true\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; private static final String TEST_ETAG = "etag-123456789012-1"; @@ -86,6 +113,10 @@ public void testGetTemplate() throws Exception { RemoteConfigTemplate template = client.getTemplate(); assertEquals(TEST_ETAG, template.getETag()); + Map parameters = template.getParameters(); + assertEquals(2, parameters.size()); + assertEquals(true, parameters.containsKey("welcome_message_text")); + assertEquals(true, parameters.containsKey("header_text")); checkGetRequestHeader(interceptor.getLastRequest()); } From 83c69db20943d3a9a121b10121de8aded3d1c06f Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Mon, 28 Sep 2020 16:12:38 -0400 Subject: [PATCH 02/11] Fix no op in conditional values --- .../remoteconfig/ExplicitParameterValue.java | 2 +- .../remoteconfig/InAppDefaultValue.java | 2 +- .../remoteconfig/RemoteConfigParameter.java | 38 +++++++++++++------ .../RemoteConfigParameterValue.java | 2 +- .../remoteconfig/RemoteConfigTemplate.java | 16 ++++++-- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java index 2f95c8d60..30d8896f1 100644 --- a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java @@ -35,7 +35,7 @@ public String getValue() { } @Override - public ParameterValueResponse toResponse() { + ParameterValueResponse toResponse() { return ParameterValueResponse.ofValue(this.value); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java index 5998f0b8c..e7985a957 100644 --- a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java @@ -31,7 +31,7 @@ public static InAppDefaultValue getInstance() { } @Override - public ParameterValueResponse toResponse() { + ParameterValueResponse toResponse() { return ParameterValueResponse.ofInAppDefaultValue(); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index e509a192b..53ddd490f 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -31,26 +31,36 @@ public final class RemoteConfigParameter { private String description; @Key("conditionalValues") - private Map conditionalValues; + private Map conditionalResponseValues; + + private Map conditionalValues; + + public RemoteConfigParameter() { + conditionalResponseValues = new HashMap<>(); + conditionalValues = null; + } public RemoteConfigParameterValue getDefaultValue() { return defaultValue.toValue(); } public String getDescription() { - return this.description; + return description; } public Map getConditionalValues() { - Map convertedMap = new HashMap<>(); - for (Map.Entry entry : this.conditionalValues.entrySet()) { - convertedMap.put(entry.getKey(), entry.getValue().toValue()); + if (conditionalValues != null) { + return conditionalValues; } - return convertedMap; + conditionalValues = new HashMap<>(); + for (Map.Entry entry : conditionalResponseValues.entrySet()) { + conditionalValues.put(entry.getKey(), entry.getValue().toValue()); + } + return conditionalValues; } public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { - this.defaultValue = value.toResponse(); + defaultValue = value.toResponse(); return this; } @@ -61,11 +71,17 @@ public RemoteConfigParameter setDescription(String description) { public RemoteConfigParameter setConditionalValues( Map conditionalValues) { - Map convertedMap = new HashMap<>(); + this.conditionalValues = conditionalValues; + return this; + } + + void wrapForTransport() { + conditionalResponseValues.clear(); + if (conditionalValues == null) { + return; + } for (Map.Entry entry : conditionalValues.entrySet()) { - convertedMap.put(entry.getKey(), entry.getValue().toResponse()); + conditionalResponseValues.put(entry.getKey(), entry.getValue().toResponse()); } - this.conditionalValues = convertedMap; - return this; } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java index 67bfa40f5..ba3b086c6 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java @@ -19,5 +19,5 @@ import com.google.firebase.remoteconfig.internal.ParameterValueResponse; public abstract class RemoteConfigParameterValue { - public abstract ParameterValueResponse toResponse(); + abstract ParameterValueResponse toResponse(); } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java index 4bff9456e..b81c0e8a6 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java @@ -17,11 +17,8 @@ package com.google.firebase.remoteconfig; import com.google.api.client.util.Key; -import com.google.common.collect.ImmutableMap; -import com.google.firebase.database.util.JsonMapper; -import com.google.firebase.internal.HttpRequestInfo; -import java.io.IOException; +import java.util.HashMap; import java.util.Map; public final class RemoteConfigTemplate { @@ -31,6 +28,10 @@ public final class RemoteConfigTemplate { @Key("parameters") private Map parameters; + public RemoteConfigTemplate() { + parameters = new HashMap<>(); + } + public String getETag() { return this.etag; } @@ -48,4 +49,11 @@ public RemoteConfigTemplate setParameters(Map par this.parameters = parameters; return this; } + + RemoteConfigTemplate wrapForTransport() { + for (Map.Entry entry : parameters.entrySet()) { + entry.getValue().wrapForTransport(); + } + return this; + } } From c001c94d0f451d8e0e7e02517689ec1e225fe206 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Mon, 28 Sep 2020 17:31:34 -0400 Subject: [PATCH 03/11] Fix empty conditional values --- .../firebase/remoteconfig/RemoteConfigParameter.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index 53ddd490f..6a99d820e 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -34,10 +34,12 @@ public final class RemoteConfigParameter { private Map conditionalResponseValues; private Map conditionalValues; + private boolean conditionalValuesModified; public RemoteConfigParameter() { conditionalResponseValues = new HashMap<>(); conditionalValues = null; + conditionalValuesModified = false; } public RemoteConfigParameterValue getDefaultValue() { @@ -52,6 +54,7 @@ public Map getConditionalValues() { if (conditionalValues != null) { return conditionalValues; } + conditionalValuesModified = true; conditionalValues = new HashMap<>(); for (Map.Entry entry : conditionalResponseValues.entrySet()) { conditionalValues.put(entry.getKey(), entry.getValue().toValue()); @@ -72,10 +75,14 @@ public RemoteConfigParameter setDescription(String description) { public RemoteConfigParameter setConditionalValues( Map conditionalValues) { this.conditionalValues = conditionalValues; + conditionalValuesModified = true; return this; } void wrapForTransport() { + if (!conditionalValuesModified) { + return; + } conditionalResponseValues.clear(); if (conditionalValues == null) { return; From 625f514beb0d09b6a6d333ab1484726204a8f7cd Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Tue, 29 Sep 2020 13:48:27 -0400 Subject: [PATCH 04/11] Introduce public and response types --- .../remoteconfig/ExplicitParameterValue.java | 4 +- .../FirebaseRemoteConfigClientImpl.java | 7 +- .../remoteconfig/InAppDefaultValue.java | 4 +- .../remoteconfig/RemoteConfigParameter.java | 45 ++----- .../RemoteConfigParameterValue.java | 4 +- .../remoteconfig/RemoteConfigTemplate.java | 11 +- .../internal/ParameterValueResponse.java | 54 --------- .../internal/TemplateResponse.java | 114 ++++++++++++++++++ .../remoteconfig/internal/package-info.java | 20 +++ .../FirebaseRemoteConfigClientImplTest.java | 21 +++- 10 files changed, 179 insertions(+), 105 deletions(-) delete mode 100644 src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java create mode 100644 src/main/java/com/google/firebase/remoteconfig/internal/package-info.java diff --git a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java index 30d8896f1..7803999d2 100644 --- a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java @@ -16,7 +16,7 @@ package com.google.firebase.remoteconfig; -import com.google.firebase.remoteconfig.internal.ParameterValueResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; public final class ExplicitParameterValue extends RemoteConfigParameterValue { @@ -35,7 +35,7 @@ public String getValue() { } @Override - ParameterValueResponse toResponse() { + ParameterValueResponse toResponseType() { return ParameterValueResponse.ofValue(this.value); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java index 20b5e3a61..7f9039993 100644 --- a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java +++ b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java @@ -20,6 +20,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import com.google.api.client.http.HttpMethods; import com.google.api.client.http.HttpRequestFactory; import com.google.api.client.http.HttpResponseInterceptor; import com.google.api.client.json.JsonFactory; @@ -36,6 +37,7 @@ import com.google.firebase.internal.HttpRequestInfo; import com.google.firebase.internal.SdkUtils; import com.google.firebase.remoteconfig.internal.RemoteConfigServiceErrorResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse; import java.io.IOException; import java.util.List; @@ -94,8 +96,9 @@ public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException { HttpRequestInfo request = HttpRequestInfo.buildGetRequest(remoteConfigUrl) .addAllHeaders(COMMON_HEADERS); IncomingHttpResponse response = httpClient.send(request); - RemoteConfigTemplate parsed = httpClient.parse(response, RemoteConfigTemplate.class); - return parsed.setETag(getETag(response)); + TemplateResponse templateResponse = httpClient.parse(response, TemplateResponse.class); + RemoteConfigTemplate template = templateResponse.toPublicType(); + return template.setETag(getETag(response)); } private String getETag(IncomingHttpResponse response) { diff --git a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java index e7985a957..d2fa8be26 100644 --- a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java @@ -16,7 +16,7 @@ package com.google.firebase.remoteconfig; -import com.google.firebase.remoteconfig.internal.ParameterValueResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; public final class InAppDefaultValue extends RemoteConfigParameterValue { @@ -31,7 +31,7 @@ public static InAppDefaultValue getInstance() { } @Override - ParameterValueResponse toResponse() { + ParameterValueResponse toResponseType() { return ParameterValueResponse.ofInAppDefaultValue(); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index 6a99d820e..dccad56f0 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -16,34 +16,24 @@ package com.google.firebase.remoteconfig; -import com.google.api.client.util.Key; -import com.google.firebase.remoteconfig.internal.ParameterValueResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; import java.util.HashMap; import java.util.Map; public final class RemoteConfigParameter { - @Key("defaultValue") - private ParameterValueResponse defaultValue; - - @Key("description") + private RemoteConfigParameterValue defaultValue; private String description; - - @Key("conditionalValues") - private Map conditionalResponseValues; - private Map conditionalValues; - private boolean conditionalValuesModified; public RemoteConfigParameter() { - conditionalResponseValues = new HashMap<>(); - conditionalValues = null; - conditionalValuesModified = false; + conditionalValues = new HashMap<>(); } public RemoteConfigParameterValue getDefaultValue() { - return defaultValue.toValue(); + return defaultValue; } public String getDescription() { @@ -51,19 +41,11 @@ public String getDescription() { } public Map getConditionalValues() { - if (conditionalValues != null) { - return conditionalValues; - } - conditionalValuesModified = true; - conditionalValues = new HashMap<>(); - for (Map.Entry entry : conditionalResponseValues.entrySet()) { - conditionalValues.put(entry.getKey(), entry.getValue().toValue()); - } return conditionalValues; } public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { - defaultValue = value.toResponse(); + defaultValue = value; return this; } @@ -75,20 +57,15 @@ public RemoteConfigParameter setDescription(String description) { public RemoteConfigParameter setConditionalValues( Map conditionalValues) { this.conditionalValues = conditionalValues; - conditionalValuesModified = true; return this; } - void wrapForTransport() { - if (!conditionalValuesModified) { - return; - } - conditionalResponseValues.clear(); - if (conditionalValues == null) { - return; - } + ParameterResponse toResponseType() { + Map conditionalResponseValues = new HashMap<>(); for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalResponseValues.put(entry.getKey(), entry.getValue().toResponse()); + conditionalResponseValues.put(entry.getKey(), entry.getValue().toResponseType()); } + return new ParameterResponse(this.defaultValue.toResponseType(), this.description, + conditionalResponseValues); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java index ba3b086c6..342667497 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java @@ -16,8 +16,8 @@ package com.google.firebase.remoteconfig; -import com.google.firebase.remoteconfig.internal.ParameterValueResponse; +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; public abstract class RemoteConfigParameterValue { - abstract ParameterValueResponse toResponse(); + abstract ParameterValueResponse toResponseType(); } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java index b81c0e8a6..3e46293c7 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java @@ -16,7 +16,7 @@ package com.google.firebase.remoteconfig; -import com.google.api.client.util.Key; +import com.google.firebase.remoteconfig.internal.TemplateResponse; import java.util.HashMap; import java.util.Map; @@ -24,8 +24,6 @@ public final class RemoteConfigTemplate { private String etag; - - @Key("parameters") private Map parameters; public RemoteConfigTemplate() { @@ -50,10 +48,11 @@ public RemoteConfigTemplate setParameters(Map par return this; } - RemoteConfigTemplate wrapForTransport() { + TemplateResponse toResponseType() { + Map parameterResponses = new HashMap<>(); for (Map.Entry entry : parameters.entrySet()) { - entry.getValue().wrapForTransport(); + parameterResponses.put(entry.getKey(), entry.getValue().toResponseType()); } - return this; + return new TemplateResponse(parameterResponses); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java deleted file mode 100644 index ff05b6f44..000000000 --- a/src/main/java/com/google/firebase/remoteconfig/internal/ParameterValueResponse.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.internal; - -import com.google.api.client.util.Key; -import com.google.firebase.remoteconfig.ExplicitParameterValue; -import com.google.firebase.remoteconfig.InAppDefaultValue; -import com.google.firebase.remoteconfig.RemoteConfigParameterValue; - -public final class ParameterValueResponse { - - @Key("value") - private String value; - - @Key("useInAppDefault") - private Boolean inAppDefaultValue; - - public ParameterValueResponse() { - } - - private ParameterValueResponse(String value, Boolean inAppDefaultValue) { - this.value = value; - this.inAppDefaultValue = inAppDefaultValue; - } - - public static ParameterValueResponse ofValue(String value) { - return new ParameterValueResponse(value, null); - } - - public static ParameterValueResponse ofInAppDefaultValue() { - return new ParameterValueResponse(null, true); - } - - public RemoteConfigParameterValue toValue() { - if (this.inAppDefaultValue) { - return InAppDefaultValue.getInstance(); - } - return ExplicitParameterValue.of(this.value); - } -} diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java new file mode 100644 index 000000000..2c422b87e --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -0,0 +1,114 @@ +/* + * 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.internal; + +import com.google.api.client.util.Key; +import com.google.firebase.remoteconfig.ExplicitParameterValue; +import com.google.firebase.remoteconfig.InAppDefaultValue; +import com.google.firebase.remoteconfig.RemoteConfigParameter; +import com.google.firebase.remoteconfig.RemoteConfigParameterValue; +import com.google.firebase.remoteconfig.RemoteConfigTemplate; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public final class TemplateResponse { + + @Key("parameters") + private Map parameters; + + public TemplateResponse() { + parameters = Collections.emptyMap(); + } + + public TemplateResponse(Map parameters) { + this.parameters = parameters; + } + + public RemoteConfigTemplate toPublicType() { + Map parameterPublicTypes = new HashMap<>(); + for (Map.Entry entry : parameters.entrySet()) { + parameterPublicTypes.put(entry.getKey(), entry.getValue().toPublicType()); + } + return new RemoteConfigTemplate().setParameters(parameterPublicTypes); + } + + public static final class ParameterResponse { + + @Key("defaultValue") + private ParameterValueResponse defaultValue; + + @Key("description") + private String description; + + @Key("conditionalValues") + private Map conditionalValues; + + public ParameterResponse() { + conditionalValues = Collections.emptyMap(); + } + + public ParameterResponse(ParameterValueResponse defaultValue, String description, + Map conditionalValues) { + this.defaultValue = defaultValue; + this.description = description; + this.conditionalValues = conditionalValues; + } + + public RemoteConfigParameter toPublicType() { + Map conditionalPublicValues = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + conditionalPublicValues.put(entry.getKey(), entry.getValue().toPublicType()); + } + return new RemoteConfigParameter().setDefaultValue(defaultValue.toPublicType()) + .setDescription(description).setConditionalValues(conditionalPublicValues); + } + } + + public static final class ParameterValueResponse { + + @Key("value") + private String value; + + @Key("useInAppDefault") + private Boolean inAppDefaultValue; + + public ParameterValueResponse() { + } + + private ParameterValueResponse(String value, Boolean inAppDefaultValue) { + this.value = value; + this.inAppDefaultValue = inAppDefaultValue; + } + + public static ParameterValueResponse ofValue(String value) { + return new ParameterValueResponse(value, null); + } + + public static ParameterValueResponse ofInAppDefaultValue() { + return new ParameterValueResponse(null, true); + } + + public RemoteConfigParameterValue toPublicType() { + if (this.inAppDefaultValue != null && this.inAppDefaultValue) { + return InAppDefaultValue.getInstance(); + } + return ExplicitParameterValue.of(this.value); + } + } +} diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/package-info.java b/src/main/java/com/google/firebase/remoteconfig/internal/package-info.java new file mode 100644 index 000000000..50a2768e9 --- /dev/null +++ b/src/main/java/com/google/firebase/remoteconfig/internal/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * @hide + */ +package com.google.firebase.remoteconfig.internal; diff --git a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java index e672610e0..5c8881034 100644 --- a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java +++ b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java @@ -45,7 +45,6 @@ import com.google.firebase.testing.TestUtils; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -115,8 +114,24 @@ public void testGetTemplate() throws Exception { assertEquals(TEST_ETAG, template.getETag()); Map parameters = template.getParameters(); assertEquals(2, parameters.size()); - assertEquals(true, parameters.containsKey("welcome_message_text")); - assertEquals(true, parameters.containsKey("header_text")); + assertTrue(parameters.containsKey("welcome_message_text")); + assertTrue(parameters.containsKey("header_text")); + Map conditionalValues = parameters + .get("welcome_message_text").getConditionalValues(); + assertEquals(1, conditionalValues.size()); + assertTrue(conditionalValues.containsKey("ios_en")); + ExplicitParameterValue value = (ExplicitParameterValue) conditionalValues.get("ios_en"); + assertEquals("welcome to app en", value.getValue()); + checkGetRequestHeader(interceptor.getLastRequest()); + + // Check empty template + response.addHeader("etag", TEST_ETAG); + response.setContent("{}"); + + template = client.getTemplate(); + + assertEquals(TEST_ETAG, template.getETag()); + assertEquals(0, template.getParameters().size()); checkGetRequestHeader(interceptor.getLastRequest()); } From 6ce6806ba11a2e9a46af600efa438639217355e6 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Tue, 29 Sep 2020 13:52:03 -0400 Subject: [PATCH 05/11] Cleanup unused imports --- .../firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java index 7f9039993..2531eb867 100644 --- a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java +++ b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import com.google.api.client.http.HttpMethods; import com.google.api.client.http.HttpRequestFactory; import com.google.api.client.http.HttpResponseInterceptor; import com.google.api.client.json.JsonFactory; From dcb38566a8384a30fb509049b4ec92ee19b1920c Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Tue, 29 Sep 2020 14:08:01 -0400 Subject: [PATCH 06/11] Init defaultValue in RemoteConfigParameter --- .../google/firebase/remoteconfig/RemoteConfigParameter.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index dccad56f0..f1652244d 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -30,6 +30,7 @@ public final class RemoteConfigParameter { public RemoteConfigParameter() { conditionalValues = new HashMap<>(); + defaultValue = ExplicitParameterValue.of(""); } public RemoteConfigParameterValue getDefaultValue() { @@ -65,7 +66,7 @@ ParameterResponse toResponseType() { for (Map.Entry entry : conditionalValues.entrySet()) { conditionalResponseValues.put(entry.getKey(), entry.getValue().toResponseType()); } - return new ParameterResponse(this.defaultValue.toResponseType(), this.description, + return new ParameterResponse(defaultValue.toResponseType(), description, conditionalResponseValues); } } From 0f5d2c791c3a9645a40b32fcfe04aab74aaf1ef7 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Wed, 30 Sep 2020 15:18:06 -0400 Subject: [PATCH 07/11] Add javadocs and other PR fixes --- .../remoteconfig/ExplicitParameterValue.java | 41 ------------ .../FirebaseRemoteConfigClientImpl.java | 2 +- .../remoteconfig/InAppDefaultValue.java | 37 ----------- .../remoteconfig/RemoteConfigParameter.java | 56 +++++++++++++++-- .../RemoteConfigParameterValue.java | 63 ++++++++++++++++++- .../remoteconfig/RemoteConfigTemplate.java | 36 +++++++++-- .../internal/TemplateResponse.java | 29 ++++++--- .../FirebaseRemoteConfigClientImplTest.java | 46 +++++--------- src/test/resources/getRemoteConfig.json | 42 +++++++++++++ 9 files changed, 220 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java delete mode 100644 src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java create mode 100644 src/test/resources/getRemoteConfig.json diff --git a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java deleted file mode 100644 index 7803999d2..000000000 --- a/src/main/java/com/google/firebase/remoteconfig/ExplicitParameterValue.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; - -public final class ExplicitParameterValue extends RemoteConfigParameterValue { - - private final String value; - - private ExplicitParameterValue(String value) { - this.value = value; - } - - public static ExplicitParameterValue of(String value) { - return new ExplicitParameterValue(value); - } - - public String getValue() { - return this.value; - } - - @Override - ParameterValueResponse toResponseType() { - return ParameterValueResponse.ofValue(this.value); - } -} diff --git a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java index 2531eb867..038a700c3 100644 --- a/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java +++ b/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java @@ -96,7 +96,7 @@ public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException { .addAllHeaders(COMMON_HEADERS); IncomingHttpResponse response = httpClient.send(request); TemplateResponse templateResponse = httpClient.parse(response, TemplateResponse.class); - RemoteConfigTemplate template = templateResponse.toPublicType(); + RemoteConfigTemplate template = templateResponse.toRemoteConfigTemplate(); return template.setETag(getETag(response)); } diff --git a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java b/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java deleted file mode 100644 index d2fa8be26..000000000 --- a/src/main/java/com/google/firebase/remoteconfig/InAppDefaultValue.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; - -public final class InAppDefaultValue extends RemoteConfigParameterValue { - - private final boolean inAppDefaultValue; - - private InAppDefaultValue(boolean inAppDefaultValue) { - this.inAppDefaultValue = inAppDefaultValue; - } - - public static InAppDefaultValue getInstance() { - return new InAppDefaultValue(true); - } - - @Override - ParameterValueResponse toResponseType() { - return ParameterValueResponse.ofInAppDefaultValue(); - } -} diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index f1652244d..d6262250c 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -22,51 +22,99 @@ import java.util.HashMap; import java.util.Map; +/** + * Represents a Remote Config parameter that can be included in a {@link RemoteConfigTemplate}. + * At minimum, a default value or a conditional value must be present for the + * parameter to have any effect. + */ public final class RemoteConfigParameter { private RemoteConfigParameterValue defaultValue; private String description; private Map conditionalValues; + /** + * Creates a new {@link RemoteConfigParameter}. + */ public RemoteConfigParameter() { conditionalValues = new HashMap<>(); - defaultValue = ExplicitParameterValue.of(""); + defaultValue = RemoteConfigParameterValue.of(""); } + /** + * Gets the default value of the parameter. + * + * @return A {@link RemoteConfigParameterValue} instance. + */ public RemoteConfigParameterValue getDefaultValue() { return defaultValue; } + /** + * Gets the description of the parameter. + * + * @return The {@link String} description of the parameter. + */ public String getDescription() { return description; } + /** + * Gets the conditional values of the parameter. + * The condition name of the highest priority (the one listed first in the + * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. + * + * @return A map of conditional values. + */ public Map getConditionalValues() { return conditionalValues; } + /** + * Sets the default value of the parameter. + * This is the value to set the parameter to, when none of the named conditions + * evaluate to true. + * + * @param value An {@link RemoteConfigParameterValue} instance. + * @return This {@link RemoteConfigParameter}. + */ public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { defaultValue = value; return this; } + /** + * Sets the description of the parameter. + * Should not be over 100 characters and may contain any Unicode characters. + * + * @param description The description of the parameter. + * @return This {@link RemoteConfigParameter}. + */ public RemoteConfigParameter setDescription(String description) { this.description = description; return this; } + /** + * Sets the conditional values of the parameter. + * The condition name of the highest priority (the one listed first in the + * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. + * + * @param conditionalValues A map of conditional values. + * @return This {@link RemoteConfigParameter}. + */ public RemoteConfigParameter setConditionalValues( Map conditionalValues) { this.conditionalValues = conditionalValues; return this; } - ParameterResponse toResponseType() { + ParameterResponse toParameterResponse() { Map conditionalResponseValues = new HashMap<>(); for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalResponseValues.put(entry.getKey(), entry.getValue().toResponseType()); + conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); } - return new ParameterResponse(defaultValue.toResponseType(), description, + return new ParameterResponse(defaultValue.toParameterValueResponse(), description, conditionalResponseValues); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java index 342667497..6023b2d3d 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java @@ -18,6 +18,67 @@ import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; +/** + * Represents a Remote Config parameter value that can be used in a {@link RemoteConfigTemplate}. + */ public abstract class RemoteConfigParameterValue { - abstract ParameterValueResponse toResponseType(); + + /** + * Creates a new {@link RemoteConfigParameterValue.Explicit} instance with the given value. + * + * @param value The value of the {@link RemoteConfigParameterValue.Explicit}. + * @return A {@link RemoteConfigParameterValue.Explicit} instance. + */ + public static Explicit of(String value) { + return new Explicit(value); + } + + /** + * Creates a new {@link RemoteConfigParameterValue.InAppDefault} instance. + * + * @return A {@link RemoteConfigParameterValue.InAppDefault} instance. + */ + public static InAppDefault inAppDefault() { + return new InAppDefault(); + } + + abstract ParameterValueResponse toParameterValueResponse(); + + /** + * Represents an explicit Remote Config parameter value with a {@link String} value that the + * parameter is set to. + */ + public static final class Explicit extends RemoteConfigParameterValue { + + private final String value; + + private Explicit(String value) { + this.value = value; + } + + /** + * Gets the value of {@link RemoteConfigParameterValue.Explicit}. + * + * @return The {@link String} value. + */ + public String getValue() { + return this.value; + } + + @Override + ParameterValueResponse toParameterValueResponse() { + return ParameterValueResponse.ofValue(this.value); + } + } + + /** + * Represents an in-app-default parameter value. + */ + public static final class InAppDefault extends RemoteConfigParameterValue { + + @Override + ParameterValueResponse toParameterValueResponse() { + return ParameterValueResponse.ofInAppDefaultValue(); + } + } } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java index 3e46293c7..6732b930c 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java @@ -21,37 +21,61 @@ import java.util.HashMap; import java.util.Map; +/** + * Represents a Remote Config template. + */ public final class RemoteConfigTemplate { private String etag; private Map parameters; + /** + * Creates a new {@link RemoteConfigTemplate}. + */ public RemoteConfigTemplate() { parameters = new HashMap<>(); } + /** + * 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 map of parameter keys to their optional default values and optional conditional + * values. + */ public Map getParameters() { return this.parameters; } - public RemoteConfigTemplate setETag(String etag) { - this.etag = etag; + /** + * Sets the map of parameters of the template. + * + * @param parameters A map of parameter keys to their optional default values and optional + * conditional values. + * @return This {@link RemoteConfigTemplate} instance. + */ + public RemoteConfigTemplate setParameters(Map parameters) { + this.parameters = parameters; return this; } - public RemoteConfigTemplate setParameters(Map parameters) { - this.parameters = parameters; + RemoteConfigTemplate setETag(String etag) { + this.etag = etag; return this; } - TemplateResponse toResponseType() { + TemplateResponse toTemplateResponse() { Map parameterResponses = new HashMap<>(); for (Map.Entry entry : parameters.entrySet()) { - parameterResponses.put(entry.getKey(), entry.getValue().toResponseType()); + parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse()); } return new TemplateResponse(parameterResponses); } diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java index 2c422b87e..390505b84 100644 --- a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -17,8 +17,6 @@ package com.google.firebase.remoteconfig.internal; import com.google.api.client.util.Key; -import com.google.firebase.remoteconfig.ExplicitParameterValue; -import com.google.firebase.remoteconfig.InAppDefaultValue; import com.google.firebase.remoteconfig.RemoteConfigParameter; import com.google.firebase.remoteconfig.RemoteConfigParameterValue; import com.google.firebase.remoteconfig.RemoteConfigTemplate; @@ -27,6 +25,9 @@ import java.util.HashMap; import java.util.Map; +/** + * The DTO for parsing Remote Config template responses from the Remote Config service. + **/ public final class TemplateResponse { @Key("parameters") @@ -40,14 +41,17 @@ public TemplateResponse(Map parameters) { this.parameters = parameters; } - public RemoteConfigTemplate toPublicType() { + public RemoteConfigTemplate toRemoteConfigTemplate() { Map parameterPublicTypes = new HashMap<>(); for (Map.Entry entry : parameters.entrySet()) { - parameterPublicTypes.put(entry.getKey(), entry.getValue().toPublicType()); + parameterPublicTypes.put(entry.getKey(), entry.getValue().toRemoteConfigParameter()); } return new RemoteConfigTemplate().setParameters(parameterPublicTypes); } + /** + * The DTO for parsing Remote Config parameter responses from the Remote Config service. + **/ public static final class ParameterResponse { @Key("defaultValue") @@ -70,16 +74,21 @@ public ParameterResponse(ParameterValueResponse defaultValue, String description this.conditionalValues = conditionalValues; } - public RemoteConfigParameter toPublicType() { + public RemoteConfigParameter toRemoteConfigParameter() { Map conditionalPublicValues = new HashMap<>(); for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalPublicValues.put(entry.getKey(), entry.getValue().toPublicType()); + conditionalPublicValues + .put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue()); } - return new RemoteConfigParameter().setDefaultValue(defaultValue.toPublicType()) + return new RemoteConfigParameter() + .setDefaultValue(defaultValue.toRemoteConfigParameterValue()) .setDescription(description).setConditionalValues(conditionalPublicValues); } } + /** + * The DTO for parsing Remote Config parameter value responses from the Remote Config service. + **/ public static final class ParameterValueResponse { @Key("value") @@ -104,11 +113,11 @@ public static ParameterValueResponse ofInAppDefaultValue() { return new ParameterValueResponse(null, true); } - public RemoteConfigParameterValue toPublicType() { + public RemoteConfigParameterValue toRemoteConfigParameterValue() { if (this.inAppDefaultValue != null && this.inAppDefaultValue) { - return InAppDefaultValue.getInstance(); + return RemoteConfigParameterValue.inAppDefault(); } - return ExplicitParameterValue.of(this.value); + return RemoteConfigParameterValue.of(this.value); } } } diff --git a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java index 5c8881034..941b9f2ba 100644 --- a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java +++ b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java @@ -63,33 +63,8 @@ public class FirebaseRemoteConfigClientImplTest { 404, ErrorCode.NOT_FOUND, 500, ErrorCode.INTERNAL); - private static final String MOCK_TEMPLATE_RESPONSE = "{" - + " \"conditions\": [\n" - + " {\n" - + " \"name\": \"ios_en\",\n" - + " \"expression\": \"device.os == 'ios' && device.country in ['us', 'uk']\",\n" - + " \"tagColor\": \"INDIGO\"\n" - + " }\n" - + " ],\n" - + " \"parameters\": {\n" - + " \"welcome_message_text\": {\n" - + " \"defaultValue\": {\n" - + " \"value\": \"welcome to app\"\n" - + " },\n" - + " \"conditionalValues\": {\n" - + " \"ios_en\": {\n" - + " \"value\": \"welcome to app en\"\n" - + " }\n" - + " },\n" - + " \"description\": \"Text for welcome message!\"\n" - + " },\n" - + " \"header_text\": {\n" - + " \"defaultValue\": {\n" - + " \"useInAppDefault\": true\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String MOCK_TEMPLATE_RESPONSE = TestUtils + .loadResource("getRemoteConfig.json"); private static final String TEST_ETAG = "etag-123456789012-1"; @@ -116,19 +91,26 @@ public void testGetTemplate() throws Exception { assertEquals(2, parameters.size()); assertTrue(parameters.containsKey("welcome_message_text")); assertTrue(parameters.containsKey("header_text")); - Map conditionalValues = parameters - .get("welcome_message_text").getConditionalValues(); + RemoteConfigParameter parameter = parameters.get("welcome_message_text"); + assertEquals("text for welcome message!", parameter.getDescription()); + RemoteConfigParameterValue.Explicit defaultValue = + (RemoteConfigParameterValue.Explicit) parameter.getDefaultValue(); + assertEquals("welcome to app", defaultValue.getValue()); + Map conditionalValues = parameter.getConditionalValues(); assertEquals(1, conditionalValues.size()); assertTrue(conditionalValues.containsKey("ios_en")); - ExplicitParameterValue value = (ExplicitParameterValue) conditionalValues.get("ios_en"); + RemoteConfigParameterValue.Explicit value = + (RemoteConfigParameterValue.Explicit) conditionalValues.get("ios_en"); assertEquals("welcome to app en", value.getValue()); checkGetRequestHeader(interceptor.getLastRequest()); + } - // Check empty template + @Test + public void testGetTemplateWithEmptyTemplateResponse() throws Exception { response.addHeader("etag", TEST_ETAG); response.setContent("{}"); - template = client.getTemplate(); + RemoteConfigTemplate template = client.getTemplate(); assertEquals(TEST_ETAG, template.getETag()); assertEquals(0, template.getParameters().size()); diff --git a/src/test/resources/getRemoteConfig.json b/src/test/resources/getRemoteConfig.json new file mode 100644 index 000000000..86322f267 --- /dev/null +++ b/src/test/resources/getRemoteConfig.json @@ -0,0 +1,42 @@ +{ + "conditions": [ + { + "name": "ios_en", + "expression": "device.os == 'ios' && device.country in ['us', 'uk']", + "tagColor": "INDIGO" + }, + { + "name": "android_en", + "expression": "device.os == 'android' && device.country in ['us', 'uk']", + "tagColor": "GREEN" + } + ], + "parameters": { + "welcome_message_text": { + "defaultValue": { + "value": "welcome to app" + }, + "conditionalValues": { + "ios_en": { + "value": "welcome to app en" + } + }, + "description": "text for welcome message!" + }, + "header_text": { + "defaultValue": { + "useInAppDefault": true + } + } + }, + "parameterGroups": {}, + "version": { + "versionNumber": "17", + "updateOrigin": "ADMIN_SDK_NODE", + "updateType": "INCREMENTAL_UPDATE", + "updateUser": { + "email": "firebase-user@account.com" + }, + "updateTime": "Wed, 30 Sep 2020 17:56:07 GMT" + } +} From 9a8062901aac3bd84604c84b6f7513d251473764 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 1 Oct 2020 13:22:51 -0400 Subject: [PATCH 08/11] Fix javadocs --- .../firebase/remoteconfig/RemoteConfigParameterValue.java | 2 +- .../firebase/remoteconfig/internal/TemplateResponse.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java index 6023b2d3d..6a51ffd8f 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameterValue.java @@ -72,7 +72,7 @@ ParameterValueResponse toParameterValueResponse() { } /** - * Represents an in-app-default parameter value. + * Represents an in app default parameter value. */ public static final class InAppDefault extends RemoteConfigParameterValue { diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java index 390505b84..753c76248 100644 --- a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -26,7 +26,7 @@ import java.util.Map; /** - * The DTO for parsing Remote Config template responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config template responses from the Remote Config service. **/ public final class TemplateResponse { @@ -50,7 +50,7 @@ public RemoteConfigTemplate toRemoteConfigTemplate() { } /** - * The DTO for parsing Remote Config parameter responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config parameter responses from the Remote Config service. **/ public static final class ParameterResponse { @@ -87,7 +87,7 @@ public RemoteConfigParameter toRemoteConfigParameter() { } /** - * The DTO for parsing Remote Config parameter value responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config parameter value responses from the Remote Config service. **/ public static final class ParameterValueResponse { From 66ec860113d9ef70b234d344400278a3de9d32ca Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 1 Oct 2020 13:40:26 -0400 Subject: [PATCH 09/11] PR fixes --- .../remoteconfig/RemoteConfigParameter.java | 22 ++++++++-------- .../internal/TemplateResponse.java | 25 +++++++++++++------ .../FirebaseRemoteConfigClientImplTest.java | 18 +++++++------ 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index d6262250c..155d60fd7 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -33,14 +33,6 @@ public final class RemoteConfigParameter { private String description; private Map conditionalValues; - /** - * Creates a new {@link RemoteConfigParameter}. - */ - public RemoteConfigParameter() { - conditionalValues = new HashMap<>(); - defaultValue = RemoteConfigParameterValue.of(""); - } - /** * Gets the default value of the parameter. * @@ -110,11 +102,17 @@ public RemoteConfigParameter setConditionalValues( } ParameterResponse toParameterResponse() { - Map conditionalResponseValues = new HashMap<>(); - for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); + Map conditionalResponseValues = null; + if (conditionalValues != null) { + conditionalResponseValues = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); + } } - return new ParameterResponse(defaultValue.toParameterValueResponse(), description, + ParameterValueResponse parameterValueResponse = (defaultValue == null) ? null : defaultValue + .toParameterValueResponse(); + + return new ParameterResponse(parameterValueResponse, description, conditionalResponseValues); } } diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java index 753c76248..a91018e91 100644 --- a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -26,7 +26,8 @@ import java.util.Map; /** - * The Data Transfer Object for parsing Remote Config template responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config template responses from the + * Remote Config service. **/ public final class TemplateResponse { @@ -50,7 +51,8 @@ public RemoteConfigTemplate toRemoteConfigTemplate() { } /** - * The Data Transfer Object for parsing Remote Config parameter responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config parameter responses from the + * Remote Config service. **/ public static final class ParameterResponse { @@ -75,19 +77,26 @@ public ParameterResponse(ParameterValueResponse defaultValue, String description } public RemoteConfigParameter toRemoteConfigParameter() { - Map conditionalPublicValues = new HashMap<>(); - for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalPublicValues - .put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue()); + Map conditionalPublicValues = null; + if (conditionalValues != null) { + conditionalPublicValues = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + conditionalPublicValues + .put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue()); + } } + RemoteConfigParameterValue remoteConfigParameterValue = + (defaultValue == null) ? null : defaultValue.toRemoteConfigParameterValue(); + return new RemoteConfigParameter() - .setDefaultValue(defaultValue.toRemoteConfigParameterValue()) + .setDefaultValue(remoteConfigParameterValue) .setDescription(description).setConditionalValues(conditionalPublicValues); } } /** - * The Data Transfer Object for parsing Remote Config parameter value responses from the Remote Config service. + * The Data Transfer Object for parsing Remote Config parameter value responses from the + * Remote Config service. **/ public static final class ParameterValueResponse { diff --git a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java index 941b9f2ba..8ee5fb595 100644 --- a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java +++ b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImplTest.java @@ -90,18 +90,22 @@ public void testGetTemplate() throws Exception { Map parameters = template.getParameters(); assertEquals(2, parameters.size()); assertTrue(parameters.containsKey("welcome_message_text")); - assertTrue(parameters.containsKey("header_text")); - RemoteConfigParameter parameter = parameters.get("welcome_message_text"); - assertEquals("text for welcome message!", parameter.getDescription()); - RemoteConfigParameterValue.Explicit defaultValue = - (RemoteConfigParameterValue.Explicit) parameter.getDefaultValue(); - assertEquals("welcome to app", defaultValue.getValue()); - Map conditionalValues = parameter.getConditionalValues(); + RemoteConfigParameter welcomeMessageParameter = parameters.get("welcome_message_text"); + assertEquals("text for welcome message!", welcomeMessageParameter.getDescription()); + RemoteConfigParameterValue.Explicit explicitDefaultValue = + (RemoteConfigParameterValue.Explicit) welcomeMessageParameter.getDefaultValue(); + assertEquals("welcome to app", explicitDefaultValue.getValue()); + Map conditionalValues = welcomeMessageParameter + .getConditionalValues(); assertEquals(1, conditionalValues.size()); assertTrue(conditionalValues.containsKey("ios_en")); RemoteConfigParameterValue.Explicit value = (RemoteConfigParameterValue.Explicit) conditionalValues.get("ios_en"); assertEquals("welcome to app en", value.getValue()); + assertTrue(parameters.containsKey("header_text")); + RemoteConfigParameter headerParameter = parameters.get("header_text"); + assertTrue( + headerParameter.getDefaultValue() instanceof RemoteConfigParameterValue.InAppDefault); checkGetRequestHeader(interceptor.getLastRequest()); } From 8aaebef4c8575492027c8441da6301dbd14560ca Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 1 Oct 2020 18:07:33 -0400 Subject: [PATCH 10/11] Made all collections non-null --- .../remoteconfig/RemoteConfigParameter.java | 40 ++++++++++++------- .../remoteconfig/RemoteConfigTemplate.java | 16 +++++--- .../internal/TemplateResponse.java | 26 +++++++----- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java index 155d60fd7..757c1437b 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigParameter.java @@ -16,6 +16,11 @@ package com.google.firebase.remoteconfig; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.firebase.internal.NonNull; +import com.google.firebase.internal.Nullable; import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse; import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; @@ -33,11 +38,19 @@ public final class RemoteConfigParameter { private String description; private Map conditionalValues; + /** + * Creates a new {@link RemoteConfigParameter}. + */ + public RemoteConfigParameter() { + conditionalValues = new HashMap<>(); + } + /** * Gets the default value of the parameter. * - * @return A {@link RemoteConfigParameterValue} instance. + * @return A {@link RemoteConfigParameterValue} instance or null. */ + @Nullable public RemoteConfigParameterValue getDefaultValue() { return defaultValue; } @@ -45,8 +58,9 @@ public RemoteConfigParameterValue getDefaultValue() { /** * Gets the description of the parameter. * - * @return The {@link String} description of the parameter. + * @return The {@link String} description of the parameter or null. */ + @Nullable public String getDescription() { return description; } @@ -56,8 +70,9 @@ public String getDescription() { * The condition name of the highest priority (the one listed first in the * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. * - * @return A map of conditional values. + * @return A non-null map of conditional values. */ + @NonNull public Map getConditionalValues() { return conditionalValues; } @@ -70,7 +85,7 @@ public Map getConditionalValues() { * @param value An {@link RemoteConfigParameterValue} instance. * @return This {@link RemoteConfigParameter}. */ - public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { + public RemoteConfigParameter setDefaultValue(@Nullable RemoteConfigParameterValue value) { defaultValue = value; return this; } @@ -82,7 +97,7 @@ public RemoteConfigParameter setDefaultValue(RemoteConfigParameterValue value) { * @param description The description of the parameter. * @return This {@link RemoteConfigParameter}. */ - public RemoteConfigParameter setDescription(String description) { + public RemoteConfigParameter setDescription(@Nullable String description) { this.description = description; return this; } @@ -92,26 +107,23 @@ public RemoteConfigParameter setDescription(String description) { * The condition name of the highest priority (the one listed first in the * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. * - * @param conditionalValues A map of conditional values. + * @param conditionalValues A non-null map of conditional values. * @return This {@link RemoteConfigParameter}. */ public RemoteConfigParameter setConditionalValues( - Map conditionalValues) { + @NonNull Map conditionalValues) { + checkNotNull(conditionalValues, "conditional values must not be null."); this.conditionalValues = conditionalValues; return this; } ParameterResponse toParameterResponse() { - Map conditionalResponseValues = null; - if (conditionalValues != null) { - conditionalResponseValues = new HashMap<>(); - for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); - } + Map conditionalResponseValues = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); } ParameterValueResponse parameterValueResponse = (defaultValue == null) ? null : defaultValue .toParameterValueResponse(); - return new ParameterResponse(parameterValueResponse, description, conditionalResponseValues); } diff --git a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java index 6732b930c..143f5692b 100644 --- a/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java +++ b/src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java @@ -16,6 +16,9 @@ package com.google.firebase.remoteconfig; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.firebase.internal.NonNull; import com.google.firebase.remoteconfig.internal.TemplateResponse; import java.util.HashMap; @@ -48,9 +51,10 @@ public String getETag() { /** * Gets the map of parameters of the template. * - * @return A map of parameter keys to their optional default values and optional conditional - * values. + * @return A non-null map of parameter keys to their optional default values and optional + * conditional values. */ + @NonNull public Map getParameters() { return this.parameters; } @@ -58,11 +62,13 @@ public Map getParameters() { /** * Sets the map of parameters of the template. * - * @param parameters A map of parameter keys to their optional default values and optional - * conditional values. + * @param parameters A non-null map of parameter keys to their optional default values and + * optional conditional values. * @return This {@link RemoteConfigTemplate} instance. */ - public RemoteConfigTemplate setParameters(Map parameters) { + public RemoteConfigTemplate setParameters( + @NonNull Map parameters) { + checkNotNull(parameters, "parameters must not be null."); this.parameters = parameters; return this; } diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java index a91018e91..9dbd93b31 100644 --- a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -16,7 +16,12 @@ package com.google.firebase.remoteconfig.internal; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.api.client.util.Key; +import com.google.firebase.database.annotations.Nullable; +import com.google.firebase.internal.NonNull; import com.google.firebase.remoteconfig.RemoteConfigParameter; import com.google.firebase.remoteconfig.RemoteConfigParameterValue; import com.google.firebase.remoteconfig.RemoteConfigTemplate; @@ -38,7 +43,8 @@ public TemplateResponse() { parameters = Collections.emptyMap(); } - public TemplateResponse(Map parameters) { + public TemplateResponse(@NonNull Map parameters) { + checkNotNull(parameters, "parameters must not be null."); this.parameters = parameters; } @@ -69,25 +75,23 @@ public ParameterResponse() { conditionalValues = Collections.emptyMap(); } - public ParameterResponse(ParameterValueResponse defaultValue, String description, - Map conditionalValues) { + public ParameterResponse(@Nullable ParameterValueResponse defaultValue, + @Nullable String description, + @NonNull Map conditionalValues) { + checkNotNull(conditionalValues, "conditional values must not be null."); this.defaultValue = defaultValue; this.description = description; this.conditionalValues = conditionalValues; } public RemoteConfigParameter toRemoteConfigParameter() { - Map conditionalPublicValues = null; - if (conditionalValues != null) { - conditionalPublicValues = new HashMap<>(); - for (Map.Entry entry : conditionalValues.entrySet()) { - conditionalPublicValues - .put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue()); - } + Map conditionalPublicValues = new HashMap<>(); + for (Map.Entry entry : conditionalValues.entrySet()) { + conditionalPublicValues + .put(entry.getKey(), entry.getValue().toRemoteConfigParameterValue()); } RemoteConfigParameterValue remoteConfigParameterValue = (defaultValue == null) ? null : defaultValue.toRemoteConfigParameterValue(); - return new RemoteConfigParameter() .setDefaultValue(remoteConfigParameterValue) .setDescription(description).setConditionalValues(conditionalPublicValues); From a4e8a4288a2a560daac5165e09b4a816ad1cb585 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 1 Oct 2020 18:33:18 -0400 Subject: [PATCH 11/11] One setter per line --- .../firebase/remoteconfig/internal/TemplateResponse.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java index 9dbd93b31..c1ae433da 100644 --- a/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java +++ b/src/main/java/com/google/firebase/remoteconfig/internal/TemplateResponse.java @@ -78,10 +78,9 @@ public ParameterResponse() { public ParameterResponse(@Nullable ParameterValueResponse defaultValue, @Nullable String description, @NonNull Map conditionalValues) { - checkNotNull(conditionalValues, "conditional values must not be null."); this.defaultValue = defaultValue; this.description = description; - this.conditionalValues = conditionalValues; + this.conditionalValues = checkNotNull(conditionalValues); } public RemoteConfigParameter toRemoteConfigParameter() { @@ -94,7 +93,8 @@ public RemoteConfigParameter toRemoteConfigParameter() { (defaultValue == null) ? null : defaultValue.toRemoteConfigParameterValue(); return new RemoteConfigParameter() .setDefaultValue(remoteConfigParameterValue) - .setDescription(description).setConditionalValues(conditionalPublicValues); + .setDescription(description) + .setConditionalValues(conditionalPublicValues); } }