Package growthbook.sdk.java.util
Class GrowthBookUtils
java.lang.Object
growthbook.sdk.java.util.GrowthBookUtils
INTERNAL: Implementation of for internal utility methods to support
GrowthBook
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Integer
chooseVariation
(@NotNull Float n, ArrayList<BucketRange> bucketRanges) Given a hash and bucket ranges, assign one of the bucket ranges.generateStickyBucketAssignmentDoc
(Map<String, StickyAssignmentsDocument> stickyBucketAssignmentDocs, String attributeName, String attributeValue, Map<String, String> assignments) Method for generating a Sticky Bucket Assignment document.static ArrayList<BucketRange>
getBucketRanges
(Integer numberOfVariations, @NotNull Float coverage, ArrayList<Float> weights) This converts and experiment's coverage and variation weights into an array of bucket ranges.getEqualWeights
(Integer numberOfVariations) Returns an array of floats with numVariations items that are all equal and sum to 1.static Boolean
getForcedBooleanValueFromUrl
(String featureKey, URL url) Evaluate a forced boolean value from a URL.static Double
getForcedDoubleValueFromUrl
(String featureKey, URL url) Evaluate a forced float value from a URL.static Float
getForcedFloatValueFromUrl
(String featureKey, URL url) Evaluate a forced float value from a URL.static Integer
getForcedIntegerValueFromUrl
(String featureKey, URL url) Evaluate a forced integer value from a URL.static <ValueType>
ValueTypegetForcedSerializableValueFromUrl
(String featureKey, URL url, Class<ValueType> valueTypeClass, com.google.gson.Gson gson) static String
getForcedStringValueFromUrl
(String featureKey, URL url) Evaluate a forced string value from a URL.static HashAttributeAndHashValue
getHashAttribute
(String attr, String fallbackAttribute, com.google.gson.JsonObject attributes) Method for get hash value by identifier.static Integer
getQueryStringOverride
(String id, String urlString, Integer numberOfVariations) This checks if an experiment variation is being forced via a URL query string.static Integer
getQueryStringOverride
(String id, URL url, Integer numberOfVariations) This checks if an experiment variation is being forced via a URL query string.getStickyBucketAssignments
(EvaluationContext context, String expHashAttribute, String expFallbackAttribute) Method to get actual Sticky Bucket assignments.getStickyBucketAttributes
(GBContext context, String featuresDataModel, com.google.gson.JsonObject attributeOverrides) Supportive method to get attribute values from the context.static String
getStickyBucketExperimentKey
(String experimentKey, Integer experimentBucketVersion) Method to get Experiment key from cachestatic StickyBucketVariation
getStickyBucketVariation
(EvaluationContext context, String experimentKey, String experimentHashAttribute, String experimentFallbackAttribute, Integer experimentBucketVersion, Integer minExperimentBucketVersion, List<VariationMeta> meta) Method to getStickyBucketVariation
: variation and versionIsBlocked.static Float
Hashes a string to a float between 0 and 1, or null if the hash version is unsupported.static Boolean
inNameSpace
(String userId, Namespace namespace) This checks if a userId is within an experiment namespace or not.static Boolean
inRange
(Float n, BucketRange range) Determines if a number n is within the provided range.static Boolean
isFilteredOut
(List<Filter> filters, com.google.gson.JsonObject attributes) Evaluates whether a user should be filtered out based on the given filters.static Boolean
isIncludedInRollout
(com.google.gson.JsonObject attributes, String seed, String hashAttribute, String fallbackAttribute, BucketRange range, Float coverage, Integer hashVersion) Determines if the user is part of a gradual feature rollout.static <K,
V> Map<K, V> static void
refreshStickyBuckets
(GBContext context, String featuresDataModel, com.google.gson.JsonObject attributeOverrides) Retrieves cached assignments and sets them in the context's Sticky Bucket Assignments documents.
-
Constructor Details
-
GrowthBookUtils
public GrowthBookUtils()
-
-
Method Details
-
hash
Hashes a string to a float between 0 and 1, or null if the hash version is unsupported. Uses the simple Fowler–Noll–Vo algorithm, specifically fnv32a.- Parameters:
stringValue
- Input stringhashVersion
- The hash versionseed
- A seed value that can be used instead of the experiment key for hashing- Returns:
- hashed float value or null if the hash version is unsupported.
-
inNameSpace
This checks if a userId is within an experiment namespace or not.- Parameters:
userId
- The user identifiernamespace
- Namespace to check the user identifier against- Returns:
- whether the user is in the namespace
-
chooseVariation
public static Integer chooseVariation(@NotNull @NotNull Float n, ArrayList<BucketRange> bucketRanges) Given a hash and bucket ranges, assign one of the bucket ranges. Returns -1 if none can be found- Parameters:
n
- hashbucketRanges
- list ofBucketRange
- Returns:
- index of the
BucketRange
list to assign
-
getEqualWeights
Returns an array of floats with numVariations items that are all equal and sum to 1. For example, getEqualWeights(2) would return [0.5, 0.5]- Parameters:
numberOfVariations
- The number of variations you would like- Returns:
- A list of variations
-
getQueryStringOverride
@Nullable public static Integer getQueryStringOverride(String id, String urlString, Integer numberOfVariations) This checks if an experiment variation is being forced via a URL query string. This may not be applicable for all SDKs (e.g. mobile).As an example, if the id is my-test and url is ..., it would return 1.
Returns null if any of these are true:
- There is no query string
- The id is not a key in the query string
- The variation is not an integer
- The variation is less than 0 or greater than or equal to numVariations
- Parameters:
id
- the identifierurlString
- the desired page URL as a stringnumberOfVariations
- the number of variations- Returns:
- integer or null
-
getQueryStringOverride
@Nullable public static Integer getQueryStringOverride(String id, URL url, Integer numberOfVariations) This checks if an experiment variation is being forced via a URL query string. This may not be applicable for all SDKs (e.g. mobile).As an example, if the id is my-test and url is ..., it would return 1.
Returns null if any of these are true:
- There is no query string
- The id is not a key in the query string
- The variation is not an integer
- The variation is less than 0 or greater than or equal to numVariations
- Parameters:
id
- the identifierurl
- the desired page URLnumberOfVariations
- the number of variations- Returns:
- integer or null
-
getForcedBooleanValueFromUrl
Evaluate a forced boolean value from a URL. If the provided key is not present in the URL, return null.- Parameters:
featureKey
- feature ID/key (not prefixed with gb~)url
- Page URL to evaluate for forced feature values- Returns:
- value or null
-
getForcedStringValueFromUrl
Evaluate a forced string value from a URL. If the provided key is not present in the URL, return null.- Parameters:
featureKey
- feature ID/key (not prefixed with gb~)url
- Page URL to evaluate for forced feature values- Returns:
- value or null
-
getForcedFloatValueFromUrl
Evaluate a forced float value from a URL. If the provided key is not present in the URL, return null.- Parameters:
featureKey
- feature ID/key (not prefixed with gb~)url
- Page URL to evaluate for forced feature values- Returns:
- value or null
-
getForcedDoubleValueFromUrl
Evaluate a forced float value from a URL. If the provided key is not present in the URL, return null.- Parameters:
featureKey
- feature ID/key (not prefixed with gb~)url
- Page URL to evaluate for forced feature values- Returns:
- value or null
-
getForcedIntegerValueFromUrl
Evaluate a forced integer value from a URL. If the provided key is not present in the URL, return null.- Parameters:
featureKey
- feature ID/key (not prefixed with gb~)url
- Page URL to evaluate for forced feature values- Returns:
- value or null
-
getForcedSerializableValueFromUrl
-
getBucketRanges
public static ArrayList<BucketRange> getBucketRanges(Integer numberOfVariations, @NotNull @NotNull Float coverage, @Nullable ArrayList<Float> weights) This converts and experiment's coverage and variation weights into an array of bucket ranges. Defaults to equal weights if the sum of the weight is not equal to 1 (rounded).- Parameters:
numberOfVariations
- number of variationscoverage
- the amount that should be covered, e.g. 0.5 is 50%weights
- List of weights. If these do not sum to 1, equal weights will be applied.- Returns:
- list of
BucketRange
-
inRange
Determines if a number n is within the provided range. Verifies if the provided float is within the lower (inclusive) and upper (exclusive) bounds of the providedBucketRange
.- Parameters:
n
- Float value to check if it's in rangerange
-BucketRange
- Returns:
- whether to include this hash value is within range. Returns false if either arguments are null.
-
isFilteredOut
Evaluates whether a user should be filtered out based on the given filters.This method processes each filter in the provided list and performs the following steps:
- Iterates through the list of filters.
- Retrieves the hash attribute and hash value from the user attributes.
- If the hash value is empty, the user is filtered out (returns
true
). - Computes a hash-based bucket value for the user.
- If none of the specified bucket ranges match, the user is filtered out.
false
, meaning the user is not filtered out.- Parameters:
filters
- The list ofFilter
objects that define filtering criteria.attributes
- AJsonObject
containing user attributes.- Returns:
true
if the user is filtered out, otherwisefalse
.
-
isIncludedInRollout
public static Boolean isIncludedInRollout(com.google.gson.JsonObject attributes, String seed, String hashAttribute, String fallbackAttribute, @Nullable BucketRange range, @Nullable Float coverage, @Nullable Integer hashVersion) Determines if the user is part of a gradual feature rollout.- Either coverage or range are required. If both are null, return true immediately
- If range is null and coverage is zero, return false immediately. This catches an edge case where the bucket is zero and users are let through when they shouldn't be
- If hashValue is empty, return false immediately
- Parameters:
attributes
- JsonObjectseed
- StringhashAttribute
- StringfallbackAttribute
- Stringrange
- BucketRangecoverage
- FloathashVersion
- Integer- Returns:
- Boolean - check if user is included
-
refreshStickyBuckets
public static void refreshStickyBuckets(GBContext context, String featuresDataModel, com.google.gson.JsonObject attributeOverrides) Retrieves cached assignments and sets them in the context's Sticky Bucket Assignments documents.- Parameters:
context
-GBContext
that contains sticky bucketing attributes and sticky bucket servicefeaturesDataModel
- A string representing the features data model.attributeOverrides
-JsonObject
containing attribute overrides.
-
getStickyBucketAttributes
public static Map<String,String> getStickyBucketAttributes(GBContext context, String featuresDataModel, com.google.gson.JsonObject attributeOverrides) Supportive method to get attribute values from the context.- Parameters:
context
-GBContext
The context containing user attributes.featuresDataModel
- A string representing the features data model.attributeOverrides
- AJsonObject
containing attribute overrides.- Returns:
Map<String, String>
A map of sticky bucket attributes.
-
getStickyBucketAssignments
public static Map<String,String> getStickyBucketAssignments(EvaluationContext context, @Nullable String expHashAttribute, @Nullable String expFallbackAttribute) Method to get actual Sticky Bucket assignments. Also, this method handles if assignments belong to the user.- Parameters:
context
-EvaluationContext
expHashAttribute
- String (nullable)expFallbackAttribute
- String (nullable)- Returns:
Map<String, String>
containing sticky bucket assignments.
-
getStickyBucketVariation
public static StickyBucketVariation getStickyBucketVariation(EvaluationContext context, String experimentKey, @Nullable String experimentHashAttribute, @Nullable String experimentFallbackAttribute, @Nullable Integer experimentBucketVersion, @Nullable Integer minExperimentBucketVersion, @Nullable List<VariationMeta> meta) Method to getStickyBucketVariation
: variation and versionIsBlocked.- Parameters:
context
-EvaluationContext
experimentKey
- StringexperimentHashAttribute
- String (nullable)experimentFallbackAttribute
- String (nullable)experimentBucketVersion
- Integer (nullable)minExperimentBucketVersion
- Integer (nullable)meta
-List<
VariationMeta
>
(nullable)- Returns:
StickyBucketVariation
-
getStickyBucketExperimentKey
public static String getStickyBucketExperimentKey(String experimentKey, @Nullable Integer experimentBucketVersion) Method to get Experiment key from cache- Parameters:
experimentKey
- StringexperimentBucketVersion
- Integer- Returns:
- key in String format
-
generateStickyBucketAssignmentDoc
public static GeneratedStickyBucketAssignmentDocModel generateStickyBucketAssignmentDoc(Map<String, StickyAssignmentsDocument> stickyBucketAssignmentDocs, String attributeName, String attributeValue, Map<String, String> assignments) Method for generating a Sticky Bucket Assignment document.- Parameters:
stickyBucketAssignmentDocs
-Map<String, StickyAssignmentsDocument>
attributeName
- StringattributeValue
- Stringassignments
-Map<String, String>
- Returns:
GeneratedStickyBucketAssignmentDocModel
-
getHashAttribute
public static HashAttributeAndHashValue getHashAttribute(@Nullable String attr, @Nullable String fallbackAttribute, com.google.gson.JsonObject attributes) Method for get hash value by identifier. User attribute used for hashing, defaulting to id if not set.- Parameters:
attr
- StringfallbackAttribute
- Stringattributes
- JsonObject- Returns:
HashAttributeAndHashValue
-
mergeMaps
-