package tcra.exercises.resolve_stack_mystery_private;

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

public class ResolveStackMystery_Mystery3 extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathInteger          lb_mystery;
   protected MathString<MathType> self;
   protected MathInteger          mystery;
   
   public ResolveStackMystery_Mystery3()
   {
      super("Mystery3");
      precondition  = 
                   new ResolveStackMystery_StringLengthGTZAssertion();
      postcondition = new ResolveStackMystery_PostMystery3Assertion();
      args.add(new MethodArgument("I", new IntegerType()));
      returnType    = new VoidType();
      
      lb_self    = self    = null;
      lb_mystery = mystery = 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_mystery = mip.parse(new StringBuilder(l[1]));
      self       = msp.parse(new StringBuilder(l[2]));
      mystery    = mip.parse(new StringBuilder(l[3]));
      
      if(lb_self == null || self == null ||
            lb_mystery == null || mystery == null)
            throw new MathFormatException(
                      "One or more unparsable objects.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_mystery);
      state.add(self);
      state.add(mystery);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])
   
   public String toString()
   {
      return("<html>Operation Mystery3(replaces I: Integer; "+
             "restores 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_mystery);
      state.add(self);
      state.add(mystery);
      
      return("<html>Operation Mystery3(replaces I: Integer; "+
            "restores S: Stack);<br>"+
            "requires "+precondition.toInstanceString(state)+
            "<br>ensures "+postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
