package tcra.exercises.resolve_bounded_queue_private;

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

public class ResolveBoundedQueue_Swap_First_EntryContract extends
      MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathInteger          lb_swap;
   protected MathString<MathType> self;
   protected MathInteger          swap;
   
   public ResolveBoundedQueue_Swap_First_EntryContract()
   {
      super("Swap_First_Entry");
      precondition  = new ResolveBoundedQueue_StringLengthGTZAssertion();
      postcondition = new ResolveBoundedQueue_PostSwap_First_EntryAssertion();
      args.add(new MethodArgument("I", new IntegerType()));
      returnType = new VoidType();
   
      lb_self = self = null;
      lb_swap = swap = 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_swap = mip.parse(new StringBuilder(l[1]));
      self    = msp.parse(new StringBuilder(l[2]));
      swap    = mip.parse(new StringBuilder(l[3]));
      
      if(lb_self == null || lb_swap == null ||
            self == null || swap == null)
            throw new MathFormatException(
                      "One or more unparsable arguments.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_swap);
      state.add(self);
      state.add(swap);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])
   
   public String toString()
   {
      return("<html>Operation Swap_First_Entry(updates I: Integer; "+
             "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_swap);
      state.add(self);
      state.add(swap);
      
      return("<html>Operation Swap_First_Entry(updates I: Integer; "+
            "updates Q: Queue);<br>"+
            "requires " + precondition.toInstanceString(state) +
            "<br>ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
