PercentageOperator extends Operator<String> { @Override public Flag.FlagResultStatus appliesTo(@Nullable String value) throws NumberFormatException { if (StringUtils.isEmpty(value)) { return DISABLED; } final int val = Integer.parseInt(value); if (val == 0) { return DISABLED; } final int asPercentage = val % MAX_PERCENTAGE; return asPercentage >= shift && asPercentage < (percentage + shift) ? Flag.FlagResultStatus.ENABLED : DISABLED; } PercentageOperator(String name, int percentage, int shift); @Override Flag.FlagResultStatus appliesTo(@Nullable String value);  }
@Test public void testSimplePercentageOperator() throws Exception { PercentageOperator percentageOperator = new PercentageOperator("modulo", 10, 0); assertEquals(Flag.FlagResultStatus.ENABLED, percentageOperator.appliesTo("8")); assertEquals(Flag.FlagResultStatus.DISABLED, percentageOperator.appliesTo("12")); }
@Test public void testSimplePercentageWithShiftOperator() throws Exception { PercentageOperator percentageOperator = new PercentageOperator("modulo", 10, 5); assertEquals(Flag.FlagResultStatus.ENABLED, percentageOperator.appliesTo("12")); assertEquals(Flag.FlagResultStatus.DISABLED, percentageOperator.appliesTo("20")); }
@Test public void testPercentageWithoutRegistrationValue() throws Exception { PercentageOperator percentageOperator = new PercentageOperator("modulo", 10, 5); assertEquals(Flag.FlagResultStatus.DISABLED, percentageOperator.appliesTo("0")); }