Package com.github.cowwoc.tokenbucket
Class Limit.ConfigurationUpdater
java.lang.Object
com.github.cowwoc.tokenbucket.Limit.ConfigurationUpdater
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
- Limit
Updates this Limit's configuration.
Thread-safety: This class is not thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the number of available tokens.availableTokens
(long availableTokens) Sets the number of available tokens.void
close()
Updates this Limit's configuration and releases its lock.long
Returns the maximum number of tokens that the bucket may hold before overflowing.maximumTokens
(long maximumTokens) Sets the maximum number of tokens that the bucket may hold before overflowing.period()
Indicates how oftentokensPerPeriod
should be added to the bucket.Indicates how oftentokensPerPeriod
should be added to the bucket.long
Returns the number of tokens that are refilled at a time.refillSize
(long refillSize) Sets the number of tokens that are refilled at a time.long
Returns the number of tokens to add to the bucket everyperiod
.tokensPerPeriod
(long tokensPerPeriod) Sets the number of tokens to add to the bucket everyperiod
.toString()
userData()
Returns the data associated with this limit.Sets the data associated with this limit.
-
Method Details
-
tokensPerPeriod
public long tokensPerPeriod()Returns the number of tokens to add to the bucket everyperiod
.- Returns:
- the number of tokens to add to the bucket every
period
- Throws:
IllegalStateException
- if the updater is closed
-
tokensPerPeriod
Sets the number of tokens to add to the bucket everyperiod
.- Parameters:
tokensPerPeriod
- the number of tokens to add to the bucket everyperiod
- Returns:
- this
- Throws:
IllegalArgumentException
- iftokensPerPeriod
is negative or zeroIllegalStateException
- if the updater is closed
-
period
Indicates how oftentokensPerPeriod
should be added to the bucket.- Returns:
- how often
tokensPerPeriod
should be added to the bucket - Throws:
IllegalStateException
- if the updater is closed
-
period
Indicates how oftentokensPerPeriod
should be added to the bucket.- Parameters:
period
- indicates how oftentokensPerPeriod
should be added to the bucket- Returns:
- this
- Throws:
NullPointerException
- ifperiod
is nullIllegalStateException
- if the updater is closed
-
availableTokens
public long availableTokens()Returns the number of available tokens. The value may be negative, in which case the bucket must accumulate a positive number of tokens before they may be consumed.- Returns:
- the number of available tokens
- Throws:
IllegalStateException
- if the updater is closed
-
availableTokens
Sets the number of available tokens. The value may be negative, in which case the bucket must accumulate a positive number of tokens before they may be consumed.- Parameters:
availableTokens
- the number of available tokens- Returns:
- this
- Throws:
IllegalStateException
- if the updater is closed
-
refillSize
public long refillSize()Returns the number of tokens that are refilled at a time.At one end of the spectrum, one may refill tokens one at a time during the
period
. On the other spectrum, one may refilltokensPerPeriod
tokens once perperiod
.NOTE: When completing a
period
a smaller number of tokens may be refilled in order to avoid surpassingtokensPerPeriod
due to rounding errors.- Returns:
- the minimum number of tokens by which the limit may be refilled
- Throws:
IllegalStateException
- if the updater is closed
-
refillSize
Sets the number of tokens that are refilled at a time.At one end of the spectrum, one may refill tokens one at a time during the
period
. On the other spectrum, one may refilltokensPerPeriod
tokens once perperiod
.NOTE: When completing a
period
a smaller number of tokens may be refilled in order to avoid surpassingtokensPerPeriod
due to rounding errors.- Parameters:
refillSize
- the minimum number of tokens by which the limit may be refilled- Returns:
- this
- Throws:
IllegalArgumentException
- ifrefillSize
is negative or zeroIllegalStateException
- if the updater is closed
-
maximumTokens
public long maximumTokens()Returns the maximum number of tokens that the bucket may hold before overflowing.- Returns:
- the maximum number of tokens that the bucket may hold before overflowing (subsequent tokens are discarded)
- Throws:
IllegalStateException
- if the updater is closed
-
maximumTokens
Sets the maximum number of tokens that the bucket may hold before overflowing.- Parameters:
maximumTokens
- the maximum number of tokens that the bucket may hold before overflowing (subsequent tokens are discarded)- Returns:
- this
- Throws:
IllegalArgumentException
- ifmaximumTokens
is negative or zeroIllegalStateException
- if the updater is closed
-
userData
Returns the data associated with this limit.- Returns:
- the data associated with this limit
- Throws:
IllegalStateException
- if the updater is closed
-
userData
Sets the data associated with this limit.- Parameters:
userData
- the data associated with this limit- Returns:
- this
- Throws:
IllegalStateException
- if the updater is closed
-
close
public void close()Updates this Limit's configuration and releases its lock.A new period is started immediately after the update takes place.
- Specified by:
close
in interfaceAutoCloseable
-
toString
-