package tcra.exercises.resolve_stack_mystery_private;

import java.util.Vector;
import tcra.math.MathFormatException;
import tcra.math.MathIntegerParser;
import tcra.math.MathString;
import tcra.math.MathStringParser;
import tcra.math.MathType;
import tcra.math.MethodContract;
import tcra.programmatic.MethodArgument;
import tcra.programmatic.StackType;
import tcra.programmatic.VoidType;

public class ResolveStackMystery_Mystery7 extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathString<MathType> lb_t;
   protected MathString<MathType> self;
   protected MathString<MathType> t;

   public ResolveStackMystery_Mystery7()
   {
      super("Mystery7");
      precondition  = new ResolveStackMystery_PreMystery6Assertion();
      postcondition = new ResolveStackMystery_PostMystery7Assertion();
      args.add(new MethodArgument("T", new StackType()));
      returnType    = new VoidType();
      
      lb_self = self = lb_t = t = null;
   } // end ctor

   public boolean check(String[] l) throws MathFormatException
   {
      MathStringParser msp = new MathStringParser(new MathIntegerParser());
      lb_self = msp.parse(new StringBuilder(l[0]));
      lb_t    = msp.parse(new StringBuilder(l[1]));
      self    = msp.parse(new StringBuilder(l[2]));
      t       = msp.parse(new StringBuilder(l[3]));
      
      if(lb_self == null || self == null ||
         lb_t == null || t == null)
         throw new MathFormatException(
                   "One or more unparsable objects.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(lb_t);
      state.add(self);
      state.add(t);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])

   public String toString()
   {
      return("<html>Operation Mystery7(updates S, T: 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_t);
      state.add(self);
      state.add(t);
      
      return("<html>Operation Mystery7(updates S, T: Stack);<br>"+
             "requires " + precondition.toInstanceString(state) +
             "<br>ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
}
