Skip to content

Commit 651ccdc

Browse files
authored
feat(decide): clone user-context before calling optimizely decide (#417)
1 parent f46dfe4 commit 651ccdc

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

core-api/src/main/java/com/optimizely/ab/OptimizelyUserContext.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2020, Optimizely and contributors
3+
* Copyright 2020-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -61,13 +61,17 @@ public String getUserId() {
6161
}
6262

6363
public Map<String, Object> getAttributes() {
64-
return new HashMap<String, Object>(attributes);
64+
return attributes;
6565
}
6666

6767
public Optimizely getOptimizely() {
6868
return optimizely;
6969
}
7070

71+
public OptimizelyUserContext copy() {
72+
return new OptimizelyUserContext(optimizely, userId, attributes);
73+
}
74+
7175
/**
7276
* Set an attribute for a given key.
7377
*
@@ -89,7 +93,7 @@ public void setAttribute(@Nonnull String key, @Nullable Object value) {
8993
*/
9094
public OptimizelyDecision decide(@Nonnull String key,
9195
@Nonnull List<OptimizelyDecideOption> options) {
92-
return optimizely.decide(this, key, options);
96+
return optimizely.decide(copy(), key, options);
9397
}
9498

9599
/**
@@ -114,7 +118,7 @@ public OptimizelyDecision decide(@Nonnull String key) {
114118
*/
115119
public Map<String, OptimizelyDecision> decideForKeys(@Nonnull List<String> keys,
116120
@Nonnull List<OptimizelyDecideOption> options) {
117-
return optimizely.decideForKeys(this, keys, options);
121+
return optimizely.decideForKeys(copy(), keys, options);
118122
}
119123

120124
/**
@@ -134,7 +138,7 @@ public Map<String, OptimizelyDecision> decideForKeys(@Nonnull List<String> keys)
134138
* @return All decision results mapped by flag keys.
135139
*/
136140
public Map<String, OptimizelyDecision> decideAll(@Nonnull List<OptimizelyDecideOption> options) {
137-
return optimizely.decideAll(this, options);
141+
return optimizely.decideAll(copy(), options);
138142
}
139143

140144
/**

core-api/src/main/java/com/optimizely/ab/optimizelydecision/OptimizelyDecision.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2020, Optimizely and contributors
3+
* Copyright 2020-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -26,23 +26,44 @@
2626
import java.util.List;
2727

2828
public class OptimizelyDecision {
29+
/**
30+
* The variation key of the decision. This value will be null when decision making fails.
31+
*/
2932
@Nullable
3033
private final String variationKey;
3134

35+
/**
36+
* The boolean value indicating if the flag is enabled or not.
37+
*/
3238
private final boolean enabled;
3339

40+
/**
41+
* The collection of variables associated with the decision.
42+
*/
3443
@Nonnull
3544
private final OptimizelyJSON variables;
3645

46+
/**
47+
* The rule key of the decision.
48+
*/
3749
@Nullable
3850
private final String ruleKey;
3951

52+
/**
53+
* The flag key for which the decision has been made for.
54+
*/
4055
@Nonnull
4156
private final String flagKey;
4257

58+
/**
59+
* A copy of the user context for which the decision has been made for.
60+
*/
4361
@Nonnull
4462
private final OptimizelyUserContext userContext;
4563

64+
/**
65+
* An array of error/info messages describing why the decision has been made.
66+
*/
4667
@Nonnull
4768
private List<String> reasons;
4869

0 commit comments

Comments
 (0)