|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.google.firebase.remoteconfig; |
| 19 | + |
| 20 | +import static com.google.common.base.Preconditions.checkArgument; |
| 21 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | + |
| 23 | +import com.google.common.base.Strings; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.firebase.internal.NonNull; |
| 26 | +import com.google.firebase.remoteconfig.internal.ServerTemplateResponse.CustomSignalConditionResponse; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +final class CustomSignalCondition { |
| 32 | + private final String customSignalKey; |
| 33 | + private final CustomSignalOperator customSignalOperator; |
| 34 | + private final ImmutableList<String> targetCustomSignalValues; |
| 35 | + |
| 36 | + public CustomSignalCondition( |
| 37 | + @NonNull String customSignalKey, |
| 38 | + @NonNull CustomSignalOperator customSignalOperator, |
| 39 | + @NonNull List<String> targetCustomSignalValues) { |
| 40 | + checkArgument( |
| 41 | + !Strings.isNullOrEmpty(customSignalKey), "Custom signal key must not be null or empty."); |
| 42 | + checkNotNull(customSignalOperator); |
| 43 | + checkNotNull(targetCustomSignalValues); |
| 44 | + checkArgument( |
| 45 | + !targetCustomSignalValues.isEmpty(), "Target custom signal values must not be empty."); |
| 46 | + this.customSignalKey = customSignalKey.trim(); |
| 47 | + this.customSignalOperator = customSignalOperator; |
| 48 | + this.targetCustomSignalValues = ImmutableList.copyOf(targetCustomSignalValues); |
| 49 | + } |
| 50 | + |
| 51 | + CustomSignalCondition(CustomSignalConditionResponse customSignalCondition) { |
| 52 | + checkArgument( |
| 53 | + !Strings.isNullOrEmpty(customSignalCondition.getKey()), |
| 54 | + "Custom signal key must not be null or empty."); |
| 55 | + checkArgument( |
| 56 | + !customSignalCondition.getTargetValues().isEmpty(), |
| 57 | + "Target custom signal values must not be empty."); |
| 58 | + this.customSignalKey = customSignalCondition.getKey().trim(); |
| 59 | + List<String> targetCustomSignalValuesList = customSignalCondition.getTargetValues(); |
| 60 | + this.targetCustomSignalValues = ImmutableList.copyOf(targetCustomSignalValuesList); |
| 61 | + switch (customSignalCondition.getOperator()) { |
| 62 | + case "NUMERIC_EQUAL": |
| 63 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_EQUAL; |
| 64 | + break; |
| 65 | + case "NUMERIC_GREATER_EQUAL": |
| 66 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_GREATER_EQUAL; |
| 67 | + break; |
| 68 | + case "NUMERIC_GREATER_THAN": |
| 69 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_GREATER_THAN; |
| 70 | + break; |
| 71 | + case "NUMERIC_LESS_EQUAL": |
| 72 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_LESS_EQUAL; |
| 73 | + break; |
| 74 | + case "NUMERIC_LESS_THAN": |
| 75 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_LESS_THAN; |
| 76 | + break; |
| 77 | + case "NUMERIC_NOT_EQUAL": |
| 78 | + this.customSignalOperator = CustomSignalOperator.NUMERIC_NOT_EQUAL; |
| 79 | + break; |
| 80 | + case "SEMANTIC_VERSION_EQUAL": |
| 81 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_EQUAL; |
| 82 | + break; |
| 83 | + case "SEMANTIC_VERSION_GREATER_EQUAL": |
| 84 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_GREATER_EQUAL; |
| 85 | + break; |
| 86 | + case "SEMANTIC_VERSION_GREATER_THAN": |
| 87 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_GREATER_THAN; |
| 88 | + break; |
| 89 | + case "SEMANTIC_VERSION_LESS_EQUAL": |
| 90 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_LESS_EQUAL; |
| 91 | + break; |
| 92 | + case "SEMANTIC_VERSION_LESS_THAN": |
| 93 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_LESS_THAN; |
| 94 | + break; |
| 95 | + case "SEMANTIC_VERSION_NOT_EQUAL": |
| 96 | + this.customSignalOperator = CustomSignalOperator.SEMANTIC_VERSION_NOT_EQUAL; |
| 97 | + break; |
| 98 | + case "STRING_CONTAINS": |
| 99 | + this.customSignalOperator = CustomSignalOperator.STRING_CONTAINS; |
| 100 | + break; |
| 101 | + case "STRING_CONTAINS_REGEX": |
| 102 | + this.customSignalOperator = CustomSignalOperator.STRING_CONTAINS_REGEX; |
| 103 | + break; |
| 104 | + case "STRING_DOES_NOT_CONTAIN": |
| 105 | + this.customSignalOperator = CustomSignalOperator.STRING_DOES_NOT_CONTAIN; |
| 106 | + break; |
| 107 | + case "STRING_EXACTLY_MATCHES": |
| 108 | + this.customSignalOperator = CustomSignalOperator.STRING_EXACTLY_MATCHES; |
| 109 | + break; |
| 110 | + default: |
| 111 | + this.customSignalOperator = CustomSignalOperator.UNSPECIFIED; |
| 112 | + } |
| 113 | + checkArgument( |
| 114 | + this.customSignalOperator != CustomSignalOperator.UNSPECIFIED, |
| 115 | + "Custom signal operator passed is invalid"); |
| 116 | + } |
| 117 | + |
| 118 | + @NonNull |
| 119 | + String getCustomSignalKey() { |
| 120 | + return customSignalKey; |
| 121 | + } |
| 122 | + |
| 123 | + @NonNull |
| 124 | + CustomSignalOperator getCustomSignalOperator() { |
| 125 | + return customSignalOperator; |
| 126 | + } |
| 127 | + |
| 128 | + @NonNull |
| 129 | + List<String> getTargetCustomSignalValues() { |
| 130 | + return new ArrayList<>(targetCustomSignalValues); |
| 131 | + } |
| 132 | + |
| 133 | + CustomSignalConditionResponse toCustomConditonResponse() { |
| 134 | + CustomSignalConditionResponse customSignalConditionResponse = |
| 135 | + new CustomSignalConditionResponse(); |
| 136 | + customSignalConditionResponse.setKey(this.customSignalKey); |
| 137 | + customSignalConditionResponse.setOperator(this.customSignalOperator.getOperator()); |
| 138 | + customSignalConditionResponse.setTargetValues(this.targetCustomSignalValues); |
| 139 | + return customSignalConditionResponse; |
| 140 | + } |
| 141 | +} |
0 commit comments