Class ConsumptionResult

java.lang.Object
com.github.cowwoc.tokenbucket.ConsumptionResult

public final class ConsumptionResult extends Object
The result of an attempt to consume tokens.
  • Constructor Details

    • ConsumptionResult

      public ConsumptionResult(Container container, long minimumTokensRequested, long maximumTokensRequested, long tokensConsumed, Instant requestedAt, Instant consumedAt, Instant availableAt, long tokensLeft, List<Limit> bottlenecks)
      Creates a result of a request to consume tokens.
      Parameters:
      container - the container that gave up tokens
      minimumTokensRequested - the minimum number of tokens that were requested (inclusive)
      maximumTokensRequested - the maximum number of tokens that were requested (inclusive)
      tokensConsumed - the number of tokens that were consumed
      requestedAt - the time at which the tokens were requested
      consumedAt - the time at which an attempt was made to consume tokens. This value differs from requestedAt in that the caller may have to block until tokens become available for consumption, at which time consumedAt is set.
      availableAt - the time at which the requested tokens are expected to become available. If tokens were consumed, this value is equal to consumedAt.
      tokensLeft - the number of tokens left
      bottlenecks - the list of Limits that are preventing tokens from being consumed (empty if none)
      Throws:
      NullPointerException - if any of the arguments are null
      IllegalArgumentException - if minimumTokensRequested or maximumTokensRequests are negative or zero. If tokensConsumed or tokensLeft are negative. If minimumTokensRequested > maximumTokensRequested. If tokensConsumed > 0 && tokensConsumed < minimumTokensRequested. If requestedAt > consumedAt or consumedAt > availableAt.
  • Method Details

    • getContainer

      public Container getContainer()
      Returns the container that tokens were consumed from. If tokens were consumed from a single bucket, it is returned. If tokens were consumed from multiple buckets, the lowest common ancestor is returned.
      Returns:
      the container that gave up tokens
    • getMinimumTokensRequested

      public long getMinimumTokensRequested()
      Returns the minimum number of tokens that were requested.
      Returns:
      the minimum number of tokens that were requested (inclusive)
    • getMaximumTokensRequested

      public long getMaximumTokensRequested()
      Returns the maximum number of tokens that were requested.
      Returns:
      the maximum number of tokens that were requested (inclusive)
    • getTokensConsumed

      public long getTokensConsumed()
      Returns the number of tokens that were consumed by the request.
      Returns:
      the number of tokens that were consumed by the request
    • isSuccessful

      public boolean isSuccessful()
      Returns true if the bucket was able to consume any tokens.
      Returns:
      true if the bucket was able to consume any tokens
    • getAvailableAt

      public Instant getAvailableAt()
      Returns the time at which the requested tokens are expected to become available. If tokens were consumed, this value is equal to consumedAt.
      Returns:
      the time at which the requested tokens are expected to become available
    • getConsumeAt

      public Instant getConsumeAt()
      Returns the time at which an attempt was made to consume tokens.

      This value differs from requestedAt in that the caller may have to block until tokens become available for consumption, at which time consumedAt is set.

      Returns:
      the time at which an attempt was made to consume tokens
    • getAvailableIn

      public Duration getAvailableIn()
      Returns the amount of time until the requested number of tokens will become available.
      Returns:
      the amount of time until the requested number of tokens will become available
    • getTokensLeft

      public long getTokensLeft()
      Returns the number of tokens left.
      Returns:
      the number of tokens left
    • getBottlenecks

      public List<Limit> getBottlenecks()
      Returns the list of limits that are preventing tokens from being consumed.
      Returns:
      empty if tokens were consumed
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object