1818import com .optimizely .ab .annotations .VisibleForTesting ;
1919import com .optimizely .ab .config .*;
2020import java .util .*;
21- import java .util .function .Function ;
22- import java .util .stream .Collectors ;
2321
2422public class OptimizelyConfigService {
2523
@@ -56,7 +54,11 @@ Map<String, List<FeatureVariable>> generateFeatureKeyToVariablesMap() {
5654 if (featureFlags == null ) {
5755 return Collections .emptyMap ();
5856 }
59- return featureFlags .stream ().collect (Collectors .toMap (FeatureFlag ::getKey , featureFlag -> featureFlag .getVariables ()));
57+ Map <String , List <FeatureVariable >> featureVariableIdMap = new HashMap <>();
58+ for (FeatureFlag featureFlag : featureFlags ) {
59+ featureVariableIdMap .put (featureFlag .getKey (), featureFlag .getVariables ());
60+ }
61+ return featureVariableIdMap ;
6062 }
6163
6264 @ VisibleForTesting
@@ -68,28 +70,36 @@ String getExperimentFeatureKey(String experimentId) {
6870 @ VisibleForTesting
6971 Map <String , OptimizelyExperiment > getExperimentsMap () {
7072 List <Experiment > experiments = projectConfig .getExperiments ();
71- if (experiments == null ) {
73+ if (experiments == null ) {
7274 return Collections .emptyMap ();
7375 }
74- return experiments .stream ().collect (Collectors .toMap (Experiment ::getKey , experiment -> new OptimizelyExperiment (
75- experiment .getId (),
76- experiment .getKey (),
77- getVariationsMap (experiment .getVariations (), experiment .getId ())
78- )));
76+ Map <String , OptimizelyExperiment > featureExperimentMap = new HashMap <>();
77+ for (Experiment experiment : experiments ) {
78+ featureExperimentMap .put (experiment .getKey (), new OptimizelyExperiment (
79+ experiment .getId (),
80+ experiment .getKey (),
81+ getVariationsMap (experiment .getVariations (), experiment .getId ())
82+ ));
83+ }
84+ return featureExperimentMap ;
7985 }
8086
8187 @ VisibleForTesting
8288 Map <String , OptimizelyVariation > getVariationsMap (List <Variation > variations , String experimentId ) {
83- if (variations == null ) {
89+ if (variations == null ) {
8490 return Collections .emptyMap ();
8591 }
8692 Boolean isFeatureExperiment = this .getExperimentFeatureKey (experimentId ) != null ;
87- return variations .stream ().collect (Collectors .toMap (Variation ::getKey , variation -> new OptimizelyVariation (
88- variation .getId (),
89- variation .getKey (),
90- isFeatureExperiment ? variation .getFeatureEnabled () : null ,
91- getMergedVariablesMap (variation , experimentId )
92- )));
93+ Map <String , OptimizelyVariation > variationKeyMap = new HashMap <>();
94+ for (Variation variation : variations ) {
95+ variationKeyMap .put (variation .getKey (), new OptimizelyVariation (
96+ variation .getId (),
97+ variation .getKey (),
98+ isFeatureExperiment ? variation .getFeatureEnabled () : null ,
99+ getMergedVariablesMap (variation , experimentId )
100+ ));
101+ }
102+ return variationKeyMap ;
93103 }
94104
95105 /**
@@ -115,43 +125,58 @@ Map<String, OptimizelyVariable> getMergedVariablesMap(Variation variation, Strin
115125 return Collections .emptyMap ();
116126 }
117127
118- return featureVariables .stream ().collect (Collectors .toMap (FeatureVariable ::getKey , featureVariable -> new OptimizelyVariable (
119- featureVariable .getId (),
120- featureVariable .getKey (),
121- featureVariable .getType ().getVariableType ().toLowerCase (),
122- variation .getFeatureEnabled () && tempVariableIdMap .get (featureVariable .getId ()) != null
123- ? tempVariableIdMap .get (featureVariable .getId ()).getValue ()
124- : featureVariable .getDefaultValue ()
125- )));
128+ Map <String , OptimizelyVariable > featureVariableKeyMap = new HashMap <>();
129+ for (FeatureVariable featureVariable : featureVariables ) {
130+ featureVariableKeyMap .put (featureVariable .getKey (), new OptimizelyVariable (
131+ featureVariable .getId (),
132+ featureVariable .getKey (),
133+ featureVariable .getType ().getVariableType ().toLowerCase (),
134+ variation .getFeatureEnabled () && tempVariableIdMap .get (featureVariable .getId ()) != null
135+ ? tempVariableIdMap .get (featureVariable .getId ()).getValue ()
136+ : featureVariable .getDefaultValue ()
137+ ));
138+ }
139+ return featureVariableKeyMap ;
126140 }
127141 return Collections .emptyMap ();
128142 }
129143
130144 @ VisibleForTesting
131145 Map <String , OptimizelyVariable > getFeatureVariableUsageInstanceMap (List <FeatureVariableUsageInstance > featureVariableUsageInstances ) {
132- if (featureVariableUsageInstances == null ) {
146+ if (featureVariableUsageInstances == null ) {
133147 return Collections .emptyMap ();
134148 }
135- return featureVariableUsageInstances .stream ().collect (Collectors .toMap (FeatureVariableUsageInstance ::getId , variable -> new OptimizelyVariable (
136- variable .getId (),
137- null ,
138- null ,
139- variable .getValue ()
140- )));
149+
150+ Map <String , OptimizelyVariable > featureVariableIdMap = new HashMap <>();
151+ for (FeatureVariableUsageInstance featureVariableUsageInstance : featureVariableUsageInstances ) {
152+ featureVariableIdMap .put (featureVariableUsageInstance .getId (), new OptimizelyVariable (
153+ featureVariableUsageInstance .getId (),
154+ null ,
155+ null ,
156+ featureVariableUsageInstance .getValue ()
157+ ));
158+ }
159+
160+ return featureVariableIdMap ;
141161 }
142162
143163 @ VisibleForTesting
144164 Map <String , OptimizelyFeature > getFeaturesMap (Map <String , OptimizelyExperiment > allExperimentsMap ) {
145165 List <FeatureFlag > featureFlags = projectConfig .getFeatureFlags ();
146- if (featureFlags == null ) {
166+ if (featureFlags == null ) {
147167 return Collections .emptyMap ();
148168 }
149- return featureFlags .stream ().collect (Collectors .toMap (FeatureFlag ::getKey , featureFlag -> new OptimizelyFeature (
150- featureFlag .getId (),
151- featureFlag .getKey (),
152- getExperimentsMapForFeature (featureFlag .getExperimentIds (), allExperimentsMap ),
153- getFeatureVariablesMap (featureFlag .getVariables ())
154- )));
169+
170+ Map <String , OptimizelyFeature > optimizelyFeatureKeyMap = new HashMap <>();
171+ for (FeatureFlag featureFlag : featureFlags ) {
172+ optimizelyFeatureKeyMap .put (featureFlag .getKey (), new OptimizelyFeature (
173+ featureFlag .getId (),
174+ featureFlag .getKey (),
175+ getExperimentsMapForFeature (featureFlag .getExperimentIds (), allExperimentsMap ),
176+ getFeatureVariablesMap (featureFlag .getVariables ())
177+ ));
178+ }
179+ return optimizelyFeatureKeyMap ;
155180 }
156181
157182 @ VisibleForTesting
@@ -160,23 +185,31 @@ Map<String, OptimizelyExperiment> getExperimentsMapForFeature(List<String> exper
160185 return Collections .emptyMap ();
161186 }
162187
163- List <String > experimentKeys = experimentIds .stream ()
164- .map (experimentId -> projectConfig .getExperimentIdMapping ().get (experimentId ).getKey ())
165- .collect (Collectors .toList ());
188+ Map <String , OptimizelyExperiment > optimizelyExperimentKeyMap = new HashMap <>();
189+ for (String experimentId : experimentIds ) {
190+ String experimentKey = projectConfig .getExperimentIdMapping ().get (experimentId ).getKey ();
191+ optimizelyExperimentKeyMap .put (experimentKey , allExperimentsMap .get (experimentKey ));
192+ }
166193
167- return experimentKeys . stream (). collect ( Collectors . toMap ( Function . identity (), key -> allExperimentsMap . get ( key ))) ;
194+ return optimizelyExperimentKeyMap ;
168195 }
169196
170197 @ VisibleForTesting
171198 Map <String , OptimizelyVariable > getFeatureVariablesMap (List <FeatureVariable > featureVariables ) {
172199 if (featureVariables == null ) {
173200 return Collections .emptyMap ();
174201 }
175- return featureVariables .stream ().collect (Collectors .toMap (FeatureVariable ::getKey , featureVariable -> new OptimizelyVariable (
176- featureVariable .getId (),
177- featureVariable .getKey (),
178- featureVariable .getType ().getVariableType ().toLowerCase (),
179- featureVariable .getDefaultValue ()
180- )));
202+
203+ Map <String , OptimizelyVariable > featureVariableKeyMap = new HashMap <>();
204+ for (FeatureVariable featureVariable : featureVariables ) {
205+ featureVariableKeyMap .put (featureVariable .getKey (), new OptimizelyVariable (
206+ featureVariable .getId (),
207+ featureVariable .getKey (),
208+ featureVariable .getType ().getVariableType ().toLowerCase (),
209+ featureVariable .getDefaultValue ()
210+ ));
211+ }
212+
213+ return featureVariableKeyMap ;
181214 }
182215}
0 commit comments