package lrs; /** * Mutable linear recursive structure. *

Visitor Pattern: Serves as a host capable of executing algorithms which are * visitors.

* @author Dung X. Nguyen Copyright 2005 - All rights reserved. * @since 02/09/05 */ public class LRStruct { /** * The state of of this LRStruct. */ private ANode _head; /** * Initializes this LRStruct to the empty state. */ public LRStruct() { _head = EmptyNode.Singleton; } /** * 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(Object dat) { return _head.insertFront(this, dat); } /** * Removes and returns this LRStruct's first. */ public final Object removeFront() { return _head.removeFront(this); } /** * Gets the first data element from this LRStruct */ public final Object 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(Object dat) { return _head.setFirst (this, dat); } /** * 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(this, tail); } /** * 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 Object execute(IAlgo algo, Object... 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; } }