package tcra.exercises.resolve_appending_capability_private;

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

public class ResolveAppendingCapability_Append_QContract extends
      MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathString<MathType> lb_append;
   protected MathString<MathType> self;
   protected MathString<MathType> append;

   public ResolveAppendingCapability_Append_QContract()
   {
      super("Append_Q");
      precondition  =
               new ResolveAppendingCapability_PreAppend_QAssertion();
      postcondition =
              new ResolveAppendingCapability_PostAppend_QAssertion();
      args.add(new MethodArgument("P", new QueueType()));
      returnType = new VoidType();
   } // end ctor

   public boolean check(String[] l) throws MathFormatException
   {
      MathStringParser msp = new MathStringParser(
                             new MathIntegerParser());
      lb_self   = msp.parse(new StringBuilder(l[0]));
      lb_append = msp.parse(new StringBuilder(l[1]));
      self      = msp.parse(new StringBuilder(l[2]));
      append    = msp.parse(new StringBuilder(l[3]));
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_append);
      state.add(self);
      state.add(append);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])

   public String toString()
   {
      return("<html>Operation Append_Q(clears P: Queue; "+
             "updates Q: Queue);<br>"+
             "requires " + precondition +
             "<br>ensures " + postcondition);
   } // end toString()
   
   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_append);
      state.add(self);
      state.add(append);
      
      return("<html>Operation Append_Q(clears P: Queue; "+
            "updates Q: Queue);<br>"+
            "requires " + precondition.toInstanceString(state) +
            "<br>ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
