Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,11 @@ private <ValueType> boolean isStickyBucketingEnabledForExperiment(EvaluationCont

private Map<String, Integer> getForcedVariations(EvaluationContext evaluationContext) {
Map<String, Integer> globalForcedVariations = evaluationContext.getGlobal() != null
? evaluationContext.getGlobal().getForcedVariations()
: Collections.emptyMap();
? evaluationContext.getGlobal().getForcedVariations() : null;

Map<String, Integer> userForcedVariations = evaluationContext.getUser() != null
? evaluationContext.getUser().getForcedVariationsMap()
: Collections.emptyMap();
? evaluationContext.getUser().getForcedVariationsMap() : null;

return GrowthBookUtils.mergeMaps(Arrays.asList(globalForcedVariations, userForcedVariations));
return GrowthBookUtils.mergeMaps(globalForcedVariations, userForcedVariations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,12 @@ private <ValueType> FeatureResult<ValueType> cacheResult(String key, FeatureResu
private Map<String, Object> getForcedFeatureValues(EvaluationContext evaluationContext) {
Map<String, Object> globalFeatures = evaluationContext.getGlobal() != null
? evaluationContext.getGlobal().getForcedFeatureValues()
: Collections.emptyMap();
: null;

Map<String, Object> userFeatures = evaluationContext.getUser() != null
? evaluationContext.getUser().getForcedFeatureValues()
: Collections.emptyMap();
: null;

return GrowthBookUtils.mergeMaps(Arrays.asList(globalFeatures, userFeatures));
return GrowthBookUtils.mergeMaps(globalFeatures, userFeatures);
}
}
20 changes: 6 additions & 14 deletions lib/src/main/java/growthbook/sdk/java/util/GrowthBookUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* <b>INTERNAL</b>: Implementation of for internal utility methods to support {@link growthbook.sdk.java.GrowthBook}
*/
Expand Down Expand Up @@ -919,17 +917,11 @@ private static int findVariationIndex(List<VariationMeta> meta, String variation
return -1;
}

public static <K, V> Map<K, V> mergeMaps(List<Map<K, V>> maps) {
return maps.stream()
.filter(Objects::nonNull)
.flatMap(map -> map.entrySet()
.stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(v1, v2) -> v2
)
);

public static <K, V> Map<K, V> mergeMaps(@Nullable Map<K, V> base, @Nullable Map<K, V> overrides) {
if (base == null || base.isEmpty()) return overrides != null ? new HashMap<>(overrides) : Collections.emptyMap();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return new HashMap<>() instead of Collections.emptyMap() for consistency — so the method always returns a mutable map and we prevent a potential UnsupportedOperationException if someone mutates the result in the future.

if (overrides == null || overrides.isEmpty()) return new HashMap<>(base);
Map<K, V> merged = new HashMap<>(base);
merged.putAll(overrides);
return merged;
}
}
Loading