Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Remote Config Parameter Group type #490

Merged
merged 4 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/main/java/com/google/firebase/remoteconfig/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents a Remote Config parameter that can be included in a {@link Template}.
Expand Down Expand Up @@ -73,7 +74,7 @@ public ParameterValue getDefaultValue() {
/**
* Gets the description of the parameter.
*
* @return The {@link String} description of the parameter or null.
* @return The description of the parameter or null.
*/
@Nullable
public String getDescription() {
Expand Down Expand Up @@ -144,4 +145,23 @@ ParameterResponse toParameterResponse() {
.setDescription(description)
.setConditionalValues(conditionalResponseValues);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Parameter parameter = (Parameter) o;
return Objects.equals(defaultValue, parameter.defaultValue)
&& Objects.equals(description, parameter.description)
&& Objects.equals(conditionalValues, parameter.conditionalValues);
}

@Override
public int hashCode() {
return Objects.hash(defaultValue, description, conditionalValues);
}
}
138 changes: 138 additions & 0 deletions src/main/java/com/google/firebase/remoteconfig/ParameterGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.remoteconfig;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;
import com.google.firebase.remoteconfig.internal.TemplateResponse;
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterGroupResponse;
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents a Remote Config parameter group that can be included in a {@link Template}.
* Grouping parameters is only for management purposes and does not affect client-side
* fetching of parameter values.
*/
public final class ParameterGroup {

private String description;
private Map<String, Parameter> parameters;

/**
* Creates a new {@link ParameterGroup}.
*/
public ParameterGroup() {
parameters = new HashMap<>();
}

ParameterGroup(@NonNull ParameterGroupResponse parameterGroupResponse) {
checkNotNull(parameterGroupResponse);
this.parameters = new HashMap<>();
if (parameterGroupResponse.getParameters() != null) {
for (Map.Entry<String, TemplateResponse.ParameterResponse> entry
: parameterGroupResponse.getParameters().entrySet()) {
this.parameters.put(entry.getKey(), new Parameter(entry.getValue()));
}
}
this.description = parameterGroupResponse.getDescription();
}

/**
* Gets the description of the parameter group.
*
* @return The description of the parameter or null.
*/
@Nullable
public String getDescription() {
return description;
}

/**
* Gets the map of parameters that belong to this group.
*
* @return A non-null map of parameter keys to their optional default values and optional
* conditional values.
*/
@NonNull
public Map<String, Parameter> getParameters() {
return parameters;
}

/**
* Sets the description of the parameter group.
* Should not be over 256 characters and may contain any Unicode characters.
*
* @param description The description of the parameter group.
* @return This {@link ParameterGroup}.
*/
public ParameterGroup setDescription(@Nullable String description) {
this.description = description;
return this;
}

/**
* Sets the map of parameters that belong to this group.
*
* <p>A parameter only appears once per Remote Config template.
* An ungrouped parameter appears at the top level, whereas a
* parameter organized within a group appears within its group's map of parameters.
*
* @param parameters A non-null map of parameter keys to their optional default values and
* optional conditional values.
* @return This {@link ParameterGroup} instance.
*/
public ParameterGroup setParameters(
@NonNull Map<String, Parameter> parameters) {
checkNotNull(parameters, "parameters must not be null.");
this.parameters = parameters;
return this;
}

ParameterGroupResponse toParameterGroupResponse() {
Map<String, ParameterResponse> parameterResponses = new HashMap<>();
for (Map.Entry<String, Parameter> entry : this.parameters.entrySet()) {
parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse());
}
return new ParameterGroupResponse()
.setDescription(this.description)
.setParameters(parameterResponses);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ParameterGroup that = (ParameterGroup) o;
return Objects.equals(description, that.description)
&& Objects.equals(parameters, that.parameters);
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public int hashCode() {
return Objects.hash(description, parameters);
}
}
34 changes: 32 additions & 2 deletions src/main/java/com/google/firebase/remoteconfig/ParameterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.firebase.internal.NonNull;
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse;

import java.util.Objects;

/**
* Represents a Remote Config parameter value that can be used in a {@link Template}.
*/
Expand Down Expand Up @@ -57,7 +59,7 @@ static ParameterValue fromParameterValueResponse(
}

/**
* Represents an explicit Remote Config parameter value with a {@link String} value that the
* Represents an explicit Remote Config parameter value with a value that the
* parameter is set to.
*/
public static final class Explicit extends ParameterValue {
Expand All @@ -71,7 +73,7 @@ private Explicit(String value) {
/**
* Gets the value of {@link ParameterValue.Explicit}.
*
* @return The {@link String} value.
* @return The value.
*/
public String getValue() {
return this.value;
Expand All @@ -82,6 +84,23 @@ ParameterValueResponse toParameterValueResponse() {
return new ParameterValueResponse()
.setValue(this.value);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Explicit explicit = (Explicit) o;
return Objects.equals(value, explicit.value);
}

@Override
public int hashCode() {
return Objects.hash(value);
}
}

/**
Expand All @@ -93,5 +112,16 @@ public static final class InAppDefault extends ParameterValue {
ParameterValueResponse toParameterValueResponse() {
return new ParameterValueResponse().setUseInAppDefault(true);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return true;
}
}
}
63 changes: 61 additions & 2 deletions src/main/java/com/google/firebase/remoteconfig/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Represents a Remote Config template.
Expand All @@ -34,19 +35,22 @@ public final class Template {
private String etag;
private Map<String, Parameter> parameters;
private List<Condition> conditions;
private Map<String, ParameterGroup> parameterGroups;

/**
* Creates a new {@link Template}.
*/
public Template() {
parameters = new HashMap<>();
conditions = new ArrayList<>();
parameterGroups = new HashMap<>();
}

Template(@NonNull TemplateResponse templateResponse) {
checkNotNull(templateResponse);
this.parameters = new HashMap<>();
this.conditions = new ArrayList<>();
this.parameterGroups = new HashMap<>();
if (templateResponse.getParameters() != null) {
for (Map.Entry<String, TemplateResponse.ParameterResponse> entry
: templateResponse.getParameters().entrySet()) {
Expand All @@ -59,6 +63,12 @@ public Template() {
this.conditions.add(new Condition(conditionResponse));
}
}
if (templateResponse.getParameterGroups() != null) {
for (Map.Entry<String, TemplateResponse.ParameterGroupResponse> entry
: templateResponse.getParameterGroups().entrySet()) {
this.parameterGroups.put(entry.getKey(), new ParameterGroup(entry.getValue()));
}
}
}

/**
Expand All @@ -84,13 +94,23 @@ public Map<String, Parameter> getParameters() {
/**
* Gets the list of conditions of the template.
*
* @return A non-null list of conditions
* @return A non-null list of conditions.
*/
@NonNull
public List<Condition> getConditions() {
return conditions;
}

/**
* Gets the map of parameter groups of the template.
*
* @return A non-null map of parameter group names to their parameter group instances.
*/
@NonNull
public Map<String, ParameterGroup> getParameterGroups() {
return parameterGroups;
}

/**
* Sets the map of parameters of the template.
*
Expand Down Expand Up @@ -118,6 +138,20 @@ public Template setConditions(
return this;
}

/**
* Sets the map of parameter groups of the template.
*
* @param parameterGroups A non-null map of parameter group names to their
* parameter group instances.
* @return This {@link Template} instance.
*/
public Template setParameterGroups(
@NonNull Map<String, ParameterGroup> parameterGroups) {
checkNotNull(parameterGroups, "parameter groups must not be null.");
this.parameterGroups = parameterGroups;
return this;
}

Template setETag(String etag) {
this.etag = etag;
return this;
Expand All @@ -132,8 +166,33 @@ TemplateResponse toTemplateResponse() {
for (Condition condition : this.conditions) {
conditionResponses.add(condition.toConditionResponse());
}
Map<String, TemplateResponse.ParameterGroupResponse> parameterGroupResponse = new HashMap<>();
for (Map.Entry<String, ParameterGroup> entry : this.parameterGroups.entrySet()) {
parameterGroupResponse.put(entry.getKey(), entry.getValue().toParameterGroupResponse());
}
return new TemplateResponse()
.setParameters(parameterResponses)
.setConditions(conditionResponses);
.setConditions(conditionResponses)
.setParameterGroups(parameterGroupResponse);
}

@Override
public boolean equals(Object o) {
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Template template = (Template) o;
return Objects.equals(etag, template.etag)
&& Objects.equals(parameters, template.parameters)
&& Objects.equals(conditions, template.conditions)
&& Objects.equals(parameterGroups, template.parameterGroups);
}

@Override
public int hashCode() {
return Objects.hash(etag, parameters, conditions, parameterGroups);
}
}
Loading