Class CapacityManager


  • public class CapacityManager
    extends Object
    Manages capacity of a finite resource. Capacity can be acquired and released.
    • Constructor Detail

      • CapacityManager

        public CapacityManager​(int maxCapacity)
        Creates a CapacityManager.
        Parameters:
        maxCapacity - maximum capacity of this resource. available capacity will initially be set to this value. if a negative value is provided the capacity manager will operate in a no-op passthrough mode in which all acquire calls will return true.
    • Method Detail

      • acquire

        public boolean acquire()
        Attempts to acquire a single capacity unit. If acquired, capacity will be consumed from the available pool.
        Returns:
        true if capacity can be acquired, false if not
      • acquire

        public boolean acquire​(int capacity)
        Attempts to acquire a given amount of capacity. If acquired, capacity will be consumed from the available pool.
        Parameters:
        capacity - capacity to acquire
        Returns:
        true if capacity can be acquired, false if not
        Throws:
        IllegalArgumentException - if given capacity is negative
      • release

        public void release()
        Releases a single unit of capacity back to the pool, making it available to consumers.
      • release

        public void release​(int capacity)
        Releases a given amount of capacity back to the pool, making it available to consumers.
        Parameters:
        capacity - capacity to release
        Throws:
        IllegalArgumentException - if given capacity is negative
      • consumedCapacity

        public int consumedCapacity()
        Returns the currently consumed capacity.
        Returns:
        consumed capacity
      • availableCapacity

        public int availableCapacity()
        Returns the currently available capacity.
        Returns:
        available capacity