- All Known Implementing Classes:
com.github.cowwoc.tokenbucket.internal.AbstractContainer
,Bucket
,ContainerList
public interface Container
A container of tokens.
-
Method Summary
Modifier and TypeMethodDescriptionconsume()
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 children containers.Returns the listeners associated with this container.Returns the data associated with this container.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.
-
Method Details
-
getUserData
Object getUserData()Returns the data associated with this container.- Returns:
- the data associated with this container
-
getChildren
Returns the children containers.- Returns:
- the children containers
-
getListeners
List<ContainerListener> getListeners()Returns the listeners associated with this container.- Returns:
- an unmodifiable list
-
tryConsume
ConsumptionResult tryConsume()Consumes a single token, only if one is available at the time of invocation. Consumption order is not guaranteed to be fair.- Returns:
- the result of the operation
-
tryConsume
Consumestokens
tokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- Parameters:
tokens
- the number of tokens to consume- Returns:
- the result of the operation
- Throws:
IllegalArgumentException
- iftokens
is negative or zero. If the request can never succeed because the container cannot hold the requested number of tokens.
-
tryConsume
Consumes the requested number oftokens
, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- 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:
NullPointerException
- ifunit
is nullIllegalArgumentException
- iftokens
is negative or zero. Iftimeout
is negative. If the request can never succeed because the container cannot hold the requested number of tokens.InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
tryConsume
Consumes[minimumTokens, maximumTokens]
tokens, only if they are available at the time of invocation. Consumption order is not guaranteed to be fair.- 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:
IllegalArgumentException
- if the arguments are negative or zero. IfminimumTokens > maximumTokens
. If the request can never succeed because the container cannot hold the requested number of tokens.
-
tryConsume
ConsumptionResult tryConsume(long minimumTokens, long maximumTokens, long timeout, TimeUnit unit) throws InterruptedException Consumes[minimumTokens, maximumTokens]
tokens, only if they become available within the given waiting time. Consumption order is not guaranteed to be fair.- 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:
NullPointerException
- ifunit
is nullIllegalArgumentException
- iftokens
is negative or zero. Iftimeout
is negative. IfminimumTokens > maximumTokens
. If the request can never succeed because the container cannot hold the requested number of tokens.InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
Consumes a single token, blocking until it becomes available. Consumption order is not guaranteed to be fair.- Returns:
- the result of the operation
- Throws:
InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
Consumes the specified number of tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- Parameters:
tokens
- the number of tokens to consume- Returns:
- the result of the operation
- Throws:
IllegalArgumentException
- iftokens
is negative or zero. If the request can never succeed because the container cannot hold the requested number of tokens.InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-
consume
Consumes[minimumTokens, maximumTokens]
tokens, blocking until they become available. Consumption order is not guaranteed to be fair.- 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:
NullPointerException
- ifunit
is nullIllegalArgumentException
- iftokens
ortimeout
are negative or zero. IfminimumTokens > maximumTokens
. If the request can never succeed because the container cannot hold the requested number of tokens.InterruptedException
- if the thread is interrupted while waiting for tokens to become available
-