Interface Cooldown
public interface Cooldown
Represents the number of times an action can be executed within a specific time frame. The default implementation
given by
of(int, Duration)
is thread-safe.-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Cooldown.Remaining
Data class containing info on the remaining permits and duration before reset. -
Method Summary
Modifier and Type Method Description void
fire(long userId)
Consumes one permit in this cooldown for the specified user.Duration
getResetInterval()
Gets the interval after which a permit becomes available again after being used.int
getTotalPermits()
Gets the total number of times the action can be executed within the interval.static Cooldown
none()
ACooldown
that gives unlimited permits.static Cooldown
of(int permits, Duration resetInterval)
Creates aCooldown
with the given permits and reset interval values.Cooldown.Remaining
remaining(long userId)
Computes and returns the remaining permits and time before next permit for the specified user.
-
Method Details
-
none
ACooldown
that gives unlimited permits.- Returns:
- an unlimited
Cooldown
-
of
Creates aCooldown
with the given permits and reset interval values.- Parameters:
permits
- the number of times the action can be executed within the intervalresetInterval
- the interval after which the number of permits is reset for the action- Returns:
- a new
Cooldown
-
fire
void fire(long userId)Consumes one permit in this cooldown for the specified user. If none is left,CooldownException
is thrown.- Parameters:
userId
- the user id
-
getTotalPermits
int getTotalPermits()Gets the total number of times the action can be executed within the interval.- Returns:
- the permits
-
getResetInterval
Duration getResetInterval()Gets the interval after which a permit becomes available again after being used.- Returns:
- the reset interval
-
remaining
Computes and returns the remaining permits and time before next permit for the specified user.- Parameters:
userId
- the user id- Returns:
- the remaining after computation
-