001 package lrs.visitor;
002
003 import lrs.IAlgo;
004 import lrs.LRStruct;
005
006 /**
007 * Remove all occurances of the item specified as input, if found.
008 *
009 * @author Mathias Ricken
010 */
011 public class Remove implements IAlgo {
012 /// singleton instance
013 public static final Remove Singleton = new Remove();
014
015 /// private singleton ctor
016 protected Remove() {
017 }
018
019 /**
020 * Operates on an empty LRStruct host, given an input object.
021 *
022 * @param host an empty LRStruct.
023 * @param inp item to remove.
024 *
025 * @return null
026 */
027 public Object emptyCase(LRStruct host, Object inp) {
028 return null;
029 }
030
031 /**
032 * Operates on a non-empty LRStruct host, given an input object.
033 *
034 * @param host a non-empty LRStruct.
035 * @param inp item to remove.
036 *
037 * @return null
038 */
039 public Object nonEmptyCase(LRStruct host, Object inp) {
040 if (host.getFirst() == inp) {
041 host.removeFront();
042 // recur on this element (used to be rest)
043 return host.execute(this, inp);
044 }
045 else {
046 // recur on rest
047 return host.getRest().execute(this, inp);
048 }
049 }
050 }