public Date getNextAppointment(Date start, Date now) { Interval appointment = getNextAppointment(new Interval(now.getTime() - start.getTime(), Moment.MILLISECOND)); Date result; if (appointment != null) result = new Date(start.getTime() + appointment.getDuration(Moment.MILLISECOND)); else result = null; return result; }
public Interval getNextAppointment(Interval interval) { Interval appointment = null; Interval iter = interval.plus(new Interval()); Interval elapsed = new Interval(); for (AppointmentBlock appointmentBlock : appointmentBlocks) { // How long is this appointment block? Interval duration = appointmentBlock.getDuration(); // Now, figure out if our next appointment is in this block. if (duration != null) { // This means this appointment block has a discrete duration. if (duration.shorterThan(iter)) { // We'll still have some time left over after we zoom // past this guy, so hang out. Nothing to do here. } else { if (duration.longerThan(iter)) { // Look in this interval for an appointment. If we // find any at all after our remaining time left, // that's our guy. appointment = appointmentBlock.getNextAppointmentAfter(iter); if (appointment != null) { // Sweet. We got our appointment. We're done here, // and we'll break out of the loop at the bottom. } else { // This is weird, but OK. This happens when there's // a "blank space" at the end of an interval past // all the appointments. Comes from saying something // like "every 15 minutes for 40 minutes", which // would have a 10 minute "gap" after the first 2 // meetings. } } else { // It's exactly the same length. We need to pass this on to // the next appointment block, so we just do nothing here. } } } else { // This means "Forever". Ask this guy for the next appointment, // and then break out. If we don't find it here, it doesn't // exist. appointment = appointmentBlock.getNextAppointmentAfter(iter); break; } // If we've found our next appointment we're done, so break out. if (appointment != null) break; // If we're not done, "fast forward" past this appointment block. iter = iter.minus(duration); elapsed = elapsed.plus(duration); // If we have no time left in our iterator, set ourselves to 0. if (iter.getDuration() <= 0) iter = new Interval(); } Interval result; if (appointment != null) result = elapsed.plus(appointment); else result = null; return result; }