Package com.amalgamasimulation.service
Class FixedCapacityService<T>
java.lang.Object
com.amalgamasimulation.service.Service<T>
com.amalgamasimulation.service.FixedCapacityService<T>
- Direct Known Subclasses:
ServiceWithResources
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
-
Field Summary
Fields inherited from class com.amalgamasimulation.service.Service
servicedUnits, serviceRequestedCallbacks, serviceRequestRemovedCallbacks, serviceStartedCallbacks, serviceStartedCallbacksByRequests -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of fixed capacity service with a default capacity equal to 1.FixedCapacityService(int capacity) Creates a new instance of fixed capacity service with the specified capacity. -
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().intReturns capacity of this service, e.g.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.Methods inherited from class com.amalgamasimulation.service.Service
addServiceRequestRemovedCallback, addServiceStartedHandler, addWaitingUnit, addWaitingUnitArrivedHandler, getServicedUnits, getWaitingUnits, removeServiceRequestRemovedCallback, removeServiceStartedHandler, removeWaitingUnit, removeWaitingUnitArrivedHandler, takeNext
-
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
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. -
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
-