io.getunleash.strategy.Strategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
The newest version!
package io.getunleash.strategy;
import io.getunleash.Constraint;
import io.getunleash.FeatureEvaluationResult;
import io.getunleash.UnleashContext;
import io.getunleash.lang.Nullable;
import io.getunleash.variant.VariantDefinition;
import io.getunleash.variant.VariantUtil;
import java.util.List;
import java.util.Map;
public interface Strategy {
String getName();
boolean isEnabled(Map parameters);
default FeatureEvaluationResult getResult(
Map parameters,
UnleashContext unleashContext,
List constraints,
@Nullable List variants) {
boolean enabled = isEnabled(parameters, unleashContext, constraints);
String strategyStickiness = getStickiness(parameters);
return new FeatureEvaluationResult(
enabled,
enabled
? VariantUtil.selectVariant(
parameters, variants, unleashContext, strategyStickiness)
: null);
}
/**
* Uses the old pre 9.0.0 way of hashing for finding the Variant to return
*
* @deprecated
* @param parameters
* @param unleashContext
* @param constraints
* @param variants
* @return
*/
default FeatureEvaluationResult getDeprecatedHashingAlgoResult(
Map parameters,
UnleashContext unleashContext,
List constraints,
@Nullable List variants) {
boolean enabled = isEnabled(parameters, unleashContext, constraints);
return new FeatureEvaluationResult(
enabled,
enabled
? VariantUtil.selectDeprecatedVariantHashingAlgo(
parameters, variants, unleashContext)
: null);
}
default boolean isEnabled(Map parameters, UnleashContext unleashContext) {
return isEnabled(parameters);
}
default boolean isEnabled(
Map parameters,
UnleashContext unleashContext,
List constraints) {
return ConstraintUtil.validate(constraints, unleashContext)
&& isEnabled(parameters, unleashContext);
}
default String getStickiness(@Nullable Map parameters) {
if (parameters != null) {
return parameters.getOrDefault("stickiness", "default");
}
return null;
}
}