001    
002    package lrs.visitor;
003    
004    import counter.*;
005    import lrs.*;
006    
007    public class SetNth implements IAlgo{
008      /** @SBGen Singleton Variable   */
009      public static SetNth Singleton = new SetNth(); 
010      private SetNth(){}
011      
012      /**
013       * @param host 
014       * @param param 
015       * @return 
016       */
017      public Object emptyCase(LRStruct host, Object... param) {
018        return (null); // No such element exists.
019      }
020      
021      /**
022       * @param host 
023       * @param param  param[0] = ICounter, param[1] = data 
024       * @return 
025       */
026      public Object nonEmptyCase(LRStruct host, Object... param) {
027        return ((ICounter) param[0]).execute(counterAlgo, host, param[1]); 
028      }
029      
030      
031      private static final ICounterAlgo counterAlgo = new ICounterAlgo(){
032        /**
033         * @param host 
034         * @param param param[0] = LRStruct host, param[1] = data
035         * @return 
036         */
037        public Object zeroCase(ICounter chost, Object... param) {
038          ((LRStruct)param[0]).setFirst(param[1]);
039          return null;
040        }
041        
042        /**
043         * @param host 
044         * @param param param[0] = LRStruct host, param[1] = data
045         * @return 
046         */
047        public Object nonZeroCase(ICounter chost, Object... param) {
048          return ((LRStruct)param[0]).getRest().execute(SetNth.Singleton, chost.decrement(), param[1]);
049        }
050      };    
051    }
052