package tcra.exercises.resolve_bounded_queue_private;

import java.util.*;
import tcra.math.*;
import tcra.programmatic.*;

public class ResolveBoundedQueue_EnqueueContract extends
      MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathInteger          lb_enqu;
   protected MathString<MathType> self;
   protected MathInteger          enqu;

   public ResolveBoundedQueue_EnqueueContract()
   {
      super("Enqueue");
      precondition  = new ResolveBoundedQueue_PreEnqueueAssertion();
      postcondition = new ResolveBoundedQueue_PostEnqueueAssertion();
      args = new Vector<MethodArgument>();
      args.add(new MethodArgument("I", new IntegerType()));
      returnType = new VoidType();
      
      lb_self = self = null;
      lb_enqu = enqu = null;
   } // end ctor

   public boolean check(String[] l) throws MathFormatException
   {
      MathIntegerParser mip = new MathIntegerParser();
      MathStringParser  msp = new MathStringParser(mip);
      lb_self = msp.parse(new StringBuilder(l[0]));
      lb_enqu = mip.parse(new StringBuilder(l[1]));
      self    = msp.parse(new StringBuilder(l[2]));
      enqu    = mip.parse(new StringBuilder(l[3]));
      
      if(lb_self == null || lb_enqu == null ||
         self == null || enqu == null)
         throw new MathFormatException(
                   "One or more unparsable arguments."); 
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_enqu);
      state.add(self);
      state.add(enqu);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])

   public String toString()
   {
      return("<html>Operation Enqueue(alters E: Entry; "+
      		 "updates Q: Queue);<br>" +
      		 "requires " + precondition+
      		 "<br>ensures " + postcondition);
   }
   
   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_enqu);
      state.add(self);
      state.add(enqu);
      
      return("<html>Operation Enqueue(alters E: Entry; "+
             "updates Q: Queue);<br>" +
             "requires " + precondition.toInstanceString(state)+
             "<br>ensures " + postcondition.toInstanceString(state));
   } // end toIntstanceString
} // end class
