

package lrs.visitor;



import lrs.*;

import counter.*;

public class LRSTrim implements IAlgo {

  public static final LRSTrim Singleton = new LRSTrim();

  private LRSTrim() {}

  

  public Object emptyCase(LRStruct host, Object... param) {

    return(null);

  }

  

  public Object nonEmptyCase(LRStruct host, Object... param) {

    return( ((ICounter) param[0]).execute(counterAlgo, host));

    

  }

  

  /**

   * Helper algo for the ICounter

   */

  private static final ICounterAlgo counterAlgo  = new ICounterAlgo() {

    public Object zeroCase(ICounter chost, Object... param) {

      ((LRStruct)param[0]).setRest(new LRStruct());

      ((LRStruct)param[0]).removeFront();

      return(null);

    }

    

    public Object nonZeroCase(ICounter chost, Object... param) {

      return ((LRStruct) param[0]).getRest().execute(LRSTrim.Singleton,chost.decrement());  // recurse on the rest of the list

    }

  };

} 



