Skip to content

Commit

Permalink
In-code documentation was added (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: Bohdan Akimenko <[email protected]>
  • Loading branch information
vazarkevych and Bohdan-Kim authored Jun 7, 2024
1 parent f773be4 commit 777ac5e
Show file tree
Hide file tree
Showing 23 changed files with 565 additions and 81 deletions.
9 changes: 7 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.freefair.gradle:lombok-plugin:8.6"
}
Expand Down Expand Up @@ -52,8 +57,8 @@ dependencies {
// Adds getter, setter and builder boilerplate
// https://projectlombok.org/
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'

// logging
implementation 'org.slf4j:slf4j-api:2.0.7'
Expand Down
7 changes: 7 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/BucketRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Builder;
import lombok.Data;
import org.apache.commons.math3.util.Precision;

import java.util.Objects;

/**
Expand All @@ -24,6 +25,12 @@ public class BucketRange {
Float rangeStart;
Float rangeEnd;

/**
* This method help to convert BucketRange object from JsonElement
*
* @param jsonElement json element
* @return BucketRange object
*/
static BucketRange fromJson(JsonElement jsonElement) {
JsonArray array = (JsonArray) jsonElement;
float start = array.get(0).getAsFloat();
Expand Down
42 changes: 42 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/Experiment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import javax.annotation.Nullable;
import java.util.ArrayList;

Expand Down Expand Up @@ -57,6 +58,9 @@ public class Experiment<ValueType> {
@Nullable
ArrayList<ParentCondition> parentConditions;

/**
* A tuple that contains the namespace identifier, plus a range of coverage for the experiment
*/
@Nullable
@Deprecated
Namespace namespace;
Expand All @@ -68,41 +72,79 @@ public class Experiment<ValueType> {

/**
* What user attribute should be used to assign variations (defaults to `id`)
* All users included in the experiment will be forced into the specific variation index
*/
@Builder.Default
String hashAttribute = "id";

//new properties v0.4.0
/**
* The hash version to use (default to 1)
*/
@Nullable
Integer hashVersion;

/**
* Array of ranges, one per variation
*/
@Nullable
ArrayList<BucketRange> ranges;

/**
* Meta info about the variations
*/
@Nullable
@SerializedName("meta")
ArrayList<VariationMeta> meta;

/**
* Array of filters to apply
*/
@Nullable
ArrayList<Filter> filters;

/**
* The hash seed to use
*/
@Nullable
String seed;

/**
* Human-readable name for the experiment
*/
@Nullable
String name;

/**
* Identifier of the current experiment phase
*/
@Nullable
String phase;

/**
* When using sticky bucketing, can be used as a fallback to assign variations
*/
@Nullable
String fallbackAttribute;

/**
* If true, sticky bucketing will be disabled for this experiment.
* (Note: sticky bucketing is only available
* if a StickyBucketingService is provided in the Context)
*/
@Nullable
Boolean disableStickyBucketing;

/**
* The sticky bucket version number that can be used to force a re-bucketing
* of users (default to 0)
*/
@Nullable
Integer bucketVersion;

/**
* Any users with a sticky bucket version less than this will be excluded from the experiment
*/
@Nullable
Integer minBucketVersion;

Expand Down
11 changes: 10 additions & 1 deletion lib/src/main/java/growthbook/sdk/java/ExperimentEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,7 +19,15 @@ class ExperimentEvaluator implements IExperimentEvaluator {
private final ConditionEvaluator conditionEvaluator = new ConditionEvaluator();
private final GrowthBookJsonUtils jsonUtils = GrowthBookJsonUtils.getInstance();


/**
* Takes Context & Experiment & returns Experiment Result
*
* @param experiment Experiment
* @param context GBContext
* @param featureId String(can be null)
* @param attributeOverrides JsonObject
* @return ExperimentResult
*/
@Override
public <ValueType> ExperimentResult<ValueType> evaluateExperiment(Experiment<ValueType> experiment,
GBContext context,
Expand Down
62 changes: 50 additions & 12 deletions lib/src/main/java/growthbook/sdk/java/ExperimentResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;

import javax.annotation.Nullable;

/**
Expand All @@ -13,38 +14,75 @@
*/
@Data
public class ExperimentResult<ValueType> {
/**
* The array value of the assigned variation
*/
@Nullable
ValueType value;

/**
* The array index of the assigned variation
*/
@Nullable
Integer variationId;

/**
* Whether the user is part of the experiment
*/
Boolean inExperiment;

/**
* The user attribute used to assign a variation
*/
@Nullable
String hashAttribute;

/**
* The value of that attribute
*/
@Nullable
String hashValue;

/**
* The id of the feature (if any) that the experiment came from
*/
@Nullable
String featureId;

/**
* If a hash was used to assign a variation
*/
Boolean hashUsed;

//new properties v0.4.0
/**
* The unique key for the assigned variation
*/
@Nullable
String key;

/**
* The human-readable name of the assigned variation
*/
@Nullable
String name;

/**
* The hash value used to assign a variation (float from 0 to 1)
*/
@Nullable
Float bucket;

/**
* Used for holdout groups
*/
@Nullable
@SerializedName("passthrough")
Boolean passThrough;

/**
* If sticky bucketing was used to assign a variation
*/
@Nullable
Boolean stickyBucketUsed;

Expand All @@ -65,18 +103,18 @@ public class ExperimentResult<ValueType> {
*/
@Builder
public ExperimentResult(
@Nullable ValueType value,
@Nullable Integer variationId,
Boolean inExperiment,
@Nullable String hashAttribute,
@Nullable String hashValue,
@Nullable String featureId,
Boolean hashUsed,
@Nullable String key,
@Nullable String name,
@Nullable Float bucket,
@Nullable Boolean passThrough,
@Nullable Boolean stickyBucketUsed
@Nullable ValueType value,
@Nullable Integer variationId,
Boolean inExperiment,
@Nullable String hashAttribute,
@Nullable String hashValue,
@Nullable String featureId,
Boolean hashUsed,
@Nullable String key,
@Nullable String name,
@Nullable Float bucket,
@Nullable Boolean passThrough,
@Nullable Boolean stickyBucketUsed
) {
this.value = value;
this.variationId = variationId;
Expand Down
26 changes: 8 additions & 18 deletions lib/src/main/java/growthbook/sdk/java/Feature.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package growthbook.sdk.java;

import lombok.Data;

import javax.annotation.Nullable;
import java.util.ArrayList;

Expand All @@ -12,29 +14,17 @@
*
* @param <ValueType> value type for the feature
*/
@Data
public class Feature<ValueType> {

@Nullable
private final ArrayList<FeatureRule<ValueType>> rules = new ArrayList<>();

private final Object defaultValue = null;

/**
* The default value for a feature evaluation
*
* @return value of the feature
* Array of Rule objects that determine when and how the defaultValue gets overridden
*/
public Object getDefaultValue() {
return this.defaultValue;
}
@Nullable
private final ArrayList<FeatureRule<ValueType>> rules = new ArrayList<>();

/**
* Returns the rules for evaluating the feature
*
* @return rules list
* The default value (should use null if not specified)
*/
@Nullable
public ArrayList<FeatureRule<ValueType>> getRules() {
return this.rules;
}
private final Object defaultValue = null;
}
12 changes: 12 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/FeatureEvalContext.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package growthbook.sdk.java;

import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

/**
* Model consist already evaluated features
*/
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class FeatureEvalContext {
/**
* Unique feature identifier
*/
private String id;

/**
* Collection of unique feature identifier that used for handle recursion
* in evaluate feature method
*/
private Set<String> evaluatedFeatures;
}
8 changes: 4 additions & 4 deletions lib/src/main/java/growthbook/sdk/java/FeatureEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import java.util.Map;

/**
* <b>INTERNAL</b>: Implementation of feature evaluation
* <p>
* Feature Evaluator Class
* Takes Context and Feature Key
* <b>INTERNAL</b>: Implementation of feature evaluation.
* Takes Context and Feature Key.
* Returns Calculated Feature Result against that key
*/
@Slf4j
Expand Down Expand Up @@ -358,6 +356,8 @@ public <ValueType> FeatureResult<ValueType> evaluateFeature(
}
return experimentFeatureResult;
}
} else {
continue;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package growthbook.sdk.java;

/**
* Enum that used in strategy for building url
*/
public enum FeatureRefreshStrategy {
STALE_WHILE_REVALIDATE,
SERVER_SENT_EVENTS
Expand Down
Loading

0 comments on commit 777ac5e

Please sign in to comment.