package tcra.exercises.resolve_deque_private;

import java.util.Vector;
import tcra.math.*;
import tcra.programmatic.*;
import tcra.exercises.resolve_preemptable_queue_private.
       ResolvePreemptableQueue_StringLengthGTZAssertion;

public class ResolveDeque_EjectContract extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathInteger          lb_eject;
   protected MathString<MathType> self;
   protected MathInteger          eject;
   
   public ResolveDeque_EjectContract()
   {
      super("Eject");
      precondition =
              new ResolvePreemptableQueue_StringLengthGTZAssertion();
      postcondition = new ResolveDeque_PostEjectAssertion();
      args.add(new MethodArgument("I", new IntegerType()));
      returnType = new VoidType();
      
      lb_self  = self  = null;
      lb_eject = eject = null;
   } // end ctor

   @Override
   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_eject = mip.parse(new StringBuilder(l[1]));
      self     = msp.parse(new StringBuilder(l[2]));
      eject    = mip.parse(new StringBuilder(l[3]));
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_eject);
      state.add(self);
      state.add(eject);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])
   
   public String toString()
   {
      return("<html>Operation Eject(replaces I: Integer; "+
             "updates Q: Deque);<br>" +
             "requires " + precondition +
             "<br>ensures " + postcondition);
   } // end toString()

   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_eject);
      state.add(self);
      state.add(eject);
      
      return("<html>Operation Eject(replaces I: Integer; "+
            "updates Q: Deque);<br>" +
            "requires " + precondition.toInstanceString(state) +
            "<br>ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
