Skip to content

Commit 15d5f12

Browse files
authored
Refactor experiment and events getters (#179)
* remove throws declarations on methods since we never actually throw. Change docs to explain that we send it to the error handler instead * rename `getExperimentOrThrow` to `getExperimentForKey` * rename `getEventTypeOrThrow` to `getEventTypeForName` * move `getExperimentForKey` from Optimizely.java to ProjectConfig.java * move `getEventTypeForName` from Optimizely.java to ProjectConfig.java.
1 parent f55a318 commit 15d5f12

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

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

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.optimizely.ab.config.parser.DefaultConfigParser;
3333
import com.optimizely.ab.error.ErrorHandler;
3434
import com.optimizely.ab.error.NoOpErrorHandler;
35-
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
3635
import com.optimizely.ab.event.EventHandler;
3736
import com.optimizely.ab.event.LogEvent;
3837
import com.optimizely.ab.event.internal.BuildVersionInfo;
@@ -42,7 +41,6 @@
4241
import org.slf4j.Logger;
4342
import org.slf4j.LoggerFactory;
4443

45-
import javax.annotation.CheckForNull;
4644
import javax.annotation.Nonnull;
4745
import javax.annotation.Nullable;
4846
import javax.annotation.concurrent.ThreadSafe;
@@ -131,7 +129,7 @@ Variation activate(@Nonnull String experimentKey,
131129

132130
ProjectConfig currentConfig = getProjectConfig();
133131

134-
Experiment experiment = getExperimentOrThrow(currentConfig, experimentKey);
132+
Experiment experiment = currentConfig.getExperimentForKey(experimentKey, errorHandler);
135133
if (experiment == null) {
136134
// if we're unable to retrieve the associated experiment, return null
137135
logger.info("Not activating user \"{}\" for experiment \"{}\".", userId, experimentKey);
@@ -228,7 +226,7 @@ public void track(@Nonnull String eventName,
228226

229227
ProjectConfig currentConfig = getProjectConfig();
230228

231-
EventType eventType = getEventTypeOrThrow(currentConfig, eventName);
229+
EventType eventType = currentConfig.getEventTypeForName(eventName, errorHandler);
232230
if (eventType == null) {
233231
// if no matching event type could be found, do not dispatch an event
234232
logger.info("Not tracking event \"{}\" for user \"{}\".", eventName, userId);
@@ -638,7 +636,7 @@ Variation getVariation(@Nonnull String experimentKey,
638636

639637
ProjectConfig currentConfig = getProjectConfig();
640638

641-
Experiment experiment = getExperimentOrThrow(currentConfig, experimentKey);
639+
Experiment experiment = currentConfig.getExperimentForKey(experimentKey, errorHandler);
642640
if (experiment == null) {
643641
// if we're unable to retrieve the associated experiment, return null
644642
return null;
@@ -720,64 +718,6 @@ public UserProfileService getUserProfileService() {
720718

721719
//======== Helper methods ========//
722720

723-
/**
724-
* Helper method to retrieve the {@link Experiment} for the given experiment key.
725-
* If {@link RaiseExceptionErrorHandler} is provided, either an experiment is returned, or an exception is thrown.
726-
* If {@link NoOpErrorHandler} is used, either an experiment or {@code null} is returned.
727-
*
728-
* @param projectConfig the current project config
729-
* @param experimentKey the experiment to retrieve from the current project config
730-
* @return the experiment for given experiment key
731-
*
732-
* @throws UnknownExperimentException if there are no experiments in the current project config with the given
733-
* experiment key
734-
*/
735-
private @CheckForNull Experiment getExperimentOrThrow(@Nonnull ProjectConfig projectConfig,
736-
@Nonnull String experimentKey)
737-
throws UnknownExperimentException {
738-
739-
Experiment experiment = projectConfig
740-
.getExperimentKeyMapping()
741-
.get(experimentKey);
742-
743-
// if the given experiment key isn't present in the config, log and potentially throw an exception
744-
if (experiment == null) {
745-
String unknownExperimentError = String.format("Experiment \"%s\" is not in the datafile.", experimentKey);
746-
logger.error(unknownExperimentError);
747-
errorHandler.handleError(new UnknownExperimentException(unknownExperimentError));
748-
}
749-
750-
return experiment;
751-
}
752-
753-
/**
754-
* Helper method to retrieve the {@link EventType} for the given event name.
755-
* If {@link RaiseExceptionErrorHandler} is provided, either an event type is returned, or an exception is thrown.
756-
* If {@link NoOpErrorHandler} is used, either an event type or {@code null} is returned.
757-
*
758-
* @param projectConfig the current project config
759-
* @param eventName the event type to retrieve from the current project config
760-
* @return the event type for the given event name
761-
*
762-
* @throws UnknownEventTypeException if there are no event types in the current project config with the given name
763-
*/
764-
private EventType getEventTypeOrThrow(ProjectConfig projectConfig, String eventName)
765-
throws UnknownEventTypeException {
766-
767-
EventType eventType = projectConfig
768-
.getEventNameMapping()
769-
.get(eventName);
770-
771-
// if the given event name isn't present in the config, log and potentially throw an exception
772-
if (eventType == null) {
773-
String unknownEventTypeError = String.format("Event \"%s\" is not in the datafile.", eventName);
774-
logger.error(unknownEventTypeError);
775-
errorHandler.handleError(new UnknownEventTypeException(unknownEventTypeError));
776-
}
777-
778-
return eventType;
779-
}
780-
781721
/**
782722
* Helper method to verify that the given attributes map contains only keys that are present in the
783723
* {@link ProjectConfig}.

core-api/src/main/java/com/optimizely/ab/config/ProjectConfig.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
package com.optimizely.ab.config;
1818

1919
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20+
import com.optimizely.ab.UnknownEventTypeException;
21+
import com.optimizely.ab.UnknownExperimentException;
2022
import com.optimizely.ab.config.audience.Audience;
2123
import com.optimizely.ab.config.audience.Condition;
24+
import com.optimizely.ab.error.ErrorHandler;
25+
import com.optimizely.ab.error.NoOpErrorHandler;
26+
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
2227
import org.slf4j.Logger;
2328
import org.slf4j.LoggerFactory;
2429

30+
import javax.annotation.CheckForNull;
2531
import javax.annotation.Nonnull;
2632
import javax.annotation.Nullable;
2733
import javax.annotation.concurrent.Immutable;
@@ -210,6 +216,62 @@ public ProjectConfig(String accountId,
210216
}
211217
}
212218

219+
/**
220+
* Helper method to retrieve the {@link Experiment} for the given experiment key.
221+
* If {@link RaiseExceptionErrorHandler} is provided, either an experiment is returned,
222+
* or an exception is sent to the error handler
223+
* if there are no experiments in the project config with the given experiment key.
224+
* If {@link NoOpErrorHandler} is used, either an experiment or {@code null} is returned.
225+
*
226+
* @param experimentKey the experiment to retrieve from the current project config
227+
* @param errorHandler the error handler to send exceptions to
228+
* @return the experiment for given experiment key
229+
*/
230+
@CheckForNull
231+
public Experiment getExperimentForKey(@Nonnull String experimentKey,
232+
@Nonnull ErrorHandler errorHandler) {
233+
234+
Experiment experiment =
235+
getExperimentKeyMapping()
236+
.get(experimentKey);
237+
238+
// if the given experiment key isn't present in the config, log an exception to the error handler
239+
if (experiment == null) {
240+
String unknownExperimentError = String.format("Experiment \"%s\" is not in the datafile.", experimentKey);
241+
logger.error(unknownExperimentError);
242+
errorHandler.handleError(new UnknownExperimentException(unknownExperimentError));
243+
}
244+
245+
return experiment;
246+
}
247+
248+
/**
249+
* Helper method to retrieve the {@link EventType} for the given event name.
250+
* If {@link RaiseExceptionErrorHandler} is provided, either an event type is returned,
251+
* or an exception is sent to the error handler if there are no event types in the project config with the given name.
252+
* If {@link NoOpErrorHandler} is used, either an event type or {@code null} is returned.
253+
*
254+
* @param eventName the event type to retrieve from the current project config
255+
* @param errorHandler the error handler to send exceptions to
256+
* @return the event type for the given event name
257+
*/
258+
public @CheckForNull EventType getEventTypeForName(String eventName, ErrorHandler errorHandler) {
259+
260+
EventType eventType =
261+
getEventNameMapping()
262+
.get(eventName);
263+
264+
// if the given event name isn't present in the config, log an exception to the error handler
265+
if (eventType == null) {
266+
String unknownEventTypeError = String.format("Event \"%s\" is not in the datafile.", eventName);
267+
logger.error(unknownEventTypeError);
268+
errorHandler.handleError(new UnknownEventTypeException(unknownEventTypeError));
269+
}
270+
271+
return eventType;
272+
}
273+
274+
213275
public @Nullable Experiment getExperimentForVariationId(String variationId) {
214276
return this.variationIdToExperimentMapping.get(variationId);
215277
}

0 commit comments

Comments
 (0)