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 classBuilds a bucket.final classUpdates this Bucket's configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic Bucket.Builderbuilder()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) Consumestokenstokens, 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 ofrefillSizeas 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
Bucketwill be locked untilBucket.ConfigurationUpdater.close()is invoked.- Returns:
- the configuration updater
-
getListeners
Description copied from interface:ContainerReturns the listeners associated with this container.- Specified by:
getListenersin interfaceContainer- Overrides:
getListenersin classcom.github.cowwoc.tokenbucket.internal.AbstractContainer- Returns:
- an unmodifiable list
-
getUserData
Description copied from interface:ContainerReturns the data associated with this container.- Specified by:
getUserDatain interfaceContainer- Overrides:
getUserDatain classcom.github.cowwoc.tokenbucket.internal.AbstractContainer- Returns:
- the data associated with this container
-
tryConsume
Description copied from interface:ContainerConsumes a single token, only if one is available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsumein interfaceContainer- Overrides:
tryConsumein classcom.github.cowwoc.tokenbucket.internal.AbstractContainer- Returns:
- the result of the operation
-
tryConsume
Description copied from interface:ContainerConsumestokenstokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsumein interfaceContainer- Overrides:
tryConsumein 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:ContainerConsumes the requested number oftokens, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- Specified by:
tryConsumein interfaceContainer- Overrides:
tryConsumein 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:ContainerConsumes[minimumTokens, maximumTokens]tokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- Specified by:
tryConsumein interfaceContainer- Overrides:
tryConsumein 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:ContainerConsumes[minimumTokens, maximumTokens]tokens, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- Specified by:
tryConsumein interfaceContainer- Overrides:
tryConsumein 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:ContainerConsumes a single token, blocking until it becomes available. Consumption order is not guaranteed to be fair.- Specified by:
consumein interfaceContainer- Overrides:
consumein 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:ContainerConsumes the specified number of tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- Specified by:
consumein interfaceContainer- Overrides:
consumein 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:ContainerConsumes[minimumTokens, maximumTokens]tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- Specified by:
consumein interfaceContainer- Overrides:
consumein 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
-