Skip to content

Commit bccb687

Browse files
authored
integrate user profile service (#98)
* add UserProfileService to Optimizely * integrate user profile service into decision service * remove old UserProfile.java * add new UserProfile.java * add Decision.java * we no longer clean the user profile for the developers * Remove ProjectValidationUtils.java and UserProfileUtilsTest.java
1 parent f4c1743 commit bccb687

File tree

14 files changed

+515
-606
lines changed

14 files changed

+515
-606
lines changed

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
/**
2-
*
3-
* Copyright 2016-2017, Optimizely and contributors
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
1+
/****************************************************************************
2+
* Copyright 2016-2017, 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+
***************************************************************************/
1716
package com.optimizely.ab;
1817

1918
import com.optimizely.ab.annotations.VisibleForTesting;
2019
import com.optimizely.ab.bucketing.Bucketer;
2120
import com.optimizely.ab.bucketing.DecisionService;
22-
import com.optimizely.ab.bucketing.UserProfile;
21+
import com.optimizely.ab.bucketing.UserProfileService;
2322
import com.optimizely.ab.config.Attribute;
2423
import com.optimizely.ab.config.EventType;
2524
import com.optimizely.ab.config.Experiment;
@@ -40,7 +39,6 @@
4039
import com.optimizely.ab.event.internal.payload.Event.ClientEngine;
4140
import com.optimizely.ab.internal.EventTagUtils;
4241
import com.optimizely.ab.internal.ReservedEventKey;
43-
import com.optimizely.ab.internal.UserProfileUtils;
4442
import com.optimizely.ab.notification.NotificationBroadcaster;
4543
import com.optimizely.ab.notification.NotificationListener;
4644
import org.slf4j.Logger;
@@ -92,26 +90,26 @@ public class Optimizely {
9290
@VisibleForTesting final EventHandler eventHandler;
9391
@VisibleForTesting final ErrorHandler errorHandler;
9492
@VisibleForTesting final NotificationBroadcaster notificationBroadcaster = new NotificationBroadcaster();
95-
@Nullable private final UserProfile userProfile;
93+
@Nullable private final UserProfileService userProfileService;
9694

9795
private Optimizely(@Nonnull ProjectConfig projectConfig,
9896
@Nonnull DecisionService decisionService,
9997
@Nonnull EventHandler eventHandler,
10098
@Nonnull EventBuilder eventBuilder,
10199
@Nonnull ErrorHandler errorHandler,
102-
@Nullable UserProfile userProfile) {
100+
@Nullable UserProfileService userProfileService) {
103101
this.projectConfig = projectConfig;
104102
this.decisionService = decisionService;
105103
this.eventHandler = eventHandler;
106104
this.eventBuilder = eventBuilder;
107105
this.errorHandler = errorHandler;
108-
this.userProfile = userProfile;
106+
this.userProfileService = userProfileService;
109107
}
110108

111109
// Do work here that should be done once per Optimizely lifecycle
112110
@VisibleForTesting
113111
void initialize() {
114-
UserProfileUtils.cleanUserProfiles(userProfile, projectConfig);
112+
115113
}
116114

117115
//======== activate calls ========//
@@ -506,11 +504,10 @@ private static ProjectConfig getProjectConfig(String datafile) throws ConfigPars
506504
}
507505

508506
@Nullable
509-
public UserProfile getUserProfile() {
510-
return userProfile;
507+
public UserProfileService getUserProfileService() {
508+
return userProfileService;
511509
}
512510

513-
514511
//======== Notification listeners ========//
515512

516513
/**
@@ -709,7 +706,7 @@ public static class Builder {
709706
private ClientEngine clientEngine;
710707
private String clientVersion;
711708
private ProjectConfig projectConfig;
712-
private UserProfile userProfile;
709+
private UserProfileService userProfileService;
713710

714711
public Builder(@Nonnull String datafile,
715712
@Nonnull EventHandler eventHandler) {
@@ -732,8 +729,8 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
732729
return this;
733730
}
734731

735-
public Builder withUserProfile(UserProfile userProfile) {
736-
this.userProfile = userProfile;
732+
public Builder withUserProfileService(UserProfileService userProfileService) {
733+
this.userProfileService = userProfileService;
737734
return this;
738735
}
739736

@@ -775,9 +772,6 @@ public Optimizely build() throws ConfigParseException {
775772
clientVersion = BuildVersionInfo.VERSION;
776773
}
777774

778-
if (decisionService == null) {
779-
decisionService = new DecisionService(bucketer, projectConfig, userProfile);
780-
}
781775

782776
if (eventBuilder == null) {
783777
eventBuilder = new EventBuilderV2(clientEngine, clientVersion);
@@ -787,7 +781,11 @@ public Optimizely build() throws ConfigParseException {
787781
errorHandler = new NoOpErrorHandler();
788782
}
789783

790-
Optimizely optimizely = new Optimizely(projectConfig, decisionService, eventHandler, eventBuilder, errorHandler, userProfile);
784+
if (decisionService == null) {
785+
decisionService = new DecisionService(bucketer, errorHandler, projectConfig, userProfileService);
786+
}
787+
788+
Optimizely optimizely = new Optimizely(projectConfig, decisionService, eventHandler, eventBuilder, errorHandler, userProfileService);
791789
optimizely.initialize();
792790
return optimizely;
793791
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public class OptimizelyRuntimeException extends RuntimeException {
2323

2424
public OptimizelyRuntimeException() { }
2525

26+
public OptimizelyRuntimeException(Exception exception) {
27+
super(exception);
28+
}
29+
2630
public OptimizelyRuntimeException(String message) {
2731
super(message);
2832
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/****************************************************************************
2+
* Copyright 2017, 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+
package com.optimizely.ab.bucketing;
17+
18+
import javax.annotation.Nonnull;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
/**
23+
* A class representing a stored decision.
24+
*/
25+
public class Decision {
26+
27+
/** The ID of the {@link com.optimizely.ab.config.Variation} the user was bucketed into. */
28+
@Nonnull public String variationId;
29+
30+
/**
31+
* Initialize a Decision object.
32+
* @param variationId The ID of the variation the user was bucketed into.
33+
*/
34+
public Decision(@Nonnull String variationId) {
35+
this.variationId = variationId;
36+
}
37+
38+
@Override
39+
public boolean equals(Object o) {
40+
if (this == o) return true;
41+
if (o == null || getClass() != o.getClass()) return false;
42+
43+
Decision decision = (Decision) o;
44+
45+
return variationId.equals(decision.variationId);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return variationId.hashCode();
51+
}
52+
53+
public Map<String, String> toMap() {
54+
Map<String, String> decisionMap = new HashMap<String, String>(1);
55+
decisionMap.put(UserProfileService.variationIdKey, variationId);
56+
return decisionMap;
57+
}
58+
}

0 commit comments

Comments
 (0)