Skip to content

Commit 4c3d66e

Browse files
authored
user profile service map structure (#96)
* Create UserProfileService keys * create user profile class of the interface
1 parent 8e5bb8e commit 4c3d66e

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

core-api/src/main/java/com/optimizely/ab/bucketing/UserProfileService.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.optimizely.ab.bucketing;
1818

19+
import java.util.HashMap;
1920
import java.util.Map;
2021

2122
/**
@@ -25,12 +26,65 @@
2526
*/
2627
public interface UserProfileService {
2728

29+
/**
30+
* A class representing a user's profile.
31+
*/
32+
class UserProfile {
33+
34+
/** A user's ID. */
35+
public final String userId;
36+
/** The bucketing decisions of the user. */
37+
public final Map<String, String> decisions;
38+
39+
/**
40+
* Construct a User Profile instance from explicit components.
41+
* @param userId The ID of the user.
42+
* @param decisions The bucketing decisions of the user.
43+
*/
44+
public UserProfile(String userId, Map<String, String> decisions) {
45+
this.userId = userId;
46+
this.decisions = decisions;
47+
}
48+
49+
/**
50+
* Construct a User Profile instance from a Map.
51+
* @param userProfileMap A {@code Map<String, Object>} containing the properties of the user profile.
52+
*/
53+
@SuppressWarnings("unchecked")
54+
public UserProfile(Map<String, Object> userProfileMap) {
55+
this((String) userProfileMap.get(userIdKey), (Map<String, String>) userProfileMap.get(decisionsKey));
56+
}
57+
58+
/**
59+
* Convert a User Profile instance to a Map.
60+
* @return A map representation of the user profile instance.
61+
*/
62+
Map<String, Object> toMap() {
63+
Map<String, Object> userProfileMap = new HashMap<String, Object>(2);
64+
userProfileMap.put(userIdKey, userId);
65+
userProfileMap.put(decisionsKey, decisions);
66+
return userProfileMap;
67+
}
68+
}
69+
70+
/** The key for the user ID. Returns a String.*/
71+
String userIdKey = "user_id";
72+
/** The key for the decisions Map. Returns a {@code Map<String, String>}.*/
73+
String decisionsKey = "decisions";
74+
2875
/**
2976
* Fetch the user profile map for the user ID.
3077
*
3178
* @param userId The ID of the user whose profile will be retrieved.
3279
* @return a Map representing the user's profile.
33-
* @throws Exception
80+
* The returned {@code Map<String, Object>} of the user profile will have the following structure.
81+
* {
82+
* userIdKey : String userId,
83+
* decisionsKey : {@code Map<String, String>} decisions {
84+
* String experimentId : String variationId
85+
* }
86+
* }
87+
* @throws Exception Passes on whatever exceptions the implementation may throw.
3488
*/
3589
Map<String, Object> lookup(String userId) throws Exception;
3690

0 commit comments

Comments
 (0)