|
| 1 | +/* |
| 2 | +* Copyright 2025 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.ServerTemplateResponse.PercentConditionResponse; |
| 26 | + |
| 27 | +/** Represents a condition that compares the instance pseudo-random percentile to a given limit. */ |
| 28 | +public final class PercentCondition { |
| 29 | + private int microPercent; |
| 30 | + private MicroPercentRange microPercentRange; |
| 31 | + private final PercentConditionOperator percentConditionOperator; |
| 32 | + private final String seed; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a percent condition for operator BETWEEN. |
| 36 | + * |
| 37 | + * @param microPercent The limit of percentiles to target in micro-percents when using the |
| 38 | + * LESS_OR_EQUAL and GREATER_THAN operators. The value must be in the range [0 and 100000000]. |
| 39 | + * @param percentConditionOperator The choice of percent operator to determine how to compare |
| 40 | + * targets to percent(s). |
| 41 | + * @param seed The seed used when evaluating the hash function to map an instance to a value in |
| 42 | + * the hash space. This is a string which can have 0 - 32 characters and can contain ASCII |
| 43 | + * characters [-_.0-9a-zA-Z].The string is case-sensitive. |
| 44 | + */ |
| 45 | + PercentCondition( |
| 46 | + @Nullable Integer microPercent, |
| 47 | + @NonNull PercentConditionOperator percentConditionOperator, |
| 48 | + @NonNull String seed) { |
| 49 | + checkNotNull(percentConditionOperator, "Percentage operator must not be null."); |
| 50 | + checkArgument(!Strings.isNullOrEmpty(seed), "Seed must not be null or empty."); |
| 51 | + this.microPercent = microPercent != null ? microPercent : 0; |
| 52 | + this.percentConditionOperator = percentConditionOperator; |
| 53 | + this.seed = seed; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a percent condition for operators GREATER_THAN and LESS_OR_EQUAL. |
| 58 | + * |
| 59 | + * @param microPercentRange The micro-percent interval to be used with the BETWEEN operator. |
| 60 | + * @param percentConditionOperator The choice of percent operator to determine how to compare |
| 61 | + * targets to percent(s). |
| 62 | + * @param seed The seed used when evaluating the hash function to map an instance to a value in |
| 63 | + * the hash space. This is a string which can have 0 - 32 characters and can contain ASCII |
| 64 | + * characters [-_.0-9a-zA-Z].The string is case-sensitive. |
| 65 | + */ |
| 66 | + PercentCondition( |
| 67 | + @NonNull MicroPercentRange microPercentRange, |
| 68 | + @NonNull PercentConditionOperator percentConditionOperator, |
| 69 | + String seed) { |
| 70 | + checkNotNull(microPercentRange, "Percent range must not be null."); |
| 71 | + checkNotNull(percentConditionOperator, "Percentage operator must not be null."); |
| 72 | + this.microPercentRange = microPercentRange; |
| 73 | + this.percentConditionOperator = percentConditionOperator; |
| 74 | + this.seed = seed; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new {@link PercentCondition} from API response. |
| 79 | + * |
| 80 | + * @param percentCondition the conditions obtained from server call. |
| 81 | + */ |
| 82 | + PercentCondition(PercentConditionResponse percentCondition) { |
| 83 | + checkArgument( |
| 84 | + !Strings.isNullOrEmpty(percentCondition.getSeed()), "Seed must not be empty or null"); |
| 85 | + this.microPercent = percentCondition.getMicroPercent(); |
| 86 | + this.seed = percentCondition.getSeed(); |
| 87 | + switch (percentCondition.getPercentOperator()) { |
| 88 | + case "BETWEEN": |
| 89 | + this.percentConditionOperator = PercentConditionOperator.BETWEEN; |
| 90 | + break; |
| 91 | + case "GREATER_THAN": |
| 92 | + this.percentConditionOperator = PercentConditionOperator.GREATER_THAN; |
| 93 | + break; |
| 94 | + case "LESS_OR_EQUAL": |
| 95 | + this.percentConditionOperator = PercentConditionOperator.LESS_OR_EQUAL; |
| 96 | + break; |
| 97 | + default: |
| 98 | + this.percentConditionOperator = PercentConditionOperator.UNSPECIFIED; |
| 99 | + } |
| 100 | + checkArgument( |
| 101 | + this.percentConditionOperator != PercentConditionOperator.UNSPECIFIED, |
| 102 | + "Percentage operator is invalid"); |
| 103 | + if (percentCondition.getMicroPercentRange() != null) { |
| 104 | + this.microPercentRange = |
| 105 | + new MicroPercentRange( |
| 106 | + percentCondition.getMicroPercentRange().getMicroPercentLowerBound(), |
| 107 | + percentCondition.getMicroPercentRange().getMicroPercentUpperBound()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the limit of percentiles to target in micro-percents when using the LESS_OR_EQUAL and |
| 113 | + * GREATER_THAN operators. The value must be in the range [0 and 100000000]. |
| 114 | + * |
| 115 | + * @return micro percent. |
| 116 | + */ |
| 117 | + @Nullable |
| 118 | + public int getMicroPercent() { |
| 119 | + return microPercent; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Gets micro-percent interval to be used with the BETWEEN operator. |
| 124 | + * |
| 125 | + * @return micro percent range. |
| 126 | + */ |
| 127 | + @Nullable |
| 128 | + public MicroPercentRange getMicroPercentRange() { |
| 129 | + return microPercentRange; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets choice of percent operator to determine how to compare targets to percent(s). |
| 134 | + * |
| 135 | + * @return operator. |
| 136 | + */ |
| 137 | + @NonNull |
| 138 | + public PercentConditionOperator getPercentConditionOperator() { |
| 139 | + return percentConditionOperator; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * The seed used when evaluating the hash function to map an instance to a value in the hash |
| 144 | + * space. This is a string which can have 0 - 32 characters and can contain ASCII characters |
| 145 | + * [-_.0-9a-zA-Z].The string is case-sensitive. |
| 146 | + * |
| 147 | + * @return seed. |
| 148 | + */ |
| 149 | + @NonNull |
| 150 | + public String getSeed() { |
| 151 | + return seed; |
| 152 | + } |
| 153 | + |
| 154 | + PercentConditionResponse toPercentConditionResponse() { |
| 155 | + PercentConditionResponse percentConditionResponse = new PercentConditionResponse(); |
| 156 | + percentConditionResponse.setMicroPercent(this.microPercent); |
| 157 | + percentConditionResponse.setMicroPercentRange( |
| 158 | + this.microPercentRange.toMicroPercentRangeResponse()); |
| 159 | + percentConditionResponse.setPercentOperator(this.percentConditionOperator.getOperator()); |
| 160 | + percentConditionResponse.setSeed(this.seed); |
| 161 | + return percentConditionResponse; |
| 162 | + } |
| 163 | +} |
0 commit comments