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 SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic IntegerchooseVariation(@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 BooleangetForcedBooleanValueFromUrl(String featureKey, URL url) Evaluate a forced boolean value from a URL.static DoublegetForcedDoubleValueFromUrl(String featureKey, URL url) Evaluate a forced float value from a URL.static FloatgetForcedFloatValueFromUrl(String featureKey, URL url) Evaluate a forced float value from a URL.static IntegergetForcedIntegerValueFromUrl(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 StringgetForcedStringValueFromUrl(String featureKey, URL url) Evaluate a forced string value from a URL.static HashAttributeAndHashValuegetHashAttribute(String attr, String fallbackAttribute, com.google.gson.JsonObject attributes) Method for get hash value by identifier.static IntegergetQueryStringOverride(String id, String urlString, Integer numberOfVariations) This checks if an experiment variation is being forced via a URL query string.static IntegergetQueryStringOverride(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 StringgetStickyBucketExperimentKey(String experimentKey, Integer experimentBucketVersion) Method to get Experiment key from cachestatic StickyBucketVariationgetStickyBucketVariation(EvaluationContext context, String experimentKey, String experimentHashAttribute, String experimentFallbackAttribute, Integer experimentBucketVersion, Integer minExperimentBucketVersion, List<VariationMeta> meta) Method to getStickyBucketVariation: variation and versionIsBlocked.static FloatHashes a string to a float between 0 and 1, or null if the hash version is unsupported.static BooleaninNameSpace(String userId, Namespace namespace) This checks if a userId is within an experiment namespace or not.static BooleaninRange(Float n, BucketRange range) Determines if a number n is within the provided range.static BooleanisFilteredOut(List<Filter> filters, com.google.gson.JsonObject attributes) Evaluates whether a user should be filtered out based on the given filters.static BooleanisIncludedInRollout(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 voidrefreshStickyBuckets(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- 
GrowthBookUtilspublic GrowthBookUtils()
 
- 
- 
Method Details- 
hashHashes 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 string
- hashVersion- The hash version
- seed- 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.
 
- 
inNameSpaceThis checks if a userId is within an experiment namespace or not.- Parameters:
- userId- The user identifier
- namespace- Namespace to check the user identifier against
- Returns:
- whether the user is in the namespace
 
- 
chooseVariationpublic 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- hash
- bucketRanges- list of- BucketRange
- Returns:
- index of the BucketRangelist to assign
 
- 
getEqualWeightsReturns 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 identifier
- urlString- the desired page URL as a string
- numberOfVariations- 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 identifier
- url- the desired page URL
- numberOfVariations- the number of variations
- Returns:
- integer or null
 
- 
getForcedBooleanValueFromUrlEvaluate 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
 
- 
getForcedStringValueFromUrlEvaluate 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
 
- 
getForcedFloatValueFromUrlEvaluate 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
 
- 
getForcedDoubleValueFromUrlEvaluate 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
 
- 
getForcedIntegerValueFromUrlEvaluate 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
- 
getBucketRangespublic 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 variations
- coverage- 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
 
- 
inRangeDetermines 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 range
- range-- BucketRange
- Returns:
- whether to include this hash value is within range. Returns false if either arguments are null.
 
- 
isFilteredOutEvaluates 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 of- Filterobjects that define filtering criteria.
- attributes- A- JsonObjectcontaining user attributes.
- Returns:
- trueif the user is filtered out, otherwise- false.
 
- 
isIncludedInRolloutpublic 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- JsonObject
- seed- String
- hashAttribute- String
- fallbackAttribute- String
- range- BucketRange
- coverage- Float
- hashVersion- Integer
- Returns:
- Boolean - check if user is included
 
- 
refreshStickyBucketspublic 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-- GBContextthat contains sticky bucketing attributes and sticky bucket service
- featuresDataModel- A string representing the features data model.
- attributeOverrides-- JsonObjectcontaining attribute overrides.
 
- 
getStickyBucketAttributespublic 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-- GBContextThe context containing user attributes.
- featuresDataModel- A string representing the features data model.
- attributeOverrides- A- JsonObjectcontaining attribute overrides.
- Returns:
- Map<String, String>A map of sticky bucket attributes.
 
- 
getStickyBucketAssignmentspublic 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.
 
- 
getStickyBucketVariationpublic 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- String
- experimentHashAttribute- String (nullable)
- experimentFallbackAttribute- String (nullable)
- experimentBucketVersion- Integer (nullable)
- minExperimentBucketVersion- Integer (nullable)
- meta-- List<- VariationMeta- >(nullable)
- Returns:
- StickyBucketVariation
 
- 
getStickyBucketExperimentKeypublic static String getStickyBucketExperimentKey(String experimentKey, @Nullable Integer experimentBucketVersion) Method to get Experiment key from cache- Parameters:
- experimentKey- String
- experimentBucketVersion- Integer
- Returns:
- key in String format
 
- 
generateStickyBucketAssignmentDocpublic 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- String
- attributeValue- String
- assignments-- Map<String, String>
- Returns:
- GeneratedStickyBucketAssignmentDocModel
 
- 
getHashAttributepublic 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- String
- fallbackAttribute- String
- attributes- JsonObject
- Returns:
- HashAttributeAndHashValue
 
- 
mergeMaps
 
-