|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.remoteconfig; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.firebase.internal.NonNull; |
| 23 | +import com.google.firebase.internal.Nullable; |
| 24 | +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse; |
| 25 | +import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * Represents a Remote Config parameter that can be included in a {@link RemoteConfigTemplate}. |
| 32 | + * At minimum, a default value or a conditional value must be present for the |
| 33 | + * parameter to have any effect. |
| 34 | + */ |
| 35 | +public final class RemoteConfigParameter { |
| 36 | + |
| 37 | + private RemoteConfigParameterValue defaultValue; |
| 38 | + private String description; |
| 39 | + private Map<String, RemoteConfigParameterValue> conditionalValues; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new {@link RemoteConfigParameter}. |
| 43 | + */ |
| 44 | + public RemoteConfigParameter() { |
| 45 | + conditionalValues = new HashMap<>(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the default value of the parameter. |
| 50 | + * |
| 51 | + * @return A {@link RemoteConfigParameterValue} instance or null. |
| 52 | + */ |
| 53 | + @Nullable |
| 54 | + public RemoteConfigParameterValue getDefaultValue() { |
| 55 | + return defaultValue; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets the description of the parameter. |
| 60 | + * |
| 61 | + * @return The {@link String} description of the parameter or null. |
| 62 | + */ |
| 63 | + @Nullable |
| 64 | + public String getDescription() { |
| 65 | + return description; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets the conditional values of the parameter. |
| 70 | + * The condition name of the highest priority (the one listed first in the |
| 71 | + * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. |
| 72 | + * |
| 73 | + * @return A non-null map of conditional values. |
| 74 | + */ |
| 75 | + @NonNull |
| 76 | + public Map<String, RemoteConfigParameterValue> getConditionalValues() { |
| 77 | + return conditionalValues; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the default value of the parameter. |
| 82 | + * This is the value to set the parameter to, when none of the named conditions |
| 83 | + * evaluate to true. |
| 84 | + * |
| 85 | + * @param value An {@link RemoteConfigParameterValue} instance. |
| 86 | + * @return This {@link RemoteConfigParameter}. |
| 87 | + */ |
| 88 | + public RemoteConfigParameter setDefaultValue(@Nullable RemoteConfigParameterValue value) { |
| 89 | + defaultValue = value; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the description of the parameter. |
| 95 | + * Should not be over 100 characters and may contain any Unicode characters. |
| 96 | + * |
| 97 | + * @param description The description of the parameter. |
| 98 | + * @return This {@link RemoteConfigParameter}. |
| 99 | + */ |
| 100 | + public RemoteConfigParameter setDescription(@Nullable String description) { |
| 101 | + this.description = description; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the conditional values of the parameter. |
| 107 | + * The condition name of the highest priority (the one listed first in the |
| 108 | + * {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter. |
| 109 | + * |
| 110 | + * @param conditionalValues A non-null map of conditional values. |
| 111 | + * @return This {@link RemoteConfigParameter}. |
| 112 | + */ |
| 113 | + public RemoteConfigParameter setConditionalValues( |
| 114 | + @NonNull Map<String, RemoteConfigParameterValue> conditionalValues) { |
| 115 | + checkNotNull(conditionalValues, "conditional values must not be null."); |
| 116 | + this.conditionalValues = conditionalValues; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + ParameterResponse toParameterResponse() { |
| 121 | + Map<String, ParameterValueResponse> conditionalResponseValues = new HashMap<>(); |
| 122 | + for (Map.Entry<String, RemoteConfigParameterValue> entry : conditionalValues.entrySet()) { |
| 123 | + conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse()); |
| 124 | + } |
| 125 | + ParameterValueResponse parameterValueResponse = (defaultValue == null) ? null : defaultValue |
| 126 | + .toParameterValueResponse(); |
| 127 | + return new ParameterResponse(parameterValueResponse, description, |
| 128 | + conditionalResponseValues); |
| 129 | + } |
| 130 | +} |
0 commit comments