-
Notifications
You must be signed in to change notification settings - Fork 0
Added tests for optional attendees and modified query to handle optional attendees #22
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,28 +28,45 @@ public final class FindMeetingQuery { | |
| public int compare(Event a, Event b) { | ||
| return Long.compare(a.getWhen().start(), b.getWhen().start()); | ||
| }}; | ||
|
|
||
| public Collection<TimeRange> query(Collection<Event> eventCollection, MeetingRequest request) { | ||
|
|
||
| public Collection<TimeRange> query(Collection<Event> eventCollection, MeetingRequest request) { | ||
| Collection<String> attendees = new ArrayList<String>(); | ||
| attendees.addAll(request.getOptionalAttendees()); | ||
| attendees.addAll(request.getAttendees()); | ||
| if (request.getOptionalAttendees().isEmpty()) { | ||
| attendees.removeAll(request.getOptionalAttendees()); | ||
| } | ||
|
Comment on lines
+36
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unnecessary. You would be removing nothing? |
||
| if (request.getAttendees().isEmpty()) { | ||
| attendees.removeAll(request.getAttendees()); | ||
| } | ||
|
Comment on lines
+39
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here. |
||
| if (queryHelper(eventCollection, request, attendees).isEmpty()) { | ||
| attendees.removeAll(request.getOptionalAttendees()); | ||
| return queryHelper(eventCollection, request, attendees); | ||
| } | ||
| return queryHelper(eventCollection, request, attendees); | ||
|
Comment on lines
+42
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should store the result of each of these calls to queryHelper rather than re-calling it. You always end up calling it one more time than necessary, which wastes time given it is a somewhat complex function. |
||
| } | ||
|
|
||
| private static Collection<TimeRange> queryHelper(Collection<Event> groupOfEvents, MeetingRequest request, Collection<String> attendees) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can probably just call the first parameter "events." Using the plural name makes clear that it's some sort of collection/list, so the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this note below, but I'll echo it here.
Consider accepting List<> or ArrayList<>. |
||
| // Too long of a request | ||
| if (request.getDuration() > TimeRange.WHOLE_DAY.duration()) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| // No events or attendees | ||
| List<Event> events = new ArrayList<Event>(eventCollection); | ||
| List<String> meetingAttendees = new ArrayList<String>(request.getAttendees()); | ||
| if (events.isEmpty() || meetingAttendees.isEmpty()) { | ||
| // No events | ||
| List<Event> events = new ArrayList<Event>(groupOfEvents); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you doing this? Is it because you're accepting "groupOfEvents" as a "Collection"? Consider updating the function signature to accept a better collection type. |
||
| if (events.isEmpty()) { | ||
| return Arrays.asList(TimeRange.WHOLE_DAY); | ||
| } | ||
|
|
||
|
|
||
| // Ignores unattended events | ||
| List<Event> importantEvents = new ArrayList<Event>(); | ||
| for (Event event : events) { | ||
| if (!Collections.disjoint(event.getAttendees(), request.getAttendees())) { | ||
| if (!Collections.disjoint(event.getAttendees(), attendees)) { | ||
| importantEvents.add(event); | ||
| } | ||
| } | ||
|
|
||
| // If the list of important events is empty, return the whole day | ||
| if (importantEvents.isEmpty()) { | ||
| return Arrays.asList(TimeRange.WHOLE_DAY); | ||
|
|
@@ -62,7 +79,7 @@ public Collection<TimeRange> query(Collection<Event> eventCollection, MeetingReq | |
| List<TimeRange> acceptableMeetingTimes = new ArrayList<TimeRange>(); | ||
|
|
||
| // Add the event that starts first | ||
| acceptableMeetingTimes.add(TimeRange.fromStartEnd(0, importantEvents.get(0).getWhen().start(), false)); | ||
| acceptableMeetingTimes.add(TimeRange.fromStartEnd(TimeRange.START_OF_DAY, importantEvents.get(0).getWhen().start(), false)); | ||
|
|
||
| // Set the end time and start time as the end of the first event | ||
| int latestEventEnd = importantEvents.get(0).getWhen().end(); | ||
|
|
@@ -72,15 +89,15 @@ public Collection<TimeRange> query(Collection<Event> eventCollection, MeetingReq | |
| for (Event event : importantEvents) { | ||
| start = event.getWhen().start(); | ||
|
|
||
| if (start > latestEventEnd) { | ||
| if (start >= latestEventEnd) { | ||
| if (rangeLessThanDuration(TimeRange.fromStartEnd(latestEventEnd, start, false), request.getDuration())) { | ||
| acceptableMeetingTimes.add(TimeRange.fromStartEnd(latestEventEnd, start, false)); | ||
| latestEventEnd = event.getWhen().end(); | ||
| } | ||
| latestEventEnd = event.getWhen().end(); | ||
| } | ||
|
|
||
| if (start < latestEventEnd) { | ||
| if (start <= latestEventEnd) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This |
||
| if (latestEventEnd < event.getWhen().end()) { | ||
| latestEventEnd = event.getWhen().end(); | ||
| if (rangeLessThanDuration(TimeRange.fromStartEnd(latestEventEnd, start, false), request.getDuration())) { | ||
|
|
@@ -110,11 +127,12 @@ public Collection<TimeRange> query(Collection<Event> eventCollection, MeetingReq | |
| } | ||
| } | ||
|
|
||
| return acceptableMeetingTimes; | ||
| return acceptableMeetingTimes; | ||
| } | ||
|
|
||
| // Check if the meeting duration fits within a given time range | ||
| private static boolean rangeLessThanDuration(TimeRange range, long meetingDuration) { | ||
| return (range.duration() >= meetingDuration); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rm extra line |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, heres the collection.
Yeah, I said before to use generic types, but
Collectionis too generic, lol. List is probably what you want.