public abstract class AbstractBucket extends Object implements Bucket
Modifier and Type | Field and Description |
---|---|
protected Bandwidth[] |
bandwidths |
protected BucketConfiguration |
configuration |
protected TimeMeter |
timeMeter |
protected static long |
UNSPECIFIED_WAITING_LIMIT |
Modifier | Constructor and Description |
---|---|
protected |
AbstractBucket(BucketConfiguration configuration) |
Modifier and Type | Method and Description |
---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to each bandwidth of bucket.
|
protected abstract void |
addTokensImpl(long tokensToAdd) |
void |
consume(long tokensToConsume)
Consumes
numTokens from the bucket. |
boolean |
consume(long tokensToConsume,
long maxWaitTimeNanos)
Consumes a specified number of tokens from the bucket.
|
protected abstract long |
consumeAsMuchAsPossibleImpl(long limit) |
protected abstract boolean |
consumeOrAwaitImpl(long tokensToConsume,
long waitIfBusyNanos,
boolean uninterruptibly) |
void |
consumeUninterruptibly(long tokensToConsume)
Consumes
numTokens from the bucket. |
boolean |
consumeUninterruptibly(long tokensToConsume,
long maxWaitTimeNanos)
Consumes a specified number of tokens from the bucket.
|
BucketConfiguration |
getConfiguration()
Returns configuration of this bucket.
|
boolean |
tryConsume(long tokensToConsume)
Tries to consume a specified number of tokens from this bucket.
|
long |
tryConsumeAsMuchAsPossible()
Tries to consume as much tokens from this bucket as available at the moment of invocation.
|
long |
tryConsumeAsMuchAsPossible(long limit)
Tries to consume as much tokens from bucket as available in the bucket at the moment of invocation,
but tokens which should be consumed is limited by than not more than
limit . |
protected abstract boolean |
tryConsumeImpl(long tokensToConsume) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
createSnapshot
protected static final long UNSPECIFIED_WAITING_LIMIT
protected final BucketConfiguration configuration
protected final Bandwidth[] bandwidths
protected final TimeMeter timeMeter
protected AbstractBucket(BucketConfiguration configuration)
protected abstract long consumeAsMuchAsPossibleImpl(long limit)
protected abstract boolean tryConsumeImpl(long tokensToConsume)
protected abstract boolean consumeOrAwaitImpl(long tokensToConsume, long waitIfBusyNanos, boolean uninterruptibly) throws InterruptedException
InterruptedException
protected abstract void addTokensImpl(long tokensToAdd)
public boolean tryConsume(long tokensToConsume)
Bucket
tryConsume
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket, must be a positive number.true
if the tokens were consumed, false
otherwise.public void consume(long tokensToConsume) throws InterruptedException
Bucket
numTokens
from the bucket. If enough tokens are not currently available then this method will block
until required number of tokens will be available or current thread is interrupted.consume
in interface Bucket
tokensToConsume
- The number of tokens to consume from bucket, must be a positive number.InterruptedException
- in case of current thread has been interrupted during waitingpublic boolean consume(long tokensToConsume, long maxWaitTimeNanos) throws InterruptedException
Bucket
maxWaitTimeNanos
has elapsed.consume
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket.maxWaitTimeNanos
- limit of time which thread can wait.numTokens
has been consumed or false when numTokens
has not been consumedInterruptedException
- in case of current thread has been interrupted during waitingpublic void consumeUninterruptibly(long tokensToConsume)
Bucket
numTokens
from the bucket. If enough tokens are not currently available then this method will block
until required number of tokens will be available or current thread is interrupted.consumeUninterruptibly
in interface Bucket
tokensToConsume
- The number of tokens to consume from bucket, must be a positive number.public boolean consumeUninterruptibly(long tokensToConsume, long maxWaitTimeNanos)
Bucket
consumeUninterruptibly
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket.maxWaitTimeNanos
- limit of time which thread can wait.numTokens
has been consumed or false when numTokens
has not been consumedpublic long tryConsumeAsMuchAsPossible(long limit)
Bucket
limit
.tryConsumeAsMuchAsPossible
in interface Bucket
limit
- maximum number of tokens to consume, should be positive.public long tryConsumeAsMuchAsPossible()
Bucket
tryConsumeAsMuchAsPossible
in interface Bucket
public void addTokens(long tokensToAdd)
Bucket
newTokens = Math.min(capacity, currentTokens + tokensToAdd)in other words resulted number of tokens never exceeds capacity independent of tokensToAdd.
Bucket wallet;
...
wallet.consume(50); // get 50 cents from wallet
try {
buyCocaCola();
} catch(NoCocaColaException e) {
// return money to wallet
wallet.addTokens(50);
}
public BucketConfiguration getConfiguration()
Bucket
getConfiguration
in interface Bucket
Copyright © 2017. All rights reserved.