package tcra.exercises.resolve_one_way_list_private;

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

public class ResolveOneWayList_InsertContract extends MethodContract
{
   protected MathString<MathType> lb_prec;
   protected MathString<MathType> lb_rem;
   protected MathInteger          lb_entry;
   protected MathString<MathType> prec;
   protected MathString<MathType> rem;
   protected MathInteger          entry;

   public ResolveOneWayList_InsertContract()
   {
      super("Insert");
      precondition  = new tcra.exercises.general_assertions.TrueAssertion();
      postcondition = new ResolveOneWayList_PostInsertAssertion();
      returnType    = new VoidType();
      
      args = new Vector<MethodArgument>();
      args.add(new MethodArgument("P.Rem", new MathStringType()));
      args.add(new MethodArgument("New_Entry", new IntegerType()));
      
      lb_prec  = prec  = null;
      lb_rem   = rem   = null;
      lb_entry = entry = 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]));
      lb_entry = mip.parse(new StringBuilder(l[2]));
      prec     = msp.parse(new StringBuilder(l[3]));
      rem      = msp.parse(new StringBuilder(l[4]));
      entry    = mip.parse(new StringBuilder(l[5]));

      if(lb_prec  == null || prec  == null ||
         lb_rem   == null || rem   == null ||
         lb_entry == null || entry == 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(lb_entry);
      state.add(prec);
      state.add(rem);
      state.add(entry);

      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(lb_entry);
      state.add(prec);
      state.add(rem);
      state.add(entry);
      
      return "<html>Oper Insert( clr New_Entry: Integer;"+
             " reassigns P: List_Position );<br>"+
             "ensures<br>"+postcondition.toInstanceString(state);
   } // end toInstanceString()
   
   public String toString()
   {
      return "<html>Oper Insert( clr New_Entry: Integer;"+
             " reassigns P: List_Position );<br>"+
             "ensures<br>"+postcondition.toString();
   } // end toString()
} // end class
