package tcra.exercises.resolve_one_way_list_private;

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

public class ResolveOneWayList_LengthOfPrecContract extends MethodContract
{
   protected MathString<MathType> lb_prec;
   protected MathString<MathType> lb_rem;
   protected MathString<MathType> prec;
   protected MathString<MathType> rem;
   protected MathInteger          len;

   public ResolveOneWayList_LengthOfPrecContract()
   {
      super("Length_of_Prec");
      precondition  = new tcra.exercises.general_assertions.TrueAssertion();
      postcondition = new ResolveOneWayList_PostLengthOfPrecAssertion();
      returnType    = new IntType();
      
      args = new Vector<MethodArgument>();
      args.add(new MethodArgument("P.Rem", new MathStringType()));
      
      lb_prec = prec = null;
      lb_rem  = rem  = null;
      len     = null;
   } // end ctor
   
   @Override
   public boolean check(String[] l) throws MathFormatException
   {
      MathIntegerParser mip = new MathIntegerParser();
      MathStringParser msp  = new MathStringParser(mip);
      
      lb_prec = msp.parse(new StringBuilder(l[0]));
      lb_rem  = msp.parse(new StringBuilder(l[1]));
      prec    = msp.parse(new StringBuilder(l[2]));
      rem     = msp.parse(new StringBuilder(l[3]));
      len     = mip.parse(new StringBuilder(l[4]));

      if(lb_prec == null || prec == null ||
         lb_rem  == null || rem  == null  ||
         len     == null)
         throw new MathFormatException(
                   "One or more unparsable arguments.");

      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_prec);
      state.add(lb_rem);
      state.add(prec);
      state.add(rem);
      state.add(len);

      return(precondition.check(state) &&
             postcondition.check(state));
   }

   public String toInstanceString()
   {
      Vector<MathType> state = new Vector<MathType>();
      state.add(lb_prec);
      state.add(lb_rem);
      state.add(prec);
      state.add(rem);
      state.add(len);
      
      return "<html>Oper Length_of_Prec( rest P: List_Position ): Integer;<br>"+
             "ensures<br>"+postcondition.toInstanceString(state);
      
   } // end toInstanceString()
   
   public String toString()
   {
      return "<html>Oper Length_of_Prec( rest P: List_Position ): Integer;<br>"+
             "ensures<br>"+postcondition.toString();
   } // end toString()
} // end class
