Class ManualService<T>

java.lang.Object
com.amalgamasimulation.service.Service<T>
com.amalgamasimulation.service.ManualService<T>

public class ManualService<T> extends Service<T>
A Service that is fully controlled programmatically, i.e. all attempts to start service of a next unit are made externally by calling takeNext() method.

Mostly applicable when the processed units should be placed in a queue, but the number of concurrently processed units is not important or is determined by some complex external logic.

Author:
Andrey Malykhanov
  • Constructor Details

    • ManualService

      public ManualService()
      Creates a new instance of a manual service.
  • 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
    • takeNext

      public T takeNext()
      Description copied from class: Service
      Attempts to start service of the next unit.

      If the waiting queue is not empty, moves the next unit from this queue to the serviced units collection, then calls the corresponding handlers.

      Overrides:
      takeNext in class Service<T>
      Returns:
      the object taken from the waiting queue, or null if the queue is empty