Class Limit.ConfigurationUpdater

java.lang.Object
com.github.cowwoc.tokenbucket.Limit.ConfigurationUpdater
All Implemented Interfaces:
AutoCloseable
Enclosing class:
Limit

public final class Limit.ConfigurationUpdater extends Object implements AutoCloseable
Updates this Limit's configuration.

Thread-safety: This class is not thread-safe.

  • Method Details

    • tokensPerPeriod

      public long tokensPerPeriod()
      Returns the number of tokens to add to the bucket every period.
      Returns:
      the number of tokens to add to the bucket every period
      Throws:
      IllegalStateException - if the updater is closed
    • tokensPerPeriod

      public Limit.ConfigurationUpdater tokensPerPeriod(long tokensPerPeriod)
      Sets the number of tokens to add to the bucket every period.
      Parameters:
      tokensPerPeriod - the number of tokens to add to the bucket every period
      Returns:
      this
      Throws:
      IllegalArgumentException - if tokensPerPeriod is negative or zero
      IllegalStateException - if the updater is closed
    • period

      public Duration period()
      Indicates how often tokensPerPeriod should be added to the bucket.
      Returns:
      how often tokensPerPeriod should be added to the bucket
      Throws:
      IllegalStateException - if the updater is closed
    • period

      public Limit.ConfigurationUpdater period(Duration period)
      Indicates how often tokensPerPeriod should be added to the bucket.
      Parameters:
      period - indicates how often tokensPerPeriod should be added to the bucket
      Returns:
      this
      Throws:
      NullPointerException - if period is null
      IllegalStateException - if the updater is closed
    • availableTokens

      public long availableTokens()
      Returns the number of available tokens. The value may be negative, in which case the bucket must accumulate a positive number of tokens before they may be consumed.
      Returns:
      the number of available tokens
      Throws:
      IllegalStateException - if the updater is closed
    • availableTokens

      public Limit.ConfigurationUpdater availableTokens(long availableTokens)
      Sets the number of available tokens. The value may be negative, in which case the bucket must accumulate a positive number of tokens before they may be consumed.
      Parameters:
      availableTokens - the number of available tokens
      Returns:
      this
      Throws:
      IllegalStateException - if the updater is closed
    • refillSize

      public long refillSize()
      Returns the number of tokens that are refilled at a time.

      At one end of the spectrum, one may refill tokens one at a time during the period. On the other spectrum, one may refill tokensPerPeriod tokens once per period.

      NOTE: When completing a period a smaller number of tokens may be refilled in order to avoid surpassing tokensPerPeriod due to rounding errors.

      Returns:
      the minimum number of tokens by which the limit may be refilled
      Throws:
      IllegalStateException - if the updater is closed
    • refillSize

      public Limit.ConfigurationUpdater refillSize(long refillSize)
      Sets the number of tokens that are refilled at a time.

      At one end of the spectrum, one may refill tokens one at a time during the period. On the other spectrum, one may refill tokensPerPeriod tokens once per period.

      NOTE: When completing a period a smaller number of tokens may be refilled in order to avoid surpassing tokensPerPeriod due to rounding errors.

      Parameters:
      refillSize - the minimum number of tokens by which the limit may be refilled
      Returns:
      this
      Throws:
      IllegalArgumentException - if refillSize is negative or zero
      IllegalStateException - if the updater is closed
    • maximumTokens

      public long maximumTokens()
      Returns the maximum number of tokens that the bucket may hold before overflowing.
      Returns:
      the maximum number of tokens that the bucket may hold before overflowing (subsequent tokens are discarded)
      Throws:
      IllegalStateException - if the updater is closed
    • maximumTokens

      public Limit.ConfigurationUpdater maximumTokens(long maximumTokens)
      Sets the maximum number of tokens that the bucket may hold before overflowing.
      Parameters:
      maximumTokens - the maximum number of tokens that the bucket may hold before overflowing (subsequent tokens are discarded)
      Returns:
      this
      Throws:
      IllegalArgumentException - if maximumTokens is negative or zero
      IllegalStateException - if the updater is closed
    • userData

      public Object userData()
      Returns the data associated with this limit.
      Returns:
      the data associated with this limit
      Throws:
      IllegalStateException - if the updater is closed
    • userData

      public Limit.ConfigurationUpdater userData(Object userData)
      Sets the data associated with this limit.
      Parameters:
      userData - the data associated with this limit
      Returns:
      this
      Throws:
      IllegalStateException - if the updater is closed
    • close

      public void close()
      Updates this Limit's configuration and releases its lock.

      A new period is started immediately after the update takes place.

      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object