// 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; }
// returns an ArrayList of ranges for the indicated day, // as defined by the Calendar class // may return ranges which overlap (occurs when the previous // night's range is over 24 hours long) @SuppressWarnings("unchecked") public ArrayList<Range> getRanges(int calendarDay) { ArrayList<Range> ranges = (ArrayList<Range>) _openRanges.get(calendarDay - 1).clone(); ArrayList<Range> yesterdayRanges = _openRanges.get((calendarDay - 2 + 7) % 7); if (!yesterdayRanges.isEmpty()) { Range lastYesterday = yesterdayRanges.get(yesterdayRanges.size() - 1); if (lastYesterday.overnight() && !lastYesterday.getEnd().equals(Time.beginning)) ranges.add(0, new Range(Time.beginning, lastYesterday.getEnd())); } return ranges; }