package eva.schedule;
import java.util.*;
import eva.schedule.Appointment.SlotReservation;
public class EventSchedule extends Schedule {
Event owner;
public EventSchedule(Event owner, VisitsChecklist visitsChecklist) {
super(owner, visitsChecklist, 0);
int countSlots = owner.countSlots();
//initial the slots
beginIndex = owner.getSlotIndex();
slots = new ArrayList<Slot>(countSlots);
super.owner = this.owner = owner;
int i;
for(i=0; i<countSlots; i++) {
slots.add(i, new Slot(this, beginIndex + i));
}
}
@Override
public void deleteAppointment(Appointment appointment) {
if(!appointments.remove(appointment)) {
throw new IllegalArgumentException("this appointment is not registered for this event");
}
visitsChecklist.markVisitUndone(appointment.getVisitor());
for(Slot slot : slots) {
slot.remove(appointment);
}
if(appointments.size() == 0) {
lastAppointment = firstAppointment = null;
}
else {
//update lastAppointment and firstAppointment
if (appointment.equals(lastAppointment)) {
lastAppointment = (Appointment) (appointments.toArray()[0]);
}
if (appointment.equals(firstAppointment)) {
firstAppointment = (Appointment) (appointments.toArray()[0]);
}
}
}
@Override
public void reserveConflictingSlots(Appointment appointment, int slotIndex,
int appointmentSlotsCount) {
try {
reserveSlots(appointment, slotIndex, appointmentSlotsCount,true);
} catch (ConflictingAppointmentsException e) {
throw new RuntimeException("exception thrown, which was not expected", e);
}
}
@Override
protected void reserveSlots(Appointment appointment, int slotIndex,
int appointmentSlotsCount, boolean conflicting)
throws ConflictingAppointmentsException {
/*
* you can reserve multiple appointments at the same time, because
* this is an event.
* so we don't need any conflict checking ;)
*/
//check whether the appointment is in the range of this event
if(slotIndex != owner.getSlotIndex() || appointmentSlotsCount != owner.countSlots() ) {
throw new IllegalArgumentException("the appointment for this event does not have the same slotIndex or the same slotsCount");
}
//reserve now all slots
for(Slot slot : slots) {
slot.reserveConflicting(appointment, false);
}
Appointment.SlotReservation reservation = appointment.new SlotReservation(slotIndex, slots.toArray(new Slot[0]));
appointment.setHostReservation(reservation);
/*
* set lastAppointment and firstAppointment, it doesn't really
* matter to which appointment they are pointing to, because
* they all start and end at the same time, because this is
* an event
*/
if(firstAppointment == null) {
firstAppointment = appointment;
}
lastAppointment = appointment;
appointments.add(appointment);
visitsChecklist.markVisitDone(appointment.getVisitor());
}
@Override
public void reserveSlots(Appointment appointment, int slotIndex,
int appointmentSlotsCount) throws ConflictingAppointmentsException {
reserveSlots(appointment, slotIndex, appointmentSlotsCount,false);
}
@Override
public Slot findFirstFreeSlotForAppointment(int index, int appointmentSlotsCount) {
if(!isSlotIndexInRange(index)) {
throw new IllegalArgumentException("index is not in the range of the schedule");
}
return slots.get(0);
}
}