001    package lrs;
002    
003    /**
004     * Mutable linear recursive structure.
005     <pre>
006     * Visitor Pattern: Serves as a host capable of executing algorithms which are
007     * visitors.
008     </pre>
009     * @author Dung X. Nguyen and Stephen Wong  Copyright 2005 - All rights reserved.
010     * @author Mathias Ricken - Copyright 2008 - All rights reserved.
011     * @since 8/25/05
012     */
013    public class LRStruct<T> {
014        /**
015         * The state of of this <code>LRStruct</code>.
016         */
017        private ANode<T> _head;
018        
019        /**
020         * Initializes this <code>LRStruct</code> to the empty state.
021         */
022        public LRStruct() {
023            _head = new EmptyNode<T>();
024        }
025        
026        /**
027         * Returns "()" if empty, otherwise returns the contents of this
028         * <code>LRStruct</code> enclosed in parentheses.
029         */
030        public String toString()  {
031            return _head.toString(this);
032        }
033        
034        /**
035         * Inserts dat to the front of this LRStruct.<br>
036         * post condition: <code>getFirst()</code> now returns dat.
037         * @param dat data to be inserted.
038         * @return this <code>LRStruct</code>
039         */
040        public final LRStruct<T> insertFront(T dat) {
041            return _head.insertFront(dat, this);
042        }
043        
044        /**
045         * Removes and returns this <code>LRStruct</code>'s first.
046         */
047        public final T removeFront() {
048            return _head.removeFront(this);
049        }
050        
051        /**
052         * Gets the first data element from this <code>LRStruct</code>
053         */
054        public final T getFirst() {
055            return _head.getFirst (this);
056        }
057        
058        /**
059         * Sets first data element to a new value.
060         * @param dat replaces the existing first for this <code>LRStruct</code>.
061         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
062         * @return this <code>LRStruct</code>
063         */
064        public final LRStruct<T> setFirst(T dat) {
065            return _head.setFirst (dat, this);
066        }
067        
068        /**
069         * Gets the rest of this <code>LRStruct</code>.
070         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
071         */
072        public final LRStruct<T> getRest() {
073            return _head.getRest(this);
074        }
075        
076        /**
077         * Sets a new tail for this <code>LRStruct</code>.<br>
078         * post condition: <code>getRest()</code> now returns tail.
079         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
080         * @return this <code>LRStruct</code>
081         */
082        public final LRStruct<T> setRest(LRStruct<T> tail) {
083            return _head.setRest(tail, this);
084        }
085        
086        /**
087         * Hook method to execute an algorithm with a given input and return
088         * an appropriate output object.
089         * @param algo an algorithm (!= null) that operates on this LRStruct.
090         * @param inp input variable argument list of objects needed by visitor algo.
091         * @return output object resulting from the execution of algo.
092         */
093        public final <R,P> R execute(IAlgo<T, R, P> algo, P ... inp) {
094            return _head.execute(this, algo, inp);
095        }
096        
097        /* Package access only: */
098        
099        /**
100         * Initiazes this <code>LRStruct</code> with a given head node.
101         * @param node != null.
102         */
103        LRStruct(ANode<T> node)  {
104            _head = node;
105        }
106        
107        /**
108         * Changes the head node (i.e. state) of this <code>LRStruct</code>.
109         * @param head replaces the exisiting state of this <code>LRStruct</code>.
110         * @return this <code>LRStruct</code>
111         */
112        final LRStruct<T> setHead (ANode<T> head) {
113            _head = head;
114            return this;
115        }
116        
117        /**
118         * Gets the head node (i.e. state) of this <code>LRStruct</code>.
119         * @return the head node.
120         */
121        final ANode<T> getHead () {
122            return _head;
123        }
124    }
125