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