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

no.finn.unleash.strategy.FlexibleRolloutStrategy Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
package no.finn.unleash.strategy;

import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

import no.finn.unleash.UnleashContext;

public class FlexibleRolloutStrategy implements Strategy {
    protected static final String PERCENTAGE = "rollout";
    protected static final String GROUP_ID = "groupId";

    private Supplier randomGenerator;

    public FlexibleRolloutStrategy() {
        this.randomGenerator = () ->  Math.random() * 100 + "";
    }

    public FlexibleRolloutStrategy(Supplier randomGenerator) {
        this.randomGenerator = randomGenerator;
    }

    @Override
    public String getName() {
        return "flexibleRollout";
    }

    @Override
    public boolean isEnabled(Map parameters) {
        return false;
    }

    private Optional resolveStickiness(String stickiness, UnleashContext context) {
        switch (stickiness) {
            case "userId": return context.getUserId();
            case "sessionId": return context.getSessionId();
            case "random": return Optional.of(randomGenerator.get());
            default:
                String value = context.getUserId()
                        .orElse(context.getSessionId()
                        .orElse(this.randomGenerator.get()));
                return Optional.of(value);
        }
    }

    @Override
    public boolean isEnabled(Map parameters, UnleashContext unleashContext) {
        final String stickiness = getStickiness(parameters);
        final Optional stickinessId = resolveStickiness(stickiness, unleashContext);
        final int percentage = StrategyUtils.getPercentage(parameters.get(PERCENTAGE));
        final String groupId = Optional.ofNullable(parameters.get(GROUP_ID)).orElse("");

        if(stickinessId.isPresent()) {
            final int normalizedUserId = StrategyUtils.getNormalizedNumber(stickinessId.get(), groupId);
            return percentage > 0 && normalizedUserId <= percentage;
        } else {
            return false;
        }
    }

    private String getStickiness(Map parameters) {
        Optional stickiness = Optional.ofNullable(parameters.get("stickiness"));
        return stickiness.orElse("default");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy