@Test public void refillShouldReturn10() { TestTicker testTicker = createTestTicker(); Stopwatch stopwatch = Stopwatch.createStarted(testTicker); testTicker.setNanos(TimeUnit.SECONDS.toNanos(9)); RefillStrategy refillStrategy = new FixedIntervalRefillStrategy(stopwatch, 1, 1, TimeUnit.SECONDS); assertEquals(10, refillStrategy.refill()); }
@Override public long refill() { synchronized (mutex) { long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); if (elapsed < nextRefillTime) { return 0; } long numberOfIntervals = Math.max(0, (elapsed - lastRefillTime) / intervalInNanos); lastRefillTime += numberOfIntervals * intervalInNanos; nextRefillTime = lastRefillTime + intervalInNanos; return numberOfIntervals * numberOfTokensPerInterval; } }
FixedIntervalRefillStrategy implements RefillStrategy { @Override public long refill() { synchronized (mutex) { long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); if (elapsed < nextRefillTime) { return 0; } long numberOfIntervals = Math.max(0, (elapsed - lastRefillTime) / intervalInNanos); lastRefillTime += numberOfIntervals * intervalInNanos; nextRefillTime = lastRefillTime + intervalInNanos; return numberOfIntervals * numberOfTokensPerInterval; } } }
FixedIntervalRefillStrategy implements RefillStrategy { @Override public long refill() { synchronized (mutex) { long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); if (elapsed < nextRefillTime) { return 0; } long numberOfIntervals = Math.max(0, (elapsed - lastRefillTime) / intervalInNanos); lastRefillTime += numberOfIntervals * intervalInNanos; nextRefillTime = lastRefillTime + intervalInNanos; return numberOfIntervals * numberOfTokensPerInterval; } } FixedIntervalRefillStrategy(Stopwatch stopwatch, long numberOfTokensPerInterval, long interval, TimeUnit unit); }
FixedIntervalRefillStrategy implements RefillStrategy { @Override public long refill() { synchronized (mutex) { long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); if (elapsed < nextRefillTime) { return 0; } long numberOfIntervals = Math.max(0, (elapsed - lastRefillTime) / intervalInNanos); lastRefillTime += numberOfIntervals * intervalInNanos; nextRefillTime = lastRefillTime + intervalInNanos; return numberOfIntervals * numberOfTokensPerInterval; } } FixedIntervalRefillStrategy(Stopwatch stopwatch, long numberOfTokensPerInterval, long interval, TimeUnit unit); @Override long refill(); @Override long getTimeUntilNextRefill(TimeUnit unit); @Override String toString(); }
FixedIntervalRefillStrategy implements RefillStrategy { @Override public long refill() { synchronized (mutex) { long elapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS); if (elapsed < nextRefillTime) { return 0; } long numberOfIntervals = Math.max(0, (elapsed - lastRefillTime) / intervalInNanos); lastRefillTime += numberOfIntervals * intervalInNanos; nextRefillTime = lastRefillTime + intervalInNanos; return numberOfIntervals * numberOfTokensPerInterval; } } FixedIntervalRefillStrategy(Stopwatch stopwatch, long numberOfTokensPerInterval, long interval, TimeUnit unit); @Override long refill(); @Override long getTimeUntilNextRefill(TimeUnit unit); @Override String toString();  }