001    package lrs;
002    
003    /**
004     * Represents the abstract list state. Has a concrete toString () method that
005     * uses anynomous inner classes to compute the String representation of the
006     * LRStruct owner.
007     * @author Dung X. Nguyen and Stephen Wong  Copyright 2005 - All rights reserved.
008     * @since 8/25/05
009     * @stereotype abstract state
010     */
011    abstract class ANode {
012      /**
013       * Uses anonymous visitor class to compute a String representation.
014       */
015      public static final IAlgo ToStringAlgo = new IAlgo() {
016        private final IAlgo Helper = new IAlgo() {
017          public Object emptyCase(LRStruct h, Object... i) {
018            return ")";
019          }
020          
021          public Object nonEmptyCase(LRStruct h, Object... i) {
022            return " " + h.getFirst() + h.getRest().execute (this);
023          }
024        };
025        
026        public Object emptyCase(LRStruct host, Object... inp) {
027          return "()";
028        }
029    
030        public Object nonEmptyCase(LRStruct host, Object... inp) {
031          return "(" + host.getFirst() + host.getRest().execute(Helper);
032        }
033      };
034      
035      String toString(LRStruct owner) {
036        return (String)owner.execute (ToStringAlgo);
037      }
038      
039      /**
040       * Returns the tail LRStruct of the referencing LRStruct.
041       * @param owner the LRStruct referencing this ANode.
042       * @return the tail LRStruct of owner.
043       * @throw java.util.NoSuchElementException if empty.
044       */
045      abstract LRStruct getRest(LRStruct owner);
046      
047      /**
048       * Returns the first data object of the referencing LRStruct.
049       * @param owner the LRStruct referencing this ANode.
050       * @return the tail LRStruct of owner.
051       * @throw java.util.NoSuchElementException if empty.
052       */
053      abstract Object getFirst(LRStruct owner);
054      
055      /**
056       * Sets a new tail for the referencing LRStruct.
057       * @param tail the new tail for the owner LRStruct.
058       * @param owner the LRS referencing this ANode.
059       * @throw java.util.NoSuchElementException if empty.
060       * @return <code>LRStruct</code> owner
061       */
062      abstract LRStruct setRest(LRStruct tail, LRStruct owner);
063      
064      /**
065       * Sets a new first data object for the referencing LRStruct.
066       * @param first the new data object for this ANode.
067       * @param owner the LRS referencing this ANode.
068       * @throw java.util.NoSuchElementException if empty.
069       * @return <code>LRStruct</code> owner
070       */
071      abstract LRStruct setFirst(Object dat, LRStruct owner);
072      
073      /**
074       * Inserts a data object at the front of the LRStruct owner.
075       * @param dat the object to be inserted at the front.
076       * @param owner the LRS referencing this ANode.
077       * @return <code>LRStruct</code> owner
078       */
079      abstract LRStruct insertFront(Object dat, LRStruct owner);
080      
081      /**
082       * Removes and returns the first data object for the referencing LRStruct.
083       * @param owner the LRS referencing this ANode.
084       * @return the front data of the LRStruct owner.
085       */
086      abstract Object removeFront(LRStruct owner);
087      
088      /**
089       * Executes a visitor algorithm and returns the output.
090       * @param owner the LRStruct referencing this ANode.
091       * @param algo the visitor algorithm to be executed.
092       * @param inp the inputs needed by the algorithm.
093       */
094      abstract Object execute(LRStruct owner, IAlgo algo, Object... inp);
095    }
096