Class FixedCapacityService<T>

java.lang.Object
com.amalgamasimulation.service.Service<T>
com.amalgamasimulation.service.FixedCapacityService<T>
Direct Known Subclasses:
ServiceWithResources

public class FixedCapacityService<T> extends Service<T>
A Service that can simultaneously process specified number of units. All other units are placed in the waiting queue.

Unlike ManualService, this class automatically decides when to start serving the next unit.

Author:
Andrey Malykhanov
  • Constructor Details

    • FixedCapacityService

      public FixedCapacityService()
      Creates a new instance of fixed capacity service with a default capacity equal to 1.
    • FixedCapacityService

      public FixedCapacityService(int capacity)
      Creates a new instance of fixed capacity service with the specified capacity.
      Parameters:
      capacity - capacity of this service, e.g. maximum number of units that can be served simultaneously
  • Method Details

    • placeRequest

      public void placeRequest(T unit, Consumer<Service<T>> onServiceStarted)
      Description copied from class: Service
      Places the request for service of the specified unit. Such placement can result in one of the two following outcomes:
      1. If the specified unit can start being served immediately, the onServiceStarted callback is called immediately and the specified unit goes into the list of served units - Service.getServicedUnits().
      2. If the specified unit cannot start being served immediately, the unit goes into the list of waiting units - Service.getWaitingUnits().
      Specified by:
      placeRequest in class Service<T>
      Parameters:
      unit - unit that request the service
      onServiceStarted - callback called when the unit start being served. Can be called immediately if no queuing is needed
    • cancelRequest

      public void cancelRequest(T unit)
      Description copied from class: Service
      Cancels the request for service of the specified unit and removes it from the list of waiting units - Service.getWaitingUnits().

      If the specified unit is not waiting for service, throws a RuntimeException.

      Specified by:
      cancelRequest in class Service<T>
      Parameters:
      unit - unit that stops waiting for the service
    • release

      public void release(T unit)
      Description copied from class: Service
      Finishes the service of the specified unit, i.e. releases the "server" used to serve this unit. Removes the specified unit from Service.getServicedUnits() collection.

      After the unit finishes its service, it is likely that service of some other unit starts if there are units waiting for service.

      If the specified unit is not currently being served, throws a RuntimeException.

      Specified by:
      release in class Service<T>
      Parameters:
      unit - unit that finishes being served
    • getCapacity

      public int getCapacity()
      Returns capacity of this service, e.g. maximum number of units that can be served simultaneously.
      Returns:
      capacity of this service