|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2019, Optimizely, Inc. and contributors * |
| 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.optimizely.ab.notification; |
| 18 | + |
| 19 | + |
| 20 | +import com.optimizely.ab.bucketing.FeatureDecision; |
| 21 | +import com.optimizely.ab.config.FeatureVariable; |
| 22 | + |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import javax.annotation.Nullable; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +public class DecisionNotification { |
| 29 | + protected String type; |
| 30 | + protected String userId; |
| 31 | + protected Map<String, ?> attributes; |
| 32 | + protected Map<String, ?> decisionInfo; |
| 33 | + |
| 34 | + protected DecisionNotification() { |
| 35 | + } |
| 36 | + |
| 37 | + protected DecisionNotification(@Nonnull String type, |
| 38 | + @Nonnull String userId, |
| 39 | + @Nullable Map<String, ?> attributes, |
| 40 | + @Nonnull Map<String, ?> decisionInfo) { |
| 41 | + this.type = type; |
| 42 | + this.userId = userId; |
| 43 | + if (attributes == null) { |
| 44 | + attributes = new HashMap<>(); |
| 45 | + } |
| 46 | + this.attributes = attributes; |
| 47 | + this.decisionInfo = decisionInfo; |
| 48 | + } |
| 49 | + |
| 50 | + public String getType() { |
| 51 | + return type; |
| 52 | + } |
| 53 | + |
| 54 | + public String getUserId() { |
| 55 | + return userId; |
| 56 | + } |
| 57 | + |
| 58 | + public Map<String, ?> getAttributes() { |
| 59 | + return attributes; |
| 60 | + } |
| 61 | + |
| 62 | + public Map<String, ?> getDecisionInfo() { |
| 63 | + return decisionInfo; |
| 64 | + } |
| 65 | + |
| 66 | + public static FeatureVariableDecisionNotificationBuilder newFeatureVariableBuilder() { |
| 67 | + return new FeatureVariableDecisionNotificationBuilder(); |
| 68 | + } |
| 69 | + |
| 70 | + public static class FeatureVariableDecisionNotificationBuilder { |
| 71 | + |
| 72 | + public static final String FEATURE_KEY = "feature_key"; |
| 73 | + public static final String FEATURE_ENABLED = "feature_enabled"; |
| 74 | + public static final String SOURCE = "source"; |
| 75 | + public static final String SOURCE_EXPERIMENT_KEY = "source_experiment_key"; |
| 76 | + public static final String SOURCE_VARIATION_KEY = "source_variation_key"; |
| 77 | + public static final String VARIABLE_KEY = "variable_key"; |
| 78 | + public static final String VARIABLE_TYPE = "variable_type"; |
| 79 | + public static final String VARIABLE_VALUE = "variable_value"; |
| 80 | + |
| 81 | + private String featureKey; |
| 82 | + private Boolean featureEnabled; |
| 83 | + private FeatureDecision featureDecision; |
| 84 | + private String variableKey; |
| 85 | + private FeatureVariable.VariableType variableType; |
| 86 | + private Object variableValue; |
| 87 | + private String userId; |
| 88 | + private Map<String, ?> attributes; |
| 89 | + private Map<String, Object> decisionInfo; |
| 90 | + |
| 91 | + protected FeatureVariableDecisionNotificationBuilder() { |
| 92 | + } |
| 93 | + |
| 94 | + public FeatureVariableDecisionNotificationBuilder withUserId(String userId) { |
| 95 | + this.userId = userId; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public FeatureVariableDecisionNotificationBuilder withAttributes(Map<String, ?> attributes) { |
| 100 | + this.attributes = attributes; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + public FeatureVariableDecisionNotificationBuilder withFeatureKey(String featureKey) { |
| 105 | + this.featureKey = featureKey; |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public FeatureVariableDecisionNotificationBuilder withFeatureEnabled(boolean featureEnabled) { |
| 110 | + this.featureEnabled = featureEnabled; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + public FeatureVariableDecisionNotificationBuilder withFeatureDecision(FeatureDecision featureDecision) { |
| 115 | + this.featureDecision = featureDecision; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public FeatureVariableDecisionNotificationBuilder withVariableKey(String variableKey) { |
| 120 | + this.variableKey = variableKey; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + public FeatureVariableDecisionNotificationBuilder withVariableType(FeatureVariable.VariableType variableType) { |
| 125 | + this.variableType = variableType; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public FeatureVariableDecisionNotificationBuilder withVariableValue(Object variableValue) { |
| 130 | + this.variableValue = variableValue; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public DecisionNotification build() { |
| 135 | + decisionInfo = new HashMap<>(); |
| 136 | + decisionInfo.put(FEATURE_KEY, featureKey); |
| 137 | + decisionInfo.put(FEATURE_ENABLED, featureEnabled); |
| 138 | + decisionInfo.put(VARIABLE_KEY, variableKey); |
| 139 | + decisionInfo.put(VARIABLE_TYPE, variableType); |
| 140 | + decisionInfo.put(VARIABLE_VALUE, variableValue); |
| 141 | + if (featureDecision != null && featureDecision.decisionSource != null && FeatureDecision.DecisionSource.EXPERIMENT.equals(featureDecision.decisionSource)) { |
| 142 | + decisionInfo.put(SOURCE_EXPERIMENT_KEY, featureDecision.experiment.getKey()); |
| 143 | + decisionInfo.put(SOURCE_VARIATION_KEY, featureDecision.variation.getKey()); |
| 144 | + decisionInfo.put(SOURCE, featureDecision.decisionSource); |
| 145 | + } else { |
| 146 | + decisionInfo.put(SOURCE_EXPERIMENT_KEY, null); |
| 147 | + decisionInfo.put(SOURCE_VARIATION_KEY, null); |
| 148 | + decisionInfo.put(SOURCE, FeatureDecision.DecisionSource.ROLLOUT); |
| 149 | + } |
| 150 | + |
| 151 | + return new DecisionNotification( |
| 152 | + NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE.toString(), |
| 153 | + userId, |
| 154 | + attributes, |
| 155 | + decisionInfo); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments