package lrs.visitor;
import lrs.*;
/**
* Reverses the host using local anonymous inner class as a helper visitor.
* @author DXN
*/
public class Reverse implements IAlgo {
public static final Reverse Singleton = new Reverse ();
private Reverse () {
}
/**
* Does nothing: the empty host is the reverse of itself.
* @return null
*/
public Object emptyCase(LRStruct host, Object... nu) {
return null;
}
/**
* Asks for help to recursively move down the host, removes one element at
* a time and inserting it to the front of the host. In the end, the host
* is reversed.
* @return null
*/
public Object nonEmptyCase(final LRStruct host, Object... nu) {
return host.getRest().execute(new IAlgo (){
/**
* Does nothing because the end of the original host is reached.
* From host to h is the reverse of the original list.
* @param h the remaining tail of the list to be reversed.
* @param nu not used.
* @return null.
*/
public Object emptyCase(LRStruct h, Object... nu) {
return null;
}
/**
* Removes the current list's first and insert it to the front of
* the original host and recurs.
* @param h the remaining tail of the list to be reversed.
* @param nu not used.
* @return null.
*/
public Object nonEmptyCase(LRStruct h, Object... nu) {
Object hFirst = h.removeFront(); // h has "advanced".
host.insertFront (hFirst); // host is in the closure!
return h.execute(this);
}
});
}
}