|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.remoteconfig; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.common.base.Strings; |
| 23 | +import com.google.firebase.internal.NonNull; |
| 24 | +import com.google.firebase.internal.Nullable; |
| 25 | +import com.google.firebase.remoteconfig.internal.TemplateResponse.ConditionResponse; |
| 26 | + |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a Remote Config condition that can be included in a {@link Template}. |
| 31 | + * A condition targets a specific group of users. A list of these conditions make up |
| 32 | + * part of a Remote Config template. |
| 33 | + */ |
| 34 | +public final class Condition { |
| 35 | + |
| 36 | + private String name; |
| 37 | + private String expression; |
| 38 | + private TagColor tagColor; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new {@link Condition}. |
| 42 | + * |
| 43 | + * @param name A non-null, non-empty, and unique name of this condition. |
| 44 | + * @param expression A non-null and non-empty expression of this condition. |
| 45 | + */ |
| 46 | + public Condition(@NonNull String name, @NonNull String expression) { |
| 47 | + this(name, expression, null); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new {@link Condition}. |
| 52 | + * |
| 53 | + * @param name A non-null, non-empty, and unique name of this condition. |
| 54 | + * @param expression A non-null and non-empty expression of this condition. |
| 55 | + * @param tagColor A color associated with this condition for display purposes in the |
| 56 | + * Firebase Console. Not specifying this value results in the console picking an |
| 57 | + * arbitrary color to associate with the condition. |
| 58 | + */ |
| 59 | + public Condition(@NonNull String name, @NonNull String expression, @Nullable TagColor tagColor) { |
| 60 | + checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty"); |
| 61 | + checkArgument(!Strings.isNullOrEmpty(expression), |
| 62 | + "condition expression must not be null or empty"); |
| 63 | + this.name = name; |
| 64 | + this.expression = expression; |
| 65 | + this.tagColor = tagColor; |
| 66 | + } |
| 67 | + |
| 68 | + Condition(@NonNull ConditionResponse conditionResponse) { |
| 69 | + checkNotNull(conditionResponse); |
| 70 | + this.name = conditionResponse.getName(); |
| 71 | + this.expression = conditionResponse.getExpression(); |
| 72 | + if (!Strings.isNullOrEmpty(conditionResponse.getTagColor())) { |
| 73 | + this.tagColor = TagColor.valueOf(conditionResponse.getTagColor()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets the name of the condition. |
| 79 | + * |
| 80 | + * @return The name of the condition. |
| 81 | + */ |
| 82 | + @NonNull |
| 83 | + public String getName() { |
| 84 | + return name; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets the expression of the condition. |
| 89 | + * |
| 90 | + * @return The expression of the condition. |
| 91 | + */ |
| 92 | + @NonNull |
| 93 | + public String getExpression() { |
| 94 | + return expression; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets the tag color of the condition used for display purposes in the Firebase Console. |
| 99 | + * |
| 100 | + * @return The tag color of the condition. |
| 101 | + */ |
| 102 | + @NonNull |
| 103 | + public TagColor getTagColor() { |
| 104 | + return tagColor; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets the name of the condition. |
| 109 | + * |
| 110 | + * @param name A non-empty and unique name of this condition. |
| 111 | + * @return This {@link Condition}. |
| 112 | + */ |
| 113 | + public Condition setName(@NonNull String name) { |
| 114 | + checkArgument(!Strings.isNullOrEmpty(name), "condition name must not be null or empty"); |
| 115 | + this.name = name; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the expression of the condition. |
| 121 | + * |
| 122 | + * <p>See <a href="https://firebase.google.com/docs/remote-config/condition-reference"> |
| 123 | + * condition expressions</a> for the expected syntax of this field. |
| 124 | + * |
| 125 | + * @param expression The logic of this condition. |
| 126 | + * @return This {@link Condition}. |
| 127 | + */ |
| 128 | + public Condition setExpression(@NonNull String expression) { |
| 129 | + checkArgument(!Strings.isNullOrEmpty(expression), |
| 130 | + "condition expression must not be null or empty"); |
| 131 | + this.expression = expression; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the tag color of the condition. |
| 137 | + * |
| 138 | + * <p>The color associated with this condition for display purposes in the Firebase Console. |
| 139 | + * Not specifying this value results in the console picking an arbitrary color to associate |
| 140 | + * with the condition. |
| 141 | + * |
| 142 | + * @param tagColor The tag color of this condition. |
| 143 | + * @return This {@link Condition}. |
| 144 | + */ |
| 145 | + public Condition setTagColor(@Nullable TagColor tagColor) { |
| 146 | + this.tagColor = tagColor; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + ConditionResponse toConditionResponse() { |
| 151 | + return new ConditionResponse() |
| 152 | + .setName(this.name) |
| 153 | + .setExpression(this.expression) |
| 154 | + .setTagColor(this.tagColor == null ? null : this.tagColor.getColor()); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public boolean equals(Object o) { |
| 159 | + if (this == o) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + if (o == null || getClass() != o.getClass()) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + Condition condition = (Condition) o; |
| 166 | + return Objects.equals(name, condition.name) |
| 167 | + && Objects.equals(expression, condition.expression) && tagColor == condition.tagColor; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public int hashCode() { |
| 172 | + return Objects.hash(name, expression, tagColor); |
| 173 | + } |
| 174 | +} |
0 commit comments