1616 */
1717package com .optimizely .ab ;
1818
19+ import com .optimizely .ab .bucketing .OptimizelyForcedDecisionKey ;
1920import com .optimizely .ab .config .Variation ;
2021import com .optimizely .ab .optimizelydecision .*;
2122import org .slf4j .Logger ;
2627import java .util .*;
2728
2829public class OptimizelyUserContext {
29- static class ForcedDecision {
30- private String flagKey ;
31- private String ruleKey ;
32- private String variationKey ;
33-
34- ForcedDecision (@ Nonnull String flagKey , String ruleKey , @ Nonnull String variationKey ) {
35- this .flagKey = flagKey ;
36- this .ruleKey = ruleKey ;
37- this .variationKey = variationKey ;
38- }
39-
40- public String getFlagKey () { return flagKey ; }
41- public String getRuleKey () { return ruleKey ; }
42- public String getVariationKey () { return variationKey ; }
43- }
4430
45- // flagKeys mapped to ruleKeys mapped to forcedDecisions
46- Map <String , Map <String , ForcedDecision >> forcedDecisionsMap = new HashMap <>();
47- Map <String , ForcedDecision > forcedDecisionsMapWithNoRuleKey = new HashMap <>();
31+ // OptimizelyForcedDecisionsKey mapped to variationKeys
32+ Map <String , String > forcedDecisionsMap = new HashMap <>();
4833
4934 @ Nonnull
5035 private final String userId ;
@@ -214,27 +199,8 @@ public Boolean setForcedDecision(@Nonnull String flagKey, String ruleKey, @Nonnu
214199 return false ;
215200 }
216201
217- if (ruleKey == null ) {
218- // If the ruleKey is null, we will populate/update the appropriate map
219- if (forcedDecisionsMapWithNoRuleKey .get (flagKey ) != null ) {
220- forcedDecisionsMapWithNoRuleKey .get (flagKey ).variationKey = variationKey ;
221- } else {
222- forcedDecisionsMapWithNoRuleKey .put (flagKey , new ForcedDecision (flagKey , null , variationKey ));
223- }
224- } else {
225- // If the flagKey and ruleKey are already present, set the updated variationKey
226- if (forcedDecisionsMap .containsKey (flagKey )) {
227- if (forcedDecisionsMap .get (flagKey ).containsKey (ruleKey )) {
228- forcedDecisionsMap .get (flagKey ).get (ruleKey ).variationKey = variationKey ;
229- } else {
230- forcedDecisionsMap .get (flagKey ).put (ruleKey , new ForcedDecision (flagKey , ruleKey , variationKey ));
231- }
232- } else {
233- Map <String , ForcedDecision > forcedDecision = new HashMap <>();
234- forcedDecision .put (ruleKey , new ForcedDecision (flagKey , ruleKey , variationKey ));
235- forcedDecisionsMap .put (flagKey , forcedDecision );
236- }
237- }
202+ OptimizelyForcedDecisionKey key = new OptimizelyForcedDecisionKey (flagKey , ruleKey );
203+ forcedDecisionsMap .put (key .toString (), variationKey );
238204
239205 return true ;
240206 }
@@ -272,18 +238,9 @@ public String getForcedDecision(@Nonnull String flagKey, String ruleKey) {
272238 * @return Returns a variationKey relating to the found forced decision, otherwise null
273239 */
274240 public String findForcedDecision (@ Nonnull String flagKey , String ruleKey ) {
275- String variationKey = null ;
276- if (ruleKey != null ) {
277- if (forcedDecisionsMap .size () > 0 && forcedDecisionsMap .containsKey (flagKey )) {
278- if (forcedDecisionsMap .get (flagKey ).containsKey (ruleKey )) {
279- variationKey = forcedDecisionsMap .get (flagKey ).get (ruleKey ).getVariationKey ();
280- }
281- }
282- } else {
283- if (forcedDecisionsMapWithNoRuleKey .size () > 0 && forcedDecisionsMapWithNoRuleKey .containsKey (flagKey )) {
284- variationKey = forcedDecisionsMapWithNoRuleKey .get (flagKey ).getVariationKey ();
285- }
286- }
241+ String variationKey ;
242+ OptimizelyForcedDecisionKey lookupKey = new OptimizelyForcedDecisionKey (flagKey , ruleKey );
243+ variationKey = forcedDecisionsMap .get (lookupKey .toString ());
287244 return variationKey ;
288245 }
289246
@@ -309,27 +266,12 @@ public boolean removeForcedDecision(@Nonnull String flagKey, String ruleKey) {
309266 logger .error ("Optimizely SDK not ready." );
310267 return false ;
311268 }
312- if (ruleKey != null ) {
313- try {
314- ForcedDecision result = forcedDecisionsMap .get (flagKey ).remove (ruleKey );
315- if (result != null ) {
316- if (forcedDecisionsMap .get (flagKey ).size () == 0 ) {
317- forcedDecisionsMap .remove (flagKey );
318- }
319- return true ;
320- }
321- } catch (Exception e ) {
322- logger .error ("Forced Decision does not exist to remove - " + e );
323- }
324- } else {
325- try {
326- ForcedDecision result = forcedDecisionsMapWithNoRuleKey .remove (flagKey );
327- if (result != null ) {
328- return true ;
329- }
330- } catch (Exception e ) {
331- logger .error ("Forced Decision does not exist to remove - " + e );
332- }
269+
270+ OptimizelyForcedDecisionKey removeKey = new OptimizelyForcedDecisionKey (flagKey , ruleKey );
271+ try {
272+ return forcedDecisionsMap .remove (removeKey .toString ()) != null ? true : false ;
273+ } catch (Exception e ) {
274+ logger .error ("Forced Decision does not exist to remove - " + e );
333275 }
334276
335277 return false ;
@@ -347,7 +289,6 @@ public boolean removeAllForcedDecisions() {
347289 }
348290 // Clear both maps for with and without ruleKey
349291 forcedDecisionsMap .clear ();
350- forcedDecisionsMapWithNoRuleKey .clear ();
351292 return true ;
352293 }
353294
0 commit comments