001    package lrs;
002    
003    
004    /**
005     * Mutable linear recursive structure.
006     * <p/>
007     * <pre>
008     * <p/>
009     * Visitor Pattern: Serves as a host capable of executing algorithms which are
010     * <p/>
011     * visitors.
012     * <p/>
013     * </pre>
014     *
015     * @author Dung X. Nguyen  Copyright 2001 - All rights reserved.
016     * @since 10/09/01
017     */
018    
019    public class LRStruct {
020    
021        /**
022         * The state of of this <code>LRStruct</code>.
023         */
024    
025        private ANode _head;
026    
027    
028        /**
029         * Initializes this <code>LRStruct</code> to the empty state.
030         */
031    
032        public LRStruct() {
033    
034            _head = EmptyNode.Singleton;
035    
036        }
037    
038    
039        /**
040         * Returns "()" if empty, otherwise returns the contents of this
041         * <p/>
042         * <code>LRStruct</code> enclosed in parentheses.
043         */
044    
045        public String toString() {
046    
047            return _head.toString(this);
048    
049        }
050    
051    
052        /**
053         * Inserts dat to the front of this LRStruct.<br>
054         * <p/>
055         * post condition: <code>getFirst()</code> now returns dat.
056         *
057         * @param dat data to be inserted.
058         *
059         * @return this <code>LRStruct</code>
060         */
061    
062        public final LRStruct insertFront(Object dat) {
063    
064            return _head.insertFront(dat, this);
065    
066        }
067    
068    
069        /**
070         * Removes and returns this <code>LRStruct</code>'s first.
071         */
072    
073        public final Object removeFront() {
074    
075            return _head.removeFront(this);
076    
077        }
078    
079    
080        /**
081         * Gets the first data element from this <code>LRStruct</code>
082         */
083    
084        public final Object getFirst() {
085    
086            return _head.getFirst(this);
087    
088        }
089    
090    
091        /**
092         * Sets first data element to a new value.
093         *
094         * @param dat replaces the existing first for this <code>LRStruct</code>.
095         *
096         * @return this <code>LRStruct</code>
097         *
098         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
099         */
100    
101        public final LRStruct setFirst(Object dat) {
102    
103            return _head.setFirst(dat, this);
104    
105        }
106    
107    
108        /**
109         * Gets the rest of this <code>LRStruct</code>.
110         *
111         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
112         */
113    
114        public final LRStruct getRest() {
115    
116            return _head.getRest(this);
117    
118        }
119    
120    
121        /**
122         * Sets a new tail for this <code>LRStruct</code>.<br>
123         * <p/>
124         * post condition: <code>getRest()</code> now returns tail.
125         *
126         * @return this <code>LRStruct</code>
127         *
128         * @throws NoSuchElementException if this <code>LRStruct</code> is empty.
129         */
130    
131        public final LRStruct setRest(LRStruct tail) {
132    
133            return _head.setRest(tail, this);
134    
135        }
136    
137    
138        /**
139         * Hook method to execute an algorithm with a given input and return
140         * <p/>
141         * an appropriate output object.
142         *
143         * @param algo an algorithm (!= null) that operates on this LRStruct.
144         * @param inp  input object needed by visitor algo.
145         *
146         * @return output object resulting from the execution of algo.
147         */
148    
149        public final Object execute(IAlgo algo, Object inp) {
150    
151            return _head.execute(algo, inp, this);
152    
153        }
154    
155    
156    
157    /* Package access only: */
158    
159    
160    
161        /**
162         * Initiazes this <code>LRStruct</code> with a given head node.
163         *
164         * @param node != null.
165         */
166    
167        LRStruct(ANode node) {
168    
169            _head = node;
170    
171        }
172    
173    
174        /**
175         * Changes the head node (i.e. state) of this <code>LRStruct</code>.
176         *
177         * @param head replaces the existing state of this <code>LRStruct</code>.
178         *
179         * @return this <code>LRStruct</code>
180         */
181    
182        final LRStruct setHead(ANode head) {
183    
184            _head = head;
185    
186            return this;
187    
188        }
189    
190    
191        /**
192         * Gets the head node (i.e. state) of this <code>LRStruct</code>.
193         *
194         * @return the head node.
195         */
196    
197        final ANode getHead() {
198    
199            return _head;
200    
201        }
202    
203    }
204    
205    
206