java.lang.Object
com.github.cowwoc.tokenbucket.internal.AbstractContainer
com.github.cowwoc.tokenbucket.Bucket
- All Implemented Interfaces:
Container
public final class Bucket
extends com.github.cowwoc.tokenbucket.internal.AbstractContainer
A container with Limits.
Thread safety: This class is thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Builds a bucket.final class
Updates this Bucket's configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic Bucket.Builder
builder()
Builds a new bucket.consume()
Consumes a single token, blocking until it becomes available.consume
(long tokens) Consumes the specified number of tokens, blocking until they become available.consume
(long minimumTokens, long maximumTokens) Consumes[minimumTokens, maximumTokens]
tokens, blocking until they become available.Returns the limits associated with this bucket.Returns the limit with the lowest refill rate.Returns the listeners associated with this container.Returns the data associated with this container.toString()
Consumes a single token, only if one is available at the time of invocation.tryConsume
(long tokens) Consumestokens
tokens, only if they are available at the time of invocation.tryConsume
(long minimumTokens, long maximumTokens) Consumes[minimumTokens, maximumTokens]
tokens, only if they are available at the time of invocation.tryConsume
(long minimumTokens, long maximumTokens, long timeout, TimeUnit unit) Consumes[minimumTokens, maximumTokens]
tokens, only if they become available within the given waiting time.tryConsume
(long tokens, long timeout, TimeUnit unit) Consumes the requested number oftokens
, only if they become available within the given waiting time.Updates this Bucket's configuration.Methods inherited from class com.github.cowwoc.tokenbucket.internal.AbstractContainer
getChildren
-
Method Details
-
builder
Builds a new bucket.- Returns:
- a Bucket builder
-
getLimits
Returns the limits associated with this bucket.- Returns:
- an unmodifiable list
-
getLimitWithLowestRefillRate
Returns the limit with the lowest refill rate. Note that this differs from the concept ofrefillSize
as the former corresponds to the number of tokens refilled per fixed period of time.- Returns:
- the limit with the lowest refill rate
-
updateConfiguration
Updates this Bucket's configuration.The
Bucket
will be locked untilBucket.ConfigurationUpdater.close()
is invoked.- Returns:
- the configuration updater
-
getListeners
Description copied from interface:Container
Returns the listeners associated with this container.- Specified by:
getListeners
in interfaceContainer
- Overrides:
getListeners
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Returns:
- an unmodifiable list
-
getUserData
Description copied from interface:Container
Returns the data associated with this container.- Specified by:
getUserData
in interfaceContainer
- Overrides:
getUserData
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Returns:
- the data associated with this container
-
tryConsume
Description copied from interface:Container
Consumes a single token, only if one is available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsume
in interfaceContainer
- Overrides:
tryConsume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Returns:
- the result of the operation
-
tryConsume
Description copied from interface:Container
Consumestokens
tokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsume
in interfaceContainer
- Overrides:
tryConsume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
tokens
- the number of tokens to consume- Returns:
- the result of the operation
-
tryConsume
public ConsumptionResult tryConsume(long tokens, long timeout, TimeUnit unit) throws InterruptedException Description copied from interface:Container
Consumes the requested number oftokens
, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- Specified by:
tryConsume
in interfaceContainer
- Overrides:
tryConsume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
tokens
- the number of tokens to consumetimeout
- the maximum number of time to waitunit
- the unit oftimeout
- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
tryConsume
Description copied from interface:Container
Consumes[minimumTokens, maximumTokens]
tokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsume
in interfaceContainer
- Overrides:
tryConsume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
minimumTokens
- the minimum number of tokens to consume (inclusive)maximumTokens
- the maximum number of tokens to consume (inclusive)- Returns:
- the result of the operation
-
tryConsume
public ConsumptionResult tryConsume(long minimumTokens, long maximumTokens, long timeout, TimeUnit unit) throws InterruptedException Description copied from interface:Container
Consumes[minimumTokens, maximumTokens]
tokens, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- Specified by:
tryConsume
in interfaceContainer
- Overrides:
tryConsume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
minimumTokens
- the minimum number of tokens to consume (inclusive)maximumTokens
- the maximum number of tokens to consume (inclusive)timeout
- the maximum amount of time to waitunit
- the unit oftimeout
- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
Description copied from interface:Container
Consumes a single token, blocking until it becomes available. Consumption order is not guaranteed to be fair.- Specified by:
consume
in interfaceContainer
- Overrides:
consume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
Description copied from interface:Container
Consumes the specified number of tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- Specified by:
consume
in interfaceContainer
- Overrides:
consume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
tokens
- the number of tokens to consume- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
public ConsumptionResult consume(long minimumTokens, long maximumTokens) throws InterruptedException Description copied from interface:Container
Consumes[minimumTokens, maximumTokens]
tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- Specified by:
consume
in interfaceContainer
- Overrides:
consume
in classcom.github.cowwoc.tokenbucket.internal.AbstractContainer
- Parameters:
minimumTokens
- the minimum number of tokens to consume (inclusive)maximumTokens
- the maximum number of tokens to consume (inclusive)- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
toString
-