package tcra.exercises.resolve_random_access_capability_private;

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

public class ResolveRandomAccessCapability_RemoveContract extends
       MethodContract
{
    protected MathString<MathType> lb_self;
    protected MathString<MathType> self;
    protected MathInteger          lb_remove;
    protected MathInteger          remove;
    protected MathInteger          lb_index;
    protected MathInteger          index;
   
   public ResolveRandomAccessCapability_RemoveContract()
   {
      super("Remove");
      precondition =
              new ResolveRandomAccessCapability_PreRemoveAssertion();
      postcondition =
             new ResolveRandomAccessCapability_PostRemoveAssertion();
      args = new Vector<MethodArgument>();
      args.add(new MethodArgument("i", new IntegerType()));
      args.add(new MethodArgument("k", new IntType()));
      returnType = new VoidType();
   } // 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_remove = mip.parse(new StringBuilder(l[1]));
      lb_index  = mip.parse(new StringBuilder(l[2]));
      self      = msp.parse(new StringBuilder(l[3]));
      remove    = mip.parse(new StringBuilder(l[4]));
      index     = mip.parse(new StringBuilder(l[5]));
      
      if(lb_self   == null || self   == null ||
         lb_remove == null || remove == null ||
         lb_index  == null || index  == null)
         throw new MathFormatException(
                   "One or more unparsable arguments.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_remove);
      state.add(lb_index);
      state.add(self);
      state.add(remove);
      state.add(index);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])

   public String toString()
   {
      return("<html>Operation Remove(replaces I:Integer; "+
             "restores k: Integer; updates S: StackI);<br>"+
             "requires:<br>" +precondition+
             "<br>ensures:<br>" +postcondition);
   } // end toString
   
   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_remove);
      state.add(lb_index);
      state.add(self);
      state.add(remove);
      state.add(index);
      
       return("<html>Operation Remove(replaces I:Integer; "+
              "restores k: Integer; updates S: StackI);<br>"+
              "requires:<br>" + precondition.toInstanceString(state)+
              "<br>ensures:<br>" +
              postcondition.toInstanceString(state));
   } // end toInstanceString
} // end class
