package tcra.exercises.resolve_bounded_stack_private;

import java.util.*;
import tcra.math.*;
import tcra.programmatic.*;
import tcra.exercises.general_assertions.TrueAssertion;

public class ResolveBoundedStack_DepthContract extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathString<MathType> self;
   protected MathInteger          depth;
   
   public ResolveBoundedStack_DepthContract()
   {
      super("Depth");
      precondition  = new TrueAssertion();
      postcondition = new ResolveBoundedStack_PostDepthAssertion();
      args          = new Vector<MethodArgument>();
      returnType    = new IntType();
      
      lb_self = self = null;
      depth = 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]));
      self    = msp.parse(new StringBuilder(l[1]));
      depth   = mip.parse(new StringBuilder(l[2]));
      
      if(lb_self == null || self == null || depth == null)
            throw new MathFormatException(
                      "One or more unparsable arguments.");
      
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(self);
      state.add(depth);
      
      return(precondition.check(state) &&
             postcondition.check(state));
   } // end check(String[])

   public String toString()
   {
      return("<html>Operation Depth(restores S: Stack): Integer;<br>"+
             "ensures " + postcondition);
   } // end toString()
   
   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_self);
      state.add(self);
      state.add(depth);
      
      return("<html>Operation Depth(restores S: Stack): Integer;<br>"+
             "ensures " + postcondition.toInstanceString(state));
   } // end toInstanceString()
} // end class
