- 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) 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.
-
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
Consumestokenstokens, 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- iftokensis 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- ifunitis nullIllegalArgumentException- iftokensis negative or zero. Iftimeoutis 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- ifunitis nullIllegalArgumentException- iftokensis negative or zero. Iftimeoutis 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- iftokensis 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- ifunitis nullIllegalArgumentException- iftokensortimeoutare 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
-