package lrs; /** * Represents the abstract list state. Has a concrete toString () method that * uses anynomous inner classes to compute the String representation of the * LRStruct owner. * @author Dung X. Nguyen and Stephen Wong Copyright 2005 - All rights reserved. * @author Mathias Ricken - Copyright 2008 - All rights reserved. * @since 8/25/05 * @stereotype abstract state */ abstract class ANode { /** * Uses anonymous visitor class to compute a String representation. */ private final IAlgo ToStringAlgo = new IAlgo() { private final IAlgo Helper = new IAlgo() { public String emptyCase(LRStruct h, Void... i) { return ")"; } public String nonEmptyCase(LRStruct h, Void... i) { return " " + h.getFirst() + h.getRest().execute (this); } }; public String emptyCase(LRStruct host, Void... inp) { return "()"; } public String nonEmptyCase(LRStruct host, Void... inp) { return "(" + host.getFirst() + host.getRest().execute(Helper); } }; String toString(LRStruct owner) { return (String)owner.execute (ToStringAlgo); } /** * 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); /** * 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 T getFirst(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. * @return LRStruct owner */ abstract LRStruct setRest(LRStruct tail, LRStruct owner); /** * Sets a new first data object for the referencing LRStruct. * @param first the new data object for this ANode. * @param owner the LRS referencing this ANode. * @throw java.util.NoSuchElementException if empty. * @return LRStruct owner */ abstract LRStruct setFirst(T dat, LRStruct owner); /** * Inserts a data object at the front of the LRStruct owner. * @param dat the object to be inserted at the front. * @param owner the LRS referencing this ANode. * @return LRStruct owner */ abstract LRStruct insertFront(T dat, 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. */ abstract T removeFront(LRStruct owner); /** * Executes a visitor algorithm and returns the output. * @param owner the LRStruct referencing this ANode. * @param algo the visitor algorithm to be executed. * @param inp the inputs needed by the algorithm. */ abstract R execute(LRStruct owner, IAlgo algo, P ... inp); }