-
Notifications
You must be signed in to change notification settings - Fork 0
Deepen calendar time-window handling #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
backend/lined/src/main/java/io/backend/lined/event/service/CalendarTimeWindow.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package io.backend.lined.event.service; | ||
|
|
||
| import io.backend.lined.common.exception.BadRequestException; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Validated half-open calendar time window where start is inclusive and end is exclusive. | ||
| * | ||
| * @param start inclusive start instant | ||
| * @param end exclusive end instant | ||
| */ | ||
| record CalendarTimeWindow(OffsetDateTime start, OffsetDateTime end) { | ||
|
|
||
| /** | ||
| * Creates a validated time window. | ||
| * | ||
| * @param start inclusive start instant | ||
| * @param end exclusive end instant | ||
| * @param message error message used when the bounds are invalid | ||
| * @return validated calendar time window | ||
| * @throws BadRequestException when either bound is null or start is not before end | ||
| */ | ||
| static CalendarTimeWindow of(OffsetDateTime start, OffsetDateTime end, String message) { | ||
| if (start == null || end == null || !start.isBefore(end)) { | ||
| throw new BadRequestException(message); | ||
| } | ||
| return new CalendarTimeWindow(start, end); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether this window overlaps another window using half-open interval semantics. | ||
| * | ||
| * @param other validated window to compare with | ||
| * @return true when the windows share a non-empty time range | ||
| */ | ||
| boolean overlaps(CalendarTimeWindow other) { | ||
| return start.isBefore(other.end) && end.isAfter(other.start); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the shared bounds between this window and another window. | ||
| * | ||
| * @param other validated window to compare with | ||
| * @return overlap bounds, or empty when the windows do not overlap | ||
| */ | ||
| Optional<CalendarTimeWindow> overlapWith(CalendarTimeWindow other) { | ||
| if (!overlaps(other)) { | ||
| return Optional.empty(); | ||
| } | ||
| // The overlap of two validated windows is always valid, so this can skip re-validation. | ||
| return Optional.of(new CalendarTimeWindow(max(start, other.start), min(end, other.end))); | ||
|
Pan14ek marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Returns the later of two timestamps. | ||
| * | ||
| * @param first first timestamp | ||
| * @param second second timestamp | ||
| * @return later timestamp | ||
| */ | ||
| private static OffsetDateTime max(OffsetDateTime first, OffsetDateTime second) { | ||
| return first.isAfter(second) ? first : second; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the earlier of two timestamps. | ||
| * | ||
| * @param first first timestamp | ||
| * @param second second timestamp | ||
| * @return earlier timestamp | ||
| */ | ||
| private static OffsetDateTime min(OffsetDateTime first, OffsetDateTime second) { | ||
| return first.isBefore(second) ? first : second; | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
backend/lined/src/main/java/io/backend/lined/event/service/EventConflictAnalyzer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package io.backend.lined.event.service; | ||
|
|
||
| import io.backend.lined.event.api.EventConflictDto; | ||
| import io.backend.lined.event.api.EventMapper; | ||
| import io.backend.lined.event.domain.EventEntity; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Finds overlapping event pairs and maps them into calendar conflict responses. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class EventConflictAnalyzer { | ||
|
|
||
| private final EventMapper mapper; | ||
|
|
||
| /** | ||
| * Finds all pairwise conflicts in the supplied event order. | ||
| * | ||
| * @param events events that already match the scheduling search window | ||
| * @return conflict pairs with calculated overlap bounds | ||
| */ | ||
| public List<EventConflictDto> findConflicts(List<EventEntity> events) { | ||
| var windows = events.stream().map(this::windowOf).toList(); | ||
| List<EventConflictDto> conflicts = new ArrayList<>(); | ||
| for (int i = 0; i < events.size(); i++) { | ||
| for (int j = i + 1; j < events.size(); j++) { | ||
| addConflict(events.get(i), windows.get(i), events.get(j), windows.get(j), conflicts); | ||
| } | ||
| } | ||
| return conflicts; | ||
| } | ||
|
|
||
| private void addConflict(EventEntity first, CalendarTimeWindow firstWindow, | ||
| EventEntity second, CalendarTimeWindow secondWindow, | ||
| List<EventConflictDto> conflicts) { | ||
| firstWindow.overlapWith(secondWindow).ifPresent(overlap -> | ||
| conflicts.add(new EventConflictDto( | ||
| mapper.toDto(first), mapper.toDto(second), overlap.start(), overlap.end()))); | ||
| } | ||
|
|
||
| private CalendarTimeWindow windowOf(EventEntity event) { | ||
| if (event.getStartAt() == null || event.getEndAt() == null | ||
| || !event.getStartAt().isBefore(event.getEndAt())) { | ||
| throw new IllegalStateException( | ||
| "Stored event %d has invalid time window".formatted(event.getId())); | ||
| } | ||
| return new CalendarTimeWindow(event.getStartAt(), event.getEndAt()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.