walkthroughs/week-5-tdd/project/src/main/java/com/google/sps/FindMeetingQuery.java
/**
   * Finds the time that is the most workable time slot for the meetings.
   *
   * @param events  collection of events happening that day
   * @param request meeting that needs to be scheduled for the attendees
   * @return collection of time when the meeting can be scheduled.
   */
Finds the time that is the most workable time slot for the meetings.
@param events  collection of events happening that day
@param request meeting that needs to be scheduled for the attendees
@return collection of time when the meeting can be scheduled.
Finds the time that is the most workable time slot for the meetings . @param events collection of events happening that day @param request meeting that needs to be scheduled for the attendees @return collection of time when the meeting can be scheduled .
public Collection<TimeRange> query(Collection<Event> events, MeetingRequest request) {
    long meetingDuration = request.getDuration();
    if (meetingDuration > TimeRange.WHOLE_DAY.duration()) return Collections.emptyList();
    if (request.getAttendees().isEmpty() && request.getOptionalAttendees().isEmpty()) return Arrays.asList(TimeRange.WHOLE_DAY);
    if (events.isEmpty()) return Arrays.asList(TimeRange.WHOLE_DAY);
    ArrayList<TimeRange> workableTimesForMandatoryAttendees = findWorkableTimes(findConflictingTimes(events, request, true), meetingDuration);
    if (request.getOptionalAttendees().isEmpty()) return workableTimesForMandatoryAttendees;
    ArrayList<TimeRange> workableTimesForOptionalAttendees = findWorkableTimes(findConflictingTimes(events, request, false), meetingDuration);
    if (request.getAttendees().isEmpty()) return workableTimesForOptionalAttendees;
    if (workableTimesForOptionalAttendees.isEmpty()) return workableTimesForMandatoryAttendees;
    ArrayList<TimeRange> workableForBoth = new ArrayList<>();
    int mandatoryPointer = 0;
    int optionalPointer = 0;
    while (mandatoryPointer < workableTimesForMandatoryAttendees.size()
        && optionalPointer < workableTimesForOptionalAttendees.size()) {
      TimeRange workableForMandatoryAttendees = workableTimesForMandatoryAttendees.get(mandatoryPointer);
      TimeRange workableForOptionalAttendees = workableTimesForOptionalAttendees.get(optionalPointer);
      if (workableForMandatoryAttendees.start() >= workableForOptionalAttendees.end()) {
        optionalPointer++;
      } else if (workableForOptionalAttendees.start() >= workableForMandatoryAttendees.end()) {
        mandatoryPointer++;
      } else if (workableForOptionalAttendees.contains(workableForMandatoryAttendees)) {
        workableForBoth.add(workableForMandatoryAttendees);
        mandatoryPointer++;
      } else if (workableForMandatoryAttendees.contains(workableForOptionalAttendees)) {
        workableForBoth.add(workableForOptionalAttendees);
        optionalPointer++;
      } else if (workableForOptionalAttendees.overlaps(workableForMandatoryAttendees)) {
        int mandatoryWorkableTimeStart = workableForMandatoryAttendees.start();
        int optionalWorkableTimeStart = workableForOptionalAttendees.start();
        if (meetingDuration < Math.abs(mandatoryWorkableTimeStart - optionalWorkableTimeStart)) {
          workableForBoth.add(workableForMandatoryAttendees);
          mandatoryPointer++;
          optionalPointer++;
        }
      }
    }
    if (workableForBoth.isEmpty()) return workableTimesForMandatoryAttendees;
    return workableForBoth;
  }
public Collection < TimeRange > query ( Collection < Event > events , MeetingRequest request ) { long meetingDuration = request . getDuration ( ) ; if ( meetingDuration > TimeRange . WHOLE_DAY . duration ( ) ) return Collections . emptyList ( ) ; if ( request . getAttendees ( ) . isEmpty ( ) && request . getOptionalAttendees ( ) . isEmpty ( ) ) return Arrays . asList ( TimeRange . WHOLE_DAY ) ; if ( events . isEmpty ( ) ) return Arrays . asList ( TimeRange . WHOLE_DAY ) ; ArrayList < TimeRange > workableTimesForMandatoryAttendees = findWorkableTimes ( findConflictingTimes ( events , request , true ) , meetingDuration ) ; if ( request . getOptionalAttendees ( ) . isEmpty ( ) ) return workableTimesForMandatoryAttendees ; ArrayList < TimeRange > workableTimesForOptionalAttendees = findWorkableTimes ( findConflictingTimes ( events , request , false ) , meetingDuration ) ; if ( request . getAttendees ( ) . isEmpty ( ) ) return workableTimesForOptionalAttendees ; if ( workableTimesForOptionalAttendees . isEmpty ( ) ) return workableTimesForMandatoryAttendees ; ArrayList < TimeRange > workableForBoth = new ArrayList < > ( ) ; int mandatoryPointer = 0 ; int optionalPointer = 0 ; while ( mandatoryPointer < workableTimesForMandatoryAttendees . size ( ) && optionalPointer < workableTimesForOptionalAttendees . size ( ) ) { TimeRange workableForMandatoryAttendees = workableTimesForMandatoryAttendees . get ( mandatoryPointer ) ; TimeRange workableForOptionalAttendees = workableTimesForOptionalAttendees . get ( optionalPointer ) ; if ( workableForMandatoryAttendees . start ( ) >= workableForOptionalAttendees . end ( ) ) { optionalPointer ++ ; } else if ( workableForOptionalAttendees . start ( ) >= workableForMandatoryAttendees . end ( ) ) { mandatoryPointer ++ ; } else if ( workableForOptionalAttendees . contains ( workableForMandatoryAttendees ) ) { workableForBoth . add ( workableForMandatoryAttendees ) ; mandatoryPointer ++ ; } else if ( workableForMandatoryAttendees . contains ( workableForOptionalAttendees ) ) { workableForBoth . add ( workableForOptionalAttendees ) ; optionalPointer ++ ; } else if ( workableForOptionalAttendees . overlaps ( workableForMandatoryAttendees ) ) { int mandatoryWorkableTimeStart = workableForMandatoryAttendees . start ( ) ; int optionalWorkableTimeStart = workableForOptionalAttendees . start ( ) ; if ( meetingDuration < Math . abs ( mandatoryWorkableTimeStart - optionalWorkableTimeStart ) ) { workableForBoth . add ( workableForMandatoryAttendees ) ; mandatoryPointer ++ ; optionalPointer ++ ; } } } if ( workableForBoth . isEmpty ( ) ) return workableTimesForMandatoryAttendees ; return workableForBoth ; }
Finds the time that is the most workable time slot for the meetings.
Finds the time that is the most workable time slot for the meetings .
// No meeting can happen more than a day. // If there are no attendees including optional, the meeting can happen anytime during the day. // If there are no events for the attendees, the meeting can happen whole day. // If there are no optional attendees, then provide time for mandatory attendees. // If there are no mandatory attendees, then provide time for optional attendees. // If optional attendees could not attend, then provide time for mandatory attendees only. // If allowable time range overlaps then find if meeting can happen during that time. // If time conflicts for optional attendees to attend, then provide time for mandatory attendees only.