All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.getunleash.strategy.Strategy Maven / Gradle / Ivy

There is a newer version: 9.2.4
Show 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy