package lrs; /** * Represents the abstract list state. * @author Dung X. Nguyen Copyright 2000 - All rights reserved. * @author Alan L. Cox * @since 02/13/01 */ abstract class ANode { /** * Converts the LRStruct to a String. * * @param owner the LRStruct referencing this ANode. * @return the String describing this ANode. */ String toString(LRStruct owner) { return (String)owner.execute( new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return "()"; } public Object nonEmptyCase(LRStruct host, Object input) { return "(" + host.getFirst() + host.getRest().execute( new IAlgo() { public Object emptyCase(LRStruct h, Object inp) { return ")"; } public Object nonEmptyCase(LRStruct h, Object inp) { return " " + h.getFirst() + h.getRest().execute(this, null); } }, null); } }, null); } /** * Returns the first data object of the referencing LRStruct. * * @param owner the LRStruct referencing this ANode. * @return the tail LRStruct of owner. * @throw java.util.NoSuchElementException if empty. */ abstract Object getFirst(LRStruct owner); /** * Sets a new first data object for the referencing LRStruct. * * @param data the new data object for this ANode. * @param owner the LRS referencing this ANode. * @throw java.util.NoSuchElementException if empty. */ abstract void setFirst(Object data, LRStruct owner); /** * Returns the tail LRStruct of the referencing LRStruct. * * @param owner the LRStruct referencing this ANode. * @return the tail LRStruct of owner. * @throw java.util.NoSuchElementException if empty. */ abstract LRStruct getRest(LRStruct owner); /** * Sets a new tail for the referencing LRStruct. * * @param tail the new tail for the owner LRStruct. * @param owner the LRS referencing this ANode. * @throw java.util.NoSuchElementException if empty. */ abstract void setRest(LRStruct tail, LRStruct owner); /** * Inserts a data object at the front of the LRStruct owner. * * @param data the object to be inserted at the front. * @param owner the LRS referencing this ANode. */ abstract void insertFront(Object data, LRStruct owner); /** * Removes and returns the first data object for the referencing LRStruct. * * @param owner the LRS referencing this ANode. * @return the front data of the LRStruct owner. * @throw java.util.NoSuchElementException if empty. */ abstract Object removeFront(LRStruct owner); /** * Executes an algorithm and returns the output. * * @param visitor the algorithm to be executed. * @param input the input needed by the algorithm. * @param owner the LRStruct referencing this ANode. */ abstract Object execute(IAlgo visitor, Object input, LRStruct owner); }