1 package io.getunleash.strategy;
2
3 import io.getunleash.Constraint;
4 import io.getunleash.FeatureEvaluationResult;
5 import io.getunleash.UnleashContext;
6 import io.getunleash.lang.Nullable;
7 import io.getunleash.variant.VariantDefinition;
8 import io.getunleash.variant.VariantUtil;
9 import java.util.List;
10 import java.util.Map;
11
12 public interface Strategy {
13
14     String getName();
15
16     boolean isEnabled(Map<String, String> parameters);
17
18     default FeatureEvaluationResult getResult(
19             Map<String, String> parameters,
20             UnleashContext unleashContext,
21             List<Constraint> constraints,
22             @Nullable List<VariantDefinition> variants) {
23         boolean enabled = isEnabled(parameters, unleashContext, constraints);
24         String strategyStickiness = getStickiness(parameters);
25         return new FeatureEvaluationResult(
26                 enabled,
27                 enabled
28                         ? VariantUtil.selectVariant(
29                                 parameters, variants, unleashContext, strategyStickiness)
30                         : null);
31     }
32
33     /**
34      * Uses the old pre 9.0.0 way of hashing for finding the Variant to return
35      *
36      * @deprecated
37      * @param parameters
38      * @param unleashContext
39      * @param constraints
40      * @param variants
41      * @return
42      */

43     default FeatureEvaluationResult getDeprecatedHashingAlgoResult(
44             Map<String, String> parameters,
45             UnleashContext unleashContext,
46             List<Constraint> constraints,
47             @Nullable List<VariantDefinition> variants) {
48         boolean enabled = isEnabled(parameters, unleashContext, constraints);
49         return new FeatureEvaluationResult(
50                 enabled,
51                 enabled
52                         ? VariantUtil.selectDeprecatedVariantHashingAlgo(
53                                 parameters, variants, unleashContext)
54                         : null);
55     }
56
57     default boolean isEnabled(Map<String, String> parameters, UnleashContext unleashContext) {
58         return isEnabled(parameters);
59     }
60
61     default boolean isEnabled(
62             Map<String, String> parameters,
63             UnleashContext unleashContext,
64             List<Constraint> constraints) {
65         return ConstraintUtil.validate(constraints, unleashContext)
66                 && isEnabled(parameters, unleashContext);
67     }
68
69     default String getStickiness(@Nullable Map<String, String> parameters) {
70         if (parameters != null) {
71             return parameters.getOrDefault("stickiness""default");
72         }
73         return null;
74     }
75 }
76