Package com.amalgamasimulation.service
Class ManualService<T>
java.lang.Object
com.amalgamasimulation.service.Service<T>
com.amalgamasimulation.service.ManualService<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
-
Field Summary
Fields inherited from class com.amalgamasimulation.service.Service
servicedUnits, serviceRequestedCallbacks, serviceRequestRemovedCallbacks, serviceStartedCallbacks, serviceStartedCallbacksByRequests -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancelRequest(T unit) Cancels the request for service of the specified unit and removes it from the list of waiting units -Service.getWaitingUnits().voidplaceRequest(T unit, Consumer<Service<T>> onServiceStarted) Places the request for service of the specified unit.voidFinishes the service of the specified unit, i.e.takeNext()Attempts to start service of the next unit.Methods inherited from class com.amalgamasimulation.service.Service
addServiceRequestRemovedCallback, addServiceStartedHandler, addWaitingUnit, addWaitingUnitArrivedHandler, getServicedUnits, getWaitingUnits, removeServiceRequestRemovedCallback, removeServiceStartedHandler, removeWaitingUnit, removeWaitingUnitArrivedHandler
-
Constructor Details
-
ManualService
public ManualService()Creates a new instance of a manual service.
-
-
Method Details
-
placeRequest
Description copied from class:ServicePlaces the request for service of the specified unit. Such placement can result in one of the two following outcomes:- 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(). - If the specified unit cannot start being served immediately, the unit
goes into the list of waiting units -
Service.getWaitingUnits().
- Specified by:
placeRequestin classService<T>- Parameters:
unit- unit that request the serviceonServiceStarted- callback called when the unit start being served. Can be called immediately if no queuing is needed
- 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 -
-
cancelRequest
Description copied from class:ServiceCancels 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:
cancelRequestin classService<T>- Parameters:
unit- unit that stops waiting for the service
-
release
Description copied from class:ServiceFinishes the service of the specified unit, i.e. releases the "server" used to serve this unit. Removes the specified unit fromService.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. -
takeNext
Description copied from class:ServiceAttempts 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.
-