StatItem { public boolean isAllowable() { long now = System.currentTimeMillis(); if (now > lastResetTime + interval) { token = buildLongAdder(rate); lastResetTime = now; } if (token.sum() < 0) { return false; } token.decrement(); return true; } StatItem(String name, int rate, long interval); boolean isAllowable(); long getInterval(); int getRate(); @Override String toString();  }
@Test public void testIsAllowable() throws Exception { statItem = new StatItem("test", 5, 1000L); long lastResetTime = statItem.getLastResetTime(); assertTrue(statItem.isAllowable()); Thread.sleep(1100L); assertTrue(statItem.isAllowable()); assertTrue(lastResetTime != statItem.getLastResetTime()); assertEquals(4, statItem.getToken()); }