package create;

import java.util.Collection;
import java.util.List;

/**
 * Database of all the saved/past Schedules.
 * 
 * @author cdreszer
 *
 */
public abstract class ScheduleDB {
   public Collection<Schedule> schedules;
   
   /**
    * Adds a Schedule to the database.
    * @param sched - Schedule to be added
    * 
      pre:
         //
         // There is no schedule in the input db with the same name
         // as the schedule to be added.
         //
         !exists (Schedule other; schedules.contains(other);
               sched.name.equals(other.name));

      post:
         //    
         // A schedule is in the output db if and only if it is the schedule
         // to be added or it is in the input db.
         //
         forall (Schedule other; 
            schedules'.contains(sched) iff
            other.equals(sched) || schedules.contains(other));
            
    */
   public abstract void add(Schedule sched);
   
   
   /**
    * Removes a Schedule from the database.
    * @param sched - Schedule to be removed
    * 
      pre:
         //
         // The given Schedule is in the ScheduleDB.
         //
         schedules.contains(sched);
         
      post:
         //
         // A schedule is in the output db if and only if it is not the existing
         // schedule to be removed and it is in the input db.
         //
         forall (Schedule other;
            schedules'.contains(other) iff
               !other.equals(sched) && schedules.contains(other));
      
    */
   public abstract void remove(Schedule sched);
   
   /**
    * Find a schedule by a unique name.
    * 
      post:
         //
         // If there is a schedule with the given name in the input db, then the
         // output schedule is equal to that schedule, otherwise the output schedule
         // is empty.
         //
         exists (Schedule found ; 
                 schedules.contains(found) ; 
                 found.name.equals(name) && found.equals(return)) 
            ||
         !exists (Schedule found ; 
                  schedules.contains(found) ;
                  found.name.equals(name) && return == null);
      
    */
   public Schedule findByName(String name) {
         Schedule return_ = null;
	 boolean b;
         for (Schedule found = null ; 
                  schedules.contains(found) ; 
                      b = found.name.equals(name) && return_ == null);
	 return null;
   }
   /* SPEST PROBLEM:
     The Spest checker gives these error messages for the findByName postcond:

   Invalid invocation: equals(antlr.TestGenerator$arguments_return@413249b) at line: 73  at character: 28
   Expected type: null, but found: boolean at line: 73 character: 42
   Invalid invocation: equals(antlr.TestGenerator$arguments_return@10bcbb94) at line: 77  at character: 29
   Expected type: null, but found: boolean at line: 77 character: 43

      The findByName method body has a version of the postcondition logic that
      compiles OK with javac, in particular the expression

          found.name.equals(name)

      that produces the Spest error.
    */
   
   /**
    * Returns sorted list of Schedules.
    * 
      post:
         //
         // The output list is sorted lexicographically by name of 
         // Schedule, according to the semantics of 
         // java.lang.String.compareto().
         //
         forall (int i; (i>=0) && (i < return.size() - 1);
            return.get(i).name.compareTo(return.get(i + 1).name) < 0);
     
    */
   public abstract List<Schedule> viewSchedules();

   /**
    * Change the given old Schedule to the given new Schedule. The old and
    * new schedules must not be the same. The old schedule must already be in
    * the input db. The new schedule must meet the same conditions as for the
    * input to the add operation. 
    * 
      post:
         //
         // A schedule is in the output data if and only if it is the new
         // schedule to be added or it is in the input data, and it is not the 
         // old schedule.
         //
         forall(Schedule other;
            schedules'.contains(other) iff
               other.equals(new_sched) ||
                  (schedules.contains(other) &&
                     !other.equals(old_sched)));
              
      
    */
   public abstract void change(Schedule old_sched, Schedule new_sched);
   
}