001
002 package lrs.visitor;
003
004 import lrs.*;
005 import counter.*;
006 public class LRSTrim implements IAlgo {
007 public static final LRSTrim Singleton = new LRSTrim();
008 private LRSTrim() {}
009
010 public Object emptyCase(LRStruct host, Object... param) {
011 return(null);
012 }
013
014 public Object nonEmptyCase(LRStruct host, Object... param) {
015 return( ((ICounter) param[0]).execute(counterAlgo, host));
016
017 }
018
019 /**
020 * Helper algo for the ICounter
021 */
022 private static final ICounterAlgo counterAlgo = new ICounterAlgo() {
023 public Object zeroCase(ICounter chost, Object... param) {
024 ((LRStruct)param[0]).setRest(new LRStruct());
025 ((LRStruct)param[0]).removeFront();
026 return(null);
027 }
028
029 public Object nonZeroCase(ICounter chost, Object... param) {
030 return ((LRStruct) param[0]).getRest().execute(LRSTrim.Singleton,chost.decrement()); // recurse on the rest of the list
031 }
032 };
033 }
034