package genLRS; /** * Mutable linear recursive structure.
 * Visitor Pattern: Serves as a host capable of executing algorithms which are
 * visitors.
 
* @author Dung X. Nguyen, Stephen B. Wong Copyright 2005 - All rights reserved. */ public class LRStruct { /** * The state of of this LRStruct. */ private ANode _head; /** * Initializes this LRStruct to the empty state. */ public LRStruct() { _head = new EmptyNode(); } /** * Returns "()" if empty, otherwise returns the contents of this * LRStruct enclosed in parentheses. */ public String toString() { return _head.toString(this); } /** * Inserts dat to the front of this LRStruct.
* post condition: getFirst() now returns dat. * @param dat data to be inserted. * @return this LRStruct */ public final LRStruct insertFront(E dat) { return _head.insertFront(dat, this); } /** * Removes and returns this LRStruct's first. */ public final E removeFront() { return _head.removeFront(this); } /** * Gets the first data element from this LRStruct */ public final E getFirst() { return _head.getFirst (this); } /** * Sets first data element to a new value. * @param dat replaces the existing first for this LRStruct. * @throws NoSuchElementException if this LRStruct is empty. * @return this LRStruct */ public final LRStruct setFirst(EE dat) { return _head.setFirst (dat, this); } /** * Gets the rest of this LRStruct. * @throws NoSuchElementException if this LRStruct is empty. */ public final LRStruct getRest() { return _head.getRest(this); } /** * Sets a new tail for this LRStruct.
* post condition: getRest() now returns tail. * @throws NoSuchElementException if this LRStruct is empty. * @return this LRStruct */ public final LRStruct setRest(LRStruct tail) { return _head.setRest(tail, this); } /** * Hook method to execute an algorithm with a given input and return * an appropriate output object. * @param algo an algorithm (!= null) that operates on this LRStruct. * @param inp input object needed by visitor algo. * @return output object resulting from the execution of algo. */ public final R execute(IAlgo algo, P ... inp) { return _head.execute(this, algo, inp); } /* Package access only: */ /** * Initiazes this LRStruct with a given head node. * @param node != null. */ LRStruct(ANode node) { _head = node; } /** * Changes the head node (i.e. state) of this LRStruct. * @param head replaces the exisiting state of this LRStruct. * @return this LRStruct */ final LRStruct setHead (ANode head) { _head = head; return this; } /** * Gets the head node (i.e. state) of this LRStruct. * @return the head node. */ final ANode getHead () { return _head; } }