001 package lrs.visitor;
002
003 import lrs.IAlgo;
004 import lrs.LRStruct;
005
006 /**
007 * Return the n'th element in the list.
008 *
009 * @author Mathias Ricken
010 */
011 public class GetNth implements IAlgo {
012 /// singleton instance
013 public static final GetNth Singleton = new GetNth();
014
015 /// private singleton ctor
016 private GetNth() {
017 }
018
019 /**
020 * Operates on an empty LRStruct host, given an input object.
021 *
022 * @param host an empty LRStruct.
023 * @param inp index of element
024 *
025 * @return nothing
026 *
027 * @throws IllegalArgumentException
028 */
029 public Object emptyCase(LRStruct host, Object inp) {
030 throw new IllegalArgumentException("LRStruct empty");
031 }
032
033 /**
034 * Operates on a non-empty LRStruct host, given an input object.
035 *
036 * @param host a non-empty LRStruct.
037 * @param inp index of element
038 *
039 * @return an n'th element
040 */
041 public Object nonEmptyCase(LRStruct host, Object inp) {
042 int i = ((Integer)inp).intValue();
043 if (0 == i) {
044 return host.getFirst();
045 }
046 else {
047 return host.getRest().execute(this, new Integer(i - 1));
048 }
049 }
050 }