package tcra.exercises.resolve_bounded_stack_private;

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

public class ResolveBoundedStack_PopContract extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathInteger          lb_pop;
   protected MathString<MathType> self;
   protected MathInteger          pop;
   
   public ResolveBoundedStack_PopContract()
   {
      super("Pop");
      precondition  =
                  new ResolveBoundedStack_StringLengthGTZAssertion();
      postcondition = new ResolveBoundedStack_PostPopAssertion();
      args          = new Vector<MethodArgument>();
      args.add(new MethodArgument("I", new IntegerType()));
      returnType    = new VoidType();
      
      lb_self = self = null;
      lb_pop  = pop  = 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_pop  = mip.parse(new StringBuilder(l[1]));
      self    = msp.parse(new StringBuilder(l[2]));
      pop     = mip.parse(new StringBuilder(l[3]));
      
      if(lb_self == null || lb_pop == null ||
            self == null || pop == null)
            throw new MathFormatException(
                      "One or more unparsable arguments.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_pop);
      state.add(self);
      state.add(pop);
      
      return(precondition.check(state) && postcondition.check(state));
   } // end ctor

   public String toString()
   {
      return("<html>Operation Pop(replaces I: Integer;"+
             " updates S: Stack);<br>"+
             "requires " + precondition +
             "<br>ensures " + postcondition);
   } // end toString()
   
   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_pop);
      state.add(self);
      state.add(pop);
      
      return("<html>Operation Pop(replaces I: Integer;"+
             " updates S: Stack);<br>"+
             "requires " + precondition.toInstanceString(state) +
             "<br>ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
