|
| 1 | +package io.reflectoring.featureflags.implementations.contextsensitive; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.security.MessageDigest; |
| 5 | +import java.security.NoSuchAlgorithmException; |
| 6 | +import java.util.Arrays; |
| 7 | + |
| 8 | +public class Feature { |
| 9 | + |
| 10 | + public enum RolloutStrategy { |
| 11 | + GLOBAL, |
| 12 | + PERCENTAGE; |
| 13 | + } |
| 14 | + |
| 15 | + private final RolloutStrategy rolloutStrategy; |
| 16 | + |
| 17 | + private final int percentage; |
| 18 | + private final String value; |
| 19 | + private final String defaultValue; |
| 20 | + |
| 21 | + public Feature(RolloutStrategy rolloutStrategy, String value, String defaultValue, int percentage) { |
| 22 | + this.rolloutStrategy = rolloutStrategy; |
| 23 | + this.percentage = percentage; |
| 24 | + this.value = value; |
| 25 | + this.defaultValue = defaultValue; |
| 26 | + } |
| 27 | + |
| 28 | + public boolean evaluateBoolean(String userId) { |
| 29 | + switch (this.rolloutStrategy) { |
| 30 | + case GLOBAL: |
| 31 | + return this.getBooleanValue(); |
| 32 | + case PERCENTAGE: |
| 33 | + if (percentageHashCode(userId) <= this.percentage) { |
| 34 | + return this.getBooleanValue(); |
| 35 | + } else { |
| 36 | + return this.getBooleanDefaultValue(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return this.getBooleanDefaultValue(); |
| 41 | + } |
| 42 | + |
| 43 | + public Integer evaluateInt(String userId) { |
| 44 | + switch (this.rolloutStrategy) { |
| 45 | + case GLOBAL: |
| 46 | + return this.getIntValue(); |
| 47 | + case PERCENTAGE: |
| 48 | + if (percentageHashCode(userId) <= this.percentage) { |
| 49 | + return this.getIntValue(); |
| 50 | + } else { |
| 51 | + return this.getIntDefaultValue(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return this.getIntDefaultValue(); |
| 56 | + } |
| 57 | + |
| 58 | + double percentageHashCode(String text) { |
| 59 | + try { |
| 60 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 61 | + byte[] encodedhash = digest.digest( |
| 62 | + text.getBytes(StandardCharsets.UTF_8)); |
| 63 | + double INTEGER_RANGE = 1L << 32; |
| 64 | + return (((long) Arrays.hashCode(encodedhash) - Integer.MIN_VALUE) / INTEGER_RANGE) * 100; |
| 65 | + } catch (NoSuchAlgorithmException e) { |
| 66 | + throw new IllegalStateException(e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public RolloutStrategy getTargeting() { |
| 71 | + return rolloutStrategy; |
| 72 | + } |
| 73 | + |
| 74 | + public int getPercentage() { |
| 75 | + return percentage; |
| 76 | + } |
| 77 | + |
| 78 | + public int getIntValue() { |
| 79 | + return Integer.parseInt(this.value); |
| 80 | + } |
| 81 | + |
| 82 | + public int getIntDefaultValue() { |
| 83 | + return Integer.parseInt(this.defaultValue); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public boolean getBooleanValue() { |
| 88 | + return Boolean.parseBoolean(this.value); |
| 89 | + } |
| 90 | + |
| 91 | + public boolean getBooleanDefaultValue() { |
| 92 | + return Boolean.parseBoolean(this.defaultValue); |
| 93 | + } |
| 94 | + |
| 95 | + public String getStringValue() { |
| 96 | + return value; |
| 97 | + } |
| 98 | + |
| 99 | + public String getDefaultValue() { |
| 100 | + return defaultValue; |
| 101 | + } |
| 102 | +} |
0 commit comments