package tcra.exercises.queue_reasoning_private;

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

public class QueueFrontContract extends MethodContract
{
   protected MathString<MathType> lb_self;
   protected MathString<MathType> self;
   protected MathInteger          front;
   
   public QueueFrontContract()
   {
      super("front");
      precondition  = new StringLengthGTZAssertion();
      postcondition = new QueuePostFrontAssertion();
      returnType    = new IntegerType();
      args          = new Vector<MethodArgument>();
      
      lb_self = null;
      self    = null;
      front   = null;
   } // end ctor

   public boolean check(String[] input) throws MathFormatException
   {
      /* Parse input to obtain MathType objects */
      MathIntegerParser mip = new MathIntegerParser();
      MathStringParser  msp = new MathStringParser(mip);
      lb_self = msp.parse(new StringBuilder(input[0]));
      self    = msp.parse(new StringBuilder(input[1]));
      front   = mip.parse(new StringBuilder(input[2]));
 
      if(front == null || lb_self == null || self == null)
         throw new MathFormatException(
                   "One or more unparsable arguments.");

      Vector<MathType> values = new Vector<MathType>();
      values.add(lb_self);
      values.add(self);
      values.add(front);

      return (precondition.check(values) &&
              postcondition.check(values));
   } // end check(List<Object>)

   public String toString()
   {
      String ret = new String("<html>" + returnType +
                              " " + name + "(");

      for(int i = 0; i < args.size(); ++i)
      {
         ret += args.get(i).getType() + " " + args.get(i);
         if(i != args.size()-1) ret += ", ";
      } // end for

      ret += ")<br>requires:<br>" + precondition + "<br>ensures:<br>"+
             postcondition;

      return ret;
   } // end toString()

   public String toInstanceString()
   {
      Vector<MathType> argVals = new Vector<MathType>();
      argVals.add(lb_self);
      argVals.add(self);
      argVals.add(front);
      
      String ret = new String("<html>" + returnType +
                              " " + name + "(");

      for(int i = 0; i < args.size(); ++i)
      {
         ret += args.get(i).getType() + " " + args.get(i);
         if(i != args.size()-1) ret += ", ";
      } // end for

      ret += ")<br>requires:<br>\t" +
             precondition.toInstanceString(argVals) +
             "<br>ensures:<br>\t" +
             postcondition.toInstanceString(argVals);

      return ret;
   }
} // end class
