@Test public void testNewLimiter() { State stateWithRateLimitDeprecatedKeys = new State(); stateWithRateLimitDeprecatedKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_TYPE_KEY, BaseLimiterType.RATE_BASED); stateWithRateLimitDeprecatedKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY_DEP, "10"); stateWithRateLimitDeprecatedKeys .setProp(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY_DEP, TimeUnit.MINUTES); Limiter rateLimiterFromDeprecatedKeys = DefaultLimiterFactory.newLimiter(stateWithRateLimitDeprecatedKeys); Assert.assertTrue(rateLimiterFromDeprecatedKeys instanceof RateBasedLimiter); Assert.assertTrue(stateWithRateLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY)); Assert .assertFalse(stateWithRateLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY_DEP)); Assert.assertTrue( stateWithRateLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)); Assert.assertFalse( stateWithRateLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY_DEP)); State stateWithTimeLimitDeprecatedKeys = new State(); stateWithTimeLimitDeprecatedKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_TYPE_KEY, BaseLimiterType.TIME_BASED); stateWithTimeLimitDeprecatedKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_KEY_DEP, "10"); stateWithTimeLimitDeprecatedKeys .setProp(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY_DEP, TimeUnit.MINUTES); Limiter timeLimiterFromDeprecatedKeys = DefaultLimiterFactory.newLimiter(stateWithTimeLimitDeprecatedKeys); Assert.assertTrue(timeLimiterFromDeprecatedKeys instanceof TimeBasedLimiter); Assert.assertTrue(stateWithTimeLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_KEY)); Assert .assertFalse(stateWithTimeLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_KEY_DEP)); Assert.assertTrue( stateWithTimeLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)); Assert.assertFalse( stateWithTimeLimitDeprecatedKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY_DEP)); State stateWithNewKeys = new State(); stateWithNewKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_TYPE_KEY, BaseLimiterType.RATE_BASED); stateWithNewKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY_DEP, "10"); stateWithNewKeys.setProp(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY_DEP, TimeUnit.MINUTES); Limiter rateLimiterFromNewKeys = DefaultLimiterFactory.newLimiter(stateWithNewKeys); Assert.assertTrue(rateLimiterFromNewKeys instanceof RateBasedLimiter); Assert.assertTrue(stateWithNewKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY)); Assert.assertFalse(stateWithNewKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_KEY_DEP)); Assert.assertTrue(stateWithNewKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)); Assert.assertFalse(stateWithNewKeys.contains(DefaultLimiterFactory.EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY_DEP)); }
public static Limiter newLimiter(State state) { state = convertDeprecatedConfigs(state); Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TYPE_KEY), String.format("Missing configuration property %s for the Limiter type", EXTRACT_LIMIT_TYPE_KEY)); BaseLimiterType type = BaseLimiterType.forName(state.getProp(EXTRACT_LIMIT_TYPE_KEY)); switch (type) { case RATE_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_RATE_LIMIT_KEY)); int rateLimit = Integer.parseInt(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)) { TimeUnit rateTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new RateBasedLimiter(rateLimit, rateTimeUnit); } return new RateBasedLimiter(rateLimit); case TIME_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TIME_LIMIT_KEY)); long timeLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)) { TimeUnit timeTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new TimeBasedLimiter(timeLimit, timeTimeUnit); } return new TimeBasedLimiter(timeLimit); case COUNT_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); long countLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); return new CountBasedLimiter(countLimit); case POOL_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_POOL_SIZE_KEY)); int poolSize = Integer.parseInt(state.getProp(EXTRACT_LIMIT_POOL_SIZE_KEY)); return new PoolBasedLimiter(poolSize); default: throw new IllegalArgumentException("Unrecognized Limiter type: " + type.toString()); } }
DefaultLimiterFactory { public static Limiter newLimiter(State state) { state = convertDeprecatedConfigs(state); Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TYPE_KEY), String.format("Missing configuration property %s for the Limiter type", EXTRACT_LIMIT_TYPE_KEY)); BaseLimiterType type = BaseLimiterType.forName(state.getProp(EXTRACT_LIMIT_TYPE_KEY)); switch (type) { case RATE_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_RATE_LIMIT_KEY)); int rateLimit = Integer.parseInt(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)) { TimeUnit rateTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new RateBasedLimiter(rateLimit, rateTimeUnit); } return new RateBasedLimiter(rateLimit); case TIME_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TIME_LIMIT_KEY)); long timeLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)) { TimeUnit timeTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new TimeBasedLimiter(timeLimit, timeTimeUnit); } return new TimeBasedLimiter(timeLimit); case COUNT_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); long countLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); return new CountBasedLimiter(countLimit); case POOL_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_POOL_SIZE_KEY)); int poolSize = Integer.parseInt(state.getProp(EXTRACT_LIMIT_POOL_SIZE_KEY)); return new PoolBasedLimiter(poolSize); default: throw new IllegalArgumentException("Unrecognized Limiter type: " + type.toString()); } } }
DefaultLimiterFactory { public static Limiter newLimiter(State state) { state = convertDeprecatedConfigs(state); Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TYPE_KEY), String.format("Missing configuration property %s for the Limiter type", EXTRACT_LIMIT_TYPE_KEY)); BaseLimiterType type = BaseLimiterType.forName(state.getProp(EXTRACT_LIMIT_TYPE_KEY)); switch (type) { case RATE_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_RATE_LIMIT_KEY)); int rateLimit = Integer.parseInt(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)) { TimeUnit rateTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new RateBasedLimiter(rateLimit, rateTimeUnit); } return new RateBasedLimiter(rateLimit); case TIME_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TIME_LIMIT_KEY)); long timeLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)) { TimeUnit timeTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new TimeBasedLimiter(timeLimit, timeTimeUnit); } return new TimeBasedLimiter(timeLimit); case COUNT_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); long countLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); return new CountBasedLimiter(countLimit); case POOL_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_POOL_SIZE_KEY)); int poolSize = Integer.parseInt(state.getProp(EXTRACT_LIMIT_POOL_SIZE_KEY)); return new PoolBasedLimiter(poolSize); default: throw new IllegalArgumentException("Unrecognized Limiter type: " + type.toString()); } }  }
DefaultLimiterFactory { public static Limiter newLimiter(State state) { state = convertDeprecatedConfigs(state); Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TYPE_KEY), String.format("Missing configuration property %s for the Limiter type", EXTRACT_LIMIT_TYPE_KEY)); BaseLimiterType type = BaseLimiterType.forName(state.getProp(EXTRACT_LIMIT_TYPE_KEY)); switch (type) { case RATE_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_RATE_LIMIT_KEY)); int rateLimit = Integer.parseInt(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)) { TimeUnit rateTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new RateBasedLimiter(rateLimit, rateTimeUnit); } return new RateBasedLimiter(rateLimit); case TIME_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TIME_LIMIT_KEY)); long timeLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)) { TimeUnit timeTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new TimeBasedLimiter(timeLimit, timeTimeUnit); } return new TimeBasedLimiter(timeLimit); case COUNT_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); long countLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); return new CountBasedLimiter(countLimit); case POOL_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_POOL_SIZE_KEY)); int poolSize = Integer.parseInt(state.getProp(EXTRACT_LIMIT_POOL_SIZE_KEY)); return new PoolBasedLimiter(poolSize); default: throw new IllegalArgumentException("Unrecognized Limiter type: " + type.toString()); } }  static Limiter newLimiter(State state); }
DefaultLimiterFactory { public static Limiter newLimiter(State state) { state = convertDeprecatedConfigs(state); Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TYPE_KEY), String.format("Missing configuration property %s for the Limiter type", EXTRACT_LIMIT_TYPE_KEY)); BaseLimiterType type = BaseLimiterType.forName(state.getProp(EXTRACT_LIMIT_TYPE_KEY)); switch (type) { case RATE_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_RATE_LIMIT_KEY)); int rateLimit = Integer.parseInt(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY)) { TimeUnit rateTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new RateBasedLimiter(rateLimit, rateTimeUnit); } return new RateBasedLimiter(rateLimit); case TIME_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_TIME_LIMIT_KEY)); long timeLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_KEY)); if (state.contains(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY)) { TimeUnit timeTimeUnit = TimeUnit.valueOf(state.getProp(EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY).toUpperCase()); return new TimeBasedLimiter(timeLimit, timeTimeUnit); } return new TimeBasedLimiter(timeLimit); case COUNT_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); long countLimit = Long.parseLong(state.getProp(EXTRACT_LIMIT_COUNT_LIMIT_KEY)); return new CountBasedLimiter(countLimit); case POOL_BASED: Preconditions.checkArgument(state.contains(EXTRACT_LIMIT_POOL_SIZE_KEY)); int poolSize = Integer.parseInt(state.getProp(EXTRACT_LIMIT_POOL_SIZE_KEY)); return new PoolBasedLimiter(poolSize); default: throw new IllegalArgumentException("Unrecognized Limiter type: " + type.toString()); } }  static Limiter newLimiter(State state); static final String EXTRACT_LIMIT_TYPE_KEY; static final String EXTRACT_LIMIT_RATE_LIMIT_KEY; static final String EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY; static final String EXTRACT_LIMIT_TIME_LIMIT_KEY; static final String EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY; static final String EXTRACT_LIMIT_COUNT_LIMIT_KEY; static final String EXTRACT_LIMIT_POOL_SIZE_KEY; @Deprecated
  static final String EXTRACT_LIMIT_RATE_LIMIT_KEY_DEP; @Deprecated
  static final String EXTRACT_LIMIT_RATE_LIMIT_TIMEUNIT_KEY_DEP; @Deprecated
  static final String EXTRACT_LIMIT_TIME_LIMIT_KEY_DEP; @Deprecated
  static final String EXTRACT_LIMIT_TIME_LIMIT_TIMEUNIT_KEY_DEP; }