001 002 package lrs.visitor; 003 004 import counter.*; 005 import lrs.*; 006 007 public class NthCdr implements IAlgo { 008 009 public static final NthCdr Singleton = new NthCdr(); 010 private NthCdr() {} 011 012 /** 013 * @param host LRS to be operated on 014 * @param param A counter object 015 */ 016 public Object emptyCase(LRStruct host, Object... param) { 017 return(host); 018 } 019 020 /** 021 * @param host LRS to be operated on 022 * @param param A counter object 023 */ 024 public Object nonEmptyCase(LRStruct host, Object... param) { 025 return( ((ICounter) param[0]).execute(counterAlgo, host) ); 026 } 027 028 private static final ICounterAlgo counterAlgo = new ICounterAlgo() { 029 /** 030 * @param host counter to be operated on 031 * @param param LRS to be operated on 032 */ 033 public Object zeroCase(ICounter chost, Object... param) { 034 // param is the host of the null/nonNull cases. 035 return(param[0]); 036 } 037 038 /** 039 * @param host counter to be operated on 040 * @param param LRS to be operated on 041 */ 042 public Object nonZeroCase(ICounter chost, Object... param) { 043 return( ((LRStruct) param[0]).getRest().execute(NthCdr.Singleton, chost.decrement()) ); 044 } 045 }; 046 }