Class ReservationService
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull org.springframework.context.ApplicationEventPublisherUsed to publish certain events likeReservationConfirmedEvent.private static final @NonNull DurationThe maximum interval between the start date and end date when callingfindReservationsByDateTime(String, LocalDateTime, LocalDateTime).static final @NonNull Durationstatic final @NonNull Durationprivate final @NonNull ReservationRepositoryProvides database access to actually create, modify and delete Reservations.private final @NonNull RoomServiceUsed to get aRoomby itsRoom.number. -
Constructor Summary
ConstructorsConstructorDescriptionReservationService(@NonNull ReservationRepository reservationRepository, @NonNull RoomService roomService, @NonNull org.springframework.context.ApplicationEventPublisher eventPublisher) Constructs a new ReservationService. -
Method Summary
Modifier and TypeMethodDescription@NonNull ReservationbookRoom(@NonNull BookRoomDTO reservationDto) Saves a newReservationto the database, based on the givenBookRoomDTO, if theFilterReservationsDTO.roomis free between the start time and end time.@NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date) Alias forbuildSchedule(Room, LocalDate, LocalTime, LocalTime)withLocalTime.MINas thestartTimeandLocalTime.MAXas theendTime.@NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date, @NonNull MergeOptions mergeOptions) Alias forbuildSchedule(Room, LocalDate, LocalTime, LocalTime, MergeOptions)withLocalTime.MINas the start time andLocalTime.MAXas the end time.@NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date, @NonNull LocalTime startTime, @NonNull LocalTime endTime) Alias forbuildSchedule(Room, LocalDate, LocalTime, LocalTime, MergeOptions)with merging disabled.@NonNull RoomSchedulebuildSchedule(@NonNull Room room, @NonNull LocalDate date, @NonNull LocalTime startTime, @NonNull LocalTime endTime, @NonNull MergeOptions mergeOptions) Creates aRoomSchedulefor the givenRoomon the givenLocalDatebetween the givenstartTimeandendTime.static @NonNull IntegercomputeMaximumOccupancy(@NonNull Collection<SeatReservation> reservations) Calculates the highest amount of overlapping SeatReservations among the given ones.computeMaximumOccupancy(@NonNull Collection<SeatReservation> reservations, @NonNull Integer threshold) Calculates the highest amount of overlapping SeatReservations among the given ones.static @NonNull @Unmodifiable List<RoomScheduleItem> convertSeatReservationChainToSchedule(@NonNull Collection<SeatReservation> chain, int capacity) Alias forconvertSeatReservationChainToSchedule(Collection, int, MergeOptions)with merging disabled.static @NonNull @Unmodifiable List<RoomScheduleItem> convertSeatReservationChainToSchedule(@NonNull Collection<SeatReservation> chain, int capacity, @NonNull MergeOptions mergeOptions) Creates a new list of RoomScheduleItems corresponding to the given chain of SeatReservations.static @NonNull List<TimespanOverlapItem> convertToTimespanOverlaps(@NonNull Collection<SeatReservation> reservations) voidPermanently removes the Reservations with the given identifiers from the database, if all of them exist.voidPermanently removes theReservationwith the givenReservation.idfrom the database.voiddeleteOwnReservation(@NonNull Long id) Permanently removes the current Actor'sReservationwith the givenReservation.idfrom the database.voiddeleteOwnReservations(@NonNull Iterable<Long> ids) Permanently removes the Reservations which belong to the current Actor and have the given identifiers from the database, if all of them exist.booleanChecks whether the database contains aReservationwith the givenReservation.id.@NonNull org.springframework.data.domain.Page<Reservation> findAll(@NonNull org.springframework.data.domain.Pageable pageable) A subset of all Reservations in the database table according to the givenPageable.@NonNull Optional<Reservation> Fetches theReservationwith the specified ID from the database.@NonNull org.springframework.data.domain.Page<Reservation> findCurrentReservationsByApplicant(@NonNull Actor applicant, @NonNull org.springframework.data.domain.Pageable pageable) @NonNull LinkedHashSet<Reservation> findReservationsAfterDateTime(@NonNull Actor applicant, @NonNull LocalDateTime date) Fetches all Reservations from the database placed for theReservation.applicantwith the givenActor.idand starting or ending after the given date.@NonNull LinkedHashSet<Reservation> findReservationsByApplicant(@NonNull Actor applicant) Fetches all Reservations from the database placed by the givenActor.@NonNull org.springframework.data.domain.Page<Reservation> findReservationsByApplicant(@NonNull String userName, @NonNull org.springframework.data.domain.Pageable pageable) @NonNull Set<Reservation> findReservationsByDateTime(@NonNull String roomNumber, @NonNull LocalDateTime startDate, @NonNull LocalDateTime endDate) @NonNull Set<Reservation> findReservationsByDateTime(@NonNull LocalDateTime startDate, @NonNull LocalDateTime endDate) @NonNull LinkedHashSet<Reservation> findReservationsByRoom(@NonNull String roomNumber) booleanisRoomFree(@NonNull Room room) booleanisSeatReservationPossible(@NonNull Room room, @NonNull LocalDate date, @NonNull LocalTime startTime, @NonNull LocalTime endTime, int seatCount) Checks whether the given number of seats is available for use in the givenRoomduring the entire duration between thestartTimeandendTimeon the givendate.private static @NonNull RoomScheduleItemmerge(@NonNull RoomScheduleItem item1, @NonNull RoomScheduleItem item2) Creates a newRoomScheduleItemfrom the two given ones.static @NonNull @Unmodifiable List<RoomScheduleItem> merge(@NonNull List<RoomScheduleItem> schedule, int roomCapacity, @NonNull MergeOptions mergeOptions) Merges consecutive RoomScheduleItems in the given schedule according to the givenMergeOptions.private voidvalideTimeInterval(@NonNull LocalDateTime startDate, @NonNull LocalDateTime endDate) Checks that:
-
Field Details
-
MAXIMUM_QUERY_TIMESPAN
The maximum interval between the start date and end date when callingfindReservationsByDateTime(String, LocalDateTime, LocalDateTime). -
MINIMUM_RESERVATION_TIMESPAN
-
MAXIMUM_RESERVATION_TIMESPAN
-
reservationRepository
Provides database access to actually create, modify and delete Reservations. -
roomService
Used to get aRoomby itsRoom.number. -
eventPublisher
@NonNull private final @NonNull org.springframework.context.ApplicationEventPublisher eventPublisherUsed to publish certain events likeReservationConfirmedEvent.
-
-
Constructor Details
-
ReservationService
public ReservationService(@NonNull @NonNull ReservationRepository reservationRepository, @NonNull @NonNull RoomService roomService, @NonNull @NonNull org.springframework.context.ApplicationEventPublisher eventPublisher) Constructs a new ReservationService.- Parameters:
reservationRepository- ThereservationRepositoryto be used for database access.roomService- Used to get aRoomby itsRoom.number.eventPublisher- TheeventPublisher.
-
-
Method Details
-
findById
@PreAuthorize("hasRole('REGULAR')") @NonNull public @NonNull Optional<Reservation> findById(@NonNull @NonNull Long id) Fetches theReservationwith the specified ID from the database.- Parameters:
id- The ID to be looked for in the database.- Returns:
- The Reservation associated with the given ID. Empty if no such Reservation exists.
-
findAll
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull org.springframework.data.domain.Page<Reservation> findAll(@NonNull @NonNull org.springframework.data.domain.Pageable pageable) A subset of all Reservations in the database table according to the givenPageable.- Parameters:
pageable- The pagination information specifying which page of Reservations shall be returned.- Returns:
- All Reservations, but only the specified subset.
-
findReservationsByRoom
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull LinkedHashSet<Reservation> findReservationsByRoom(@NonNull @NonNull String roomNumber) throws NotFoundException - Parameters:
roomNumber- The number of the Room whose Reservations shall be looked for.- Returns:
- All Reservations for the specified Room.
- Throws:
NotFoundException- If the given room number does not exist.
-
findReservationsByApplicant
@PreAuthorize("#applicant == authentication.principal or hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull LinkedHashSet<Reservation> findReservationsByApplicant(@NonNull @NonNull Actor applicant) Fetches all Reservations from the database placed by the givenActor.- Parameters:
applicant- The user whose Reservations shall be looked for.- Returns:
- All Reservations of the specified user.
-
findCurrentReservationsByApplicant
@PreAuthorize("#applicant == authentication.principal or hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull org.springframework.data.domain.Page<Reservation> findCurrentReservationsByApplicant(@NonNull @NonNull Actor applicant, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) - Parameters:
applicant- The user whose Reservations shall be looked for.pageable- The pagination information specifying which page of Reservations shall be returned.- Returns:
- All Reservations of the specified user.
-
findReservationsByApplicant
@PreAuthorize("#userName == authentication.principal.getUsername() or hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull org.springframework.data.domain.Page<Reservation> findReservationsByApplicant(@NonNull @NonNull String userName, @NonNull @NonNull org.springframework.data.domain.Pageable pageable) - Parameters:
userName- The user whose Reservations shall be looked for.pageable- The pagination information specifying which page of Reservations shall be returned.- Returns:
- All Reservations of the specified user.
-
isRoomFree
- Parameters:
room- The Room whose status shall be checked.- Returns:
- True if no Reservation is currently active for the Room.
-
findReservationsByDateTime
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull Set<Reservation> findReservationsByDateTime(@NonNull @NonNull String roomNumber, @NonNull @NonNull LocalDateTime startDate, @NonNull @NonNull LocalDateTime endDate) throws de.gustavblass.commons.exceptions.IllegalArgumentException, NotFoundException Fetches all Reservations from the database placed for the
Roomwith the givenRoom.idand starting or ending between the two given dates.To be clear: Only those Reservations are returned that simultaneously match the Room ID and the specified date interval.
- Parameters:
roomNumber- The number of the Room whose Reservations shall be looked for.startDate- The Reservations looked for start or end after this date. The start date must come before the end date.endDate- The Reservations looked for start or end before this date. The end date must come after the start date.- Returns:
- The Reservations between the two dates for the given Room.
- Throws:
NotFoundException- If the given room number does not exist.de.gustavblass.commons.exceptions.IllegalArgumentException- If the end date is not after the start date.
-
findReservationsByDateTime
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull Set<Reservation> findReservationsByDateTime(@NonNull @NonNull LocalDateTime startDate, @NonNull @NonNull LocalDateTime endDate) throws de.gustavblass.commons.exceptions.IllegalArgumentException - Parameters:
startDate- The Reservations looked for start or end after this date. The start date must come before the end date.endDate- The Reservations looked for start or end before this date. The end date must come after the start date.- Returns:
- All Reservations between the two dates.
- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the end date is not after the start date.
-
findReservationsAfterDateTime
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") @NonNull public @NonNull LinkedHashSet<Reservation> findReservationsAfterDateTime(@NonNull @NonNull Actor applicant, @NonNull @NonNull LocalDateTime date) Fetches all Reservations from the database placed for the
Reservation.applicantwith the givenActor.idand starting or ending after the given date.To be clear: Only those Reservations are returned that simultaneously match the
ActorID and the specified lower (exclusive) date-boundary.- Parameters:
applicant- TheActorwhose Reservations shall be looked for.date- The Reservations looked for start or end after this date (greater than).- Returns:
- The Reservations between the two dates for the given Room.
-
isSeatReservationPossible
public boolean isSeatReservationPossible(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull LocalTime startTime, @NonNull @NonNull LocalTime endTime, int seatCount) throws de.gustavblass.commons.exceptions.IllegalArgumentException, TimeTravelException, ReversedTimePeriodException Checks whether the given number of seats is available for use in the givenRoomduring the entire duration between thestartTimeandendTimeon the givendate.- Parameters:
room- The Room whose seat availability shall be checked.date- TheLocalDatefor which to check the seat availability. Must not come before todaystartTime- The seat availability shall be checked only for the period after thisLocalTimeon thedate. Must not come beforeLocalTime.now().endTime- The seat availability shall be checked only for the period before thisLocalTimeon thedate. Must come after thestartTime.seatCount- The number of seats that shall be available in the Room. Must be greater than zero and not greater than theRoom.capacityof the given Room.- Returns:
- True if it is possible to place a new
SeatReservationwith the specifiedSeatReservation.countis possible for the given time period in the given Room. - Throws:
TimeTravelException- If the time period is in the past.ReversedTimePeriodException- If the given start time does not come before the given end time.de.gustavblass.commons.exceptions.IllegalArgumentException- If the seat count is less than 1 or if the seat count exceeds the given Room's capacity.
-
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date) Alias for
buildSchedule(Room, LocalDate, LocalTime, LocalTime)withLocalTime.MINas thestartTimeandLocalTime.MAXas theendTime.Creates a
RoomSchedulefor the givenRoomon the givenLocalDate.- Parameters:
room- The Room for which the schedule shall be built.date- The day for which the schedule shall be valid.- Returns:
- The schedule for the given Room on the given day.
-
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull MergeOptions mergeOptions) Alias forbuildSchedule(Room, LocalDate, LocalTime, LocalTime, MergeOptions)withLocalTime.MINas the start time andLocalTime.MAXas the end time. -
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull LocalTime startTime, @NonNull @NonNull LocalTime endTime) throws ReversedTimePeriodException, ReservationPeriodTooSmallException Alias forbuildSchedule(Room, LocalDate, LocalTime, LocalTime, MergeOptions)with merging disabled. -
buildSchedule
@NonNull public @NonNull RoomSchedule buildSchedule(@NonNull @NonNull Room room, @NonNull @NonNull LocalDate date, @NonNull @NonNull LocalTime startTime, @NonNull @NonNull LocalTime endTime, @NonNull @NonNull MergeOptions mergeOptions) throws ReversedTimePeriodException, ReservationPeriodTooSmallException Creates a
RoomSchedulefor the givenRoomon the givenLocalDatebetween the givenstartTimeandendTime.The schedule will include
- one
RoomScheduleItemfor eachReservationmatching the given parameters; - one schedule item for each time period – between the start and end time – during which there is no Reservation, this includes the period before the first reservation and the period after the last reservation.
If there is no Reservation for the Room between the start and end time, the whole period will be a free period.
- Parameters:
room- The Room for which the schedule shall be built.date- The day for which the schedule shall be valid.startTime- Ignore Reservations that come before this point in time. Must come >5 minutes before theendTime!endTime- Ignore Reservations that come after this point in time. Must come >5 minutes after thestartTime!mergeOptions- The resulting schedule items will be merged according to these options.- Returns:
- The schedule for the given Room between the given
startTimeandendTimeon the given day. - Throws:
ReversedTimePeriodException- If the start time does not come before the end time.ReservationPeriodTooSmallException- If thestartTimedoes not come >5 minutes before theendTime.- Implementation Note:
- Please put on protective goggles before looking at this code!
- one
-
valideTimeInterval
@Contract(pure=true) private void valideTimeInterval(@NonNull @NonNull LocalDateTime startDate, @NonNull @NonNull LocalDateTime endDate) throws de.gustavblass.commons.exceptions.IllegalArgumentException Checks that:
- the start date comes before the end date,
- both LocalDateTimes are on the same day,
- the difference between the two dates is at most
MAXIMUM_QUERY_TIMESPAN.
- Parameters:
startDate- The date that is supposed to come first.endDate- The date that is supposed to come second.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException- If the validation failed.
-
bookRoom
@PreAuthorize("hasRole('REGULAR')") @NonNull public @NonNull Reservation bookRoom(@NonNull @NonNull BookRoomDTO reservationDto) throws NotFoundException, IllegalStateException, TimeTravelException, ReversedTimePeriodException, ReservationPeriodTooSmallException, ReservationPeriodTooBigException, InvalidUseException, ConflictingReservationsException, InvalidSeatCountException, de.gustavblass.commons.exceptions.IllegalArgumentException Saves a new
Reservationto the database, based on the givenBookRoomDTO, if theFilterReservationsDTO.roomis free between the start time and end time.Publishes a
ReservationConfirmedEventwith the new Reservation.- Parameters:
reservationDto- The Reservation that shall be placed, represented as a data-transfer object.- Returns:
- The Reservation created from the DTO.
- Throws:
NotFoundException- If theRoom.numberdoes not exist.TimeTravelException- If the reservation starts in the past.ReversedTimePeriodException- IfFilterReservationsDTO.fromdoes not come beforeFilterReservationsDTO.to.ReservationPeriodTooSmallException- IfFilterReservationsDTO.tocomes too shortly afterFilterReservationsDTO.from.ReservationPeriodTooBigException- If the period betweenFilterReservationsDTO.fromandFilterReservationsDTO.tois too long.ConflictingReservationsException- If there is already at least one Reservation for the Room in the specified time period.InvalidUseException- If theBookRoomDTO.useis not anIntendedUseenum value.InvalidSeatCountException- If theBookRoomDTO.typeisReservationType.SEAT_RESERVATION, but no seat count greater than zero is specified.de.gustavblass.commons.exceptions.IllegalArgumentException- If the Room,FilterReservationsDTO.date, start time, end time, type or intended use is not specified.IllegalStateException- If the logged-in user cannot be found.
-
exists
Checks whether the database contains aReservationwith the givenReservation.id.- Parameters:
id- The Reservation identifier that shall be tested.- Returns:
- True if the ID exists, false if there is no such Reservation.
-
delete
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") public void delete(@NonNull @NonNull Long id) throws NotFoundException Permanently removes theReservationwith the givenReservation.idfrom the database.- Parameters:
id- The identifier of the Reservation that shall no longer be stored in the system.- Throws:
NotFoundException- If no Reservation with the given ID exists.
-
deleteOwnReservation
@PreAuthorize("hasRole('REGULAR')") public void deleteOwnReservation(@NonNull @NonNull Long id) throws NotFoundException Permanently removes the current Actor'sReservationwith the givenReservation.idfrom the database.- Parameters:
id- The identifier of the Reservation that shall no longer be stored in the system.- Throws:
NotFoundException- If no Reservation with the given ID exists.- Implementation Note:
- This is a separate method from
delete(Long), because otherwise a malicious user would be able to tell if the Reservation ID exists, even if the Reservation belongs to a different applicant. This timing attack would be facilitated by the fact that it takes slightly longer to check the user's authorisation than to check the ID's existence. (The authorisation can only be checked if the ID exists.)
-
deleteOwnReservations
Permanently removes the Reservations which belong to the current Actor and have the given identifiers from the database, if all of them exist.- Parameters:
ids- The identifiers of the Reservations that shall no longer be stored in the system.- Throws:
NotFoundException- If at least for one of the given IDs, there is no corresponding Reservation in the database. Then, no Reservation is deleted.
-
delete
@PreAuthorize("hasRole('FACULTY_ADMINISTRATION')") public void delete(@NonNull @NonNull Iterable<Long> ids) throws NotFoundException Permanently removes the Reservations with the given identifiers from the database, if all of them exist.- Parameters:
ids- The identifiers of the Reservations that shall no longer be stored in the system.- Throws:
NotFoundException- If at least for one of the given IDs, there is no corresponding Reservation in the database. Then, no Reservation is deleted.
-
computeMaximumOccupancy
@Contract(pure=true) @NonNull public static @NonNull Integer computeMaximumOccupancy(@NonNull @NonNull Collection<SeatReservation> reservations) Calculates the highest amount of overlapping SeatReservations among the given ones.- Parameters:
reservations- The SeatReservations among which the maximum occupancy shall be calculated.- Returns:
- The maximum number of simultaneously occupied seats.
-
computeMaximumOccupancy
@Contract(pure=true) @NonNull public static @NonNull Optional<Integer> computeMaximumOccupancy(@NonNull @NonNull Collection<SeatReservation> reservations, @NonNull @NonNull Integer threshold) Calculates the highest amount of overlapping SeatReservations among the given ones.
This method will return early if the preliminary result exceeds this given threshold. This may improve the performance, depending on the number of given SeatReservations.
- Parameters:
reservations- The SeatReservations among which the maximum occupancy shall be calculated.threshold- The maximum occupancy tolerated before the computation shall be cancelled.- Returns:
- The maximum number of simultaneously occupied seats. Empty if the number is greater than the given threshold.
-
convertSeatReservationChainToSchedule
@Contract(pure=true) @NonNull public static @NonNull @Unmodifiable List<RoomScheduleItem> convertSeatReservationChainToSchedule(@NonNull @NonNull Collection<SeatReservation> chain, int capacity) Alias forconvertSeatReservationChainToSchedule(Collection, int, MergeOptions)with merging disabled. -
convertSeatReservationChainToSchedule
@Contract(pure=true) @NonNull public static @NonNull @Unmodifiable List<RoomScheduleItem> convertSeatReservationChainToSchedule(@NonNull @NonNull Collection<SeatReservation> chain, int capacity, @NonNull @NonNull MergeOptions mergeOptions) Creates a new list of RoomScheduleItems corresponding to the given chain of SeatReservations.
If the SeatReservations overlap, then there will be a separate RoomScheduleItem for each overlapping time period, with the
RoomScheduleItem.occupancycomputed accordingly.If two consecutive RoomScheduleItems have the same occupancy, they will be merged into one single RoomScheduleItem. (Consecutive means that the first item's
RoomScheduleItem.endTimeis equal to the second item'sRoomScheduleItem.startTime.)For RoomScheduleItems with
RoomStatus.FREE, theRoomScheduleItem.occupancywill be null.- Parameters:
chain- The SeatReservations that shall be converted to a schedule.capacity- TheRoom.capacityof theRoomfor which the schedule of the given SeatReservations shall be built. This number is used to determine whether the Room isRoomStatus.PARTIALLY_OCCUPIEDorRoomStatus.OCCUPIED. If the Room shall never be fully occupied, thecapacityparameter shall be set toInteger.MAX_VALUE.mergeOptions- The resulting schedule items will be merged according to these options.- Returns:
- The schedule built from the given SeatReservations. Empty if no SeatReservations were given.
- Implementation Note:
- This method has a time complexity of
O(n log n)wherenis theCollection.size().
-
convertToTimespanOverlaps
@Contract(pure=true) @NonNull public static @NonNull List<TimespanOverlapItem> convertToTimespanOverlaps(@NonNull @NonNull Collection<SeatReservation> reservations) Turns the given SeatReservations into TimespanOverlapItems, sorted by the
TimespanOverlapItem.time.For each SeatReservation, there will be one TimespanOverlapItem whose
TimespanOverlapItem.typeisTimespanOverlapItem.Type.STARTand one TimespanOverlapItem whose type isTimespanOverlapItem.Type.END. On both TimespanOverlapItems, theTimespanOverlapItem.valuewill be set to theSeatReservation.count.- Parameters:
reservations- The SeatReservations that shall be converted.- Returns:
- The corresponding TimespanOverlapItems. Twice as many as SeatReservations.
- Implementation Note:
- As a safety precaution, only seat counts greater than zero will be used (otherwise: one). This method
has a time complexity of
O(n log n)wherenis theCollection.size().
-
merge
@Contract(pure=true) @NonNull public static @NonNull @Unmodifiable List<RoomScheduleItem> merge(@NonNull @NonNull List<RoomScheduleItem> schedule, int roomCapacity, @NonNull @NonNull MergeOptions mergeOptions) Merges consecutive RoomScheduleItems in the given schedule according to the givenMergeOptions.- Parameters:
schedule- The RoomScheduleItems that shall be merged according to the given options.roomCapacity- TheRoom.capacityof theRoomwhich the Reservations represented by the given RoomScheduleItems correspond to. (Necessary to compute a useful merge score.)mergeOptions- Define the behaviour of this method.- Returns:
- A new unmodifiable schedule where a certain number of items has been merged. The time order of the given items is preserved, of course.
- Implementation Note:
- This method has a time complexity of
O(n)wherenis the size of the given schedule.
-
merge
@Contract(pure=true) @NonNull private static @NonNull RoomScheduleItem merge(@NonNull @NonNull RoomScheduleItem item1, @NonNull @NonNull RoomScheduleItem item2) Creates a new
RoomScheduleItemfrom the two given ones.The
RoomScheduleItem.startTimewill be the start time ofitem1, while theRoomScheduleItem.endTimewill be the end time ofitem2.The
RoomScheduleItem.statuswill be the highest one betweenitem1's anditem2's statuses.The
RoomScheduleItem.occupancywill bemax{item1.occupancy, item2.occupancy}.- Parameters:
item1- The RoomScheduleItem that comes first in time.item2- The RoomScheduleItem that comes second in time.- Returns:
- The merging result.
- Implementation Note:
- This method has constant time complexity.
-