io.getunleash.strategy.FlexibleRolloutStrategy 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.UnleashContext;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
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() * 10000 + "";
}
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());
case "default":
String value =
context.getUserId()
.orElse(context.getSessionId().orElse(this.randomGenerator.get()));
return Optional.of(value);
default:
return context.getByName(stickiness);
}
}
@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 = parameters.getOrDefault(GROUP_ID, "");
return stickinessId
.map(stick -> StrategyUtils.getNormalizedNumber(stick, groupId, 0))
.map(norm -> percentage > 0 && norm <= percentage)
.orElse(false);
}
}