package tcra.exercises.queue_reasoning_private;

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

public class QueueLengthContract extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathString<MathType> self;
   protected MathInteger          length;

   public QueueLengthContract()
   {
      super("length");
      precondition  = new TrueAssertion();
      postcondition = new QueuePostLengthAssertion();
      returnType    = new IntType();
      args          = new Vector<MethodArgument>();
      
      lb_self = self = null;
      length  = null;
   } // end ctor

   @SuppressWarnings("unchecked")
   public boolean check(String[] input) throws MathFormatException
   {
      MathIntegerParser mip = new MathIntegerParser();
      MathStringParser  msp = new MathStringParser(mip);

      lb_self = msp.parse(new StringBuilder(input[0]));
      self    = msp.parse(new StringBuilder(input[1]));
      length  = mip.parse(new StringBuilder(input[2]));
      
      if(lb_self == null || self == null || length == null)
         throw new MathFormatException(
                   "One or more unparsable arguments.");

      Vector<MathType> states = new Vector<MathType>();
      states.add(lb_self);
      states.add(self);
      states.add(length);

      return (precondition.check(states) &&
              postcondition.check(states));
   } // end check(List)

   public String toString()
   {
      return("<html>" + returnType + " " + name + "( )<br>" +
             "requires:<br>"+ precondition +
             "<br>ensures:<br>" + postcondition);
   } // end toString()

   public String toInstanceString()
   {
      Vector<MathType> argVals = new Vector<MathType>();
      argVals.add(lb_self);
      argVals.add(self);
      argVals.add(length);
     
      return("<html>" + returnType + " " + name + "( )<br>" +
             "requires:<br>"+
             precondition.toInstanceString(argVals) +
             "<br>ensures:<br>" +
             postcondition.toInstanceString(argVals));
   } // end toString(List)
} // end class
