001 package lrs.visitor;
002 import lrs.*;
003 import logic.*;
004 // This visitor removes the node whose value matches the param's value.
005 // Returns true if remove was successful, returns false othewise.
006
007 public class RemoveItem implements IAlgo{
008 public static RemoveItem Singleton = new RemoveItem();
009
010 private RemoveItem(){
011 }
012
013 public Object emptyCase(LRStruct host, Object... param) {
014 return BooleanFactory.Singleton.makeBoolean(false); // item not found.
015 }
016
017 public Object nonEmptyCase(final LRStruct host, final Object... param) {
018 //Integer item = (Integer) param[0]; // saves recasting all the time
019
020 // Remove this node if the local data equals the param.
021 return BooleanFactory.Singleton.makeBoolean(param[0].equals(host.getFirst())).
022 execute( new IBooleanAlgo() {
023 public Object trueCase(IBoolean h, Object... inp) {;
024 host.removeFront();
025 return BooleanFactory.Singleton.makeBoolean(true);
026 }
027 public Object falseCase(IBoolean h, Object... inp) {
028 return host.getRest().execute(RemoveItem.this, param);
029 }
030 });
031
032 // if (item.equals((Integer)host.getFirst()))
033 // {
034 // host.removeFront();
035 // return new Boolean(true);
036 // }
037 // else // otherwise recurse and return the result
038 // {
039 // return host.getRest().execute(this, param);
040 // }
041 }
042 }