// pre: restaurant is open, and returns null if open // more than 24 hours from now public Time getNextCloseTime() { Range current = getCurrentRange(); if (current == null) throw new RuntimeException("Restaurant must be open"); if (current.overnight()) { ArrayList<Range> tomorrow = getRangesToModify( (new GregorianCalendar().get(Calendar.DAY_OF_WEEK) + 1 - Calendar.SUNDAY) % 7 + Calendar.SUNDAY); if (!tomorrow.isEmpty()) { Range next = tomorrow.get(0); if (!next.after(current.getEnd())) { Time now = new Time(); if (!next.overnight() && !next.getEnd().after(now)) return next.getEnd(); else return null; } } } if (current.getStart().equals(Time.beginning)) { ArrayList<Range> today = getTodayRanges(); if (today.size() > 1) { Range next = today.get(1); Time now = new Time(); if (!next.after(current)) // they overlap if (!next.overnight() || !next.getEnd().after(now)) return next.getEnd(); else return null; } } return current.getEnd(); // if (current != null) // return current.getEnd(); // return null; }
// returns minutes to the next closing time for the restaurant, 1440 if open over 24 hours, -1 if // closed for the day public int minutesToClose() { Time now = new Time(); Range current = getCurrentRange(); if (current != null) { if (current.overnight()) { ArrayList<Range> tomorrow = getRangesToModify( (new GregorianCalendar().get(Calendar.DAY_OF_WEEK) + 1 - Calendar.SUNDAY) % 7 + Calendar.SUNDAY); if (!tomorrow.isEmpty()) { Range next = tomorrow.get(0); if (!next.after(current.getEnd())) { int min = current.minutesUntilEnd(now) + next.minutesUntilEnd(next.getStart()); return min > 1440 ? 1440 : min; } } } if (current.getStart().equals(Time.beginning)) { ArrayList<Range> today = getTodayRanges(); if (today.size() > 1) { Range next = today.get(1); if (!next.after(current)) { // they overlap int min = current.minutesUntilEnd(now) + next.minutesUntilEnd(next.getStart()); return min > 1440 ? 1440 : min; } } } return current.minutesUntilEnd(now); } return -1; }
// adds a range to a particular day in sorted order, concatenating with currently // existing ranges if necessary public void addRange(int calendarDay, Range newRange) { ArrayList<Range> ranges = getRangesToModify(calendarDay); for (int i = ranges.size(); i > 0; i--) { if (newRange.after(ranges.get(i - 1))) { ranges.add(i, newRange); return; } if (ranges.get(i - 1).overlap(newRange)) { ranges.get(i - 1).concat(newRange); mergeAll(ranges); return; } } ranges.add(0, newRange); }