001 package lrs;
002
003 import java.util.NoSuchElementException;
004
005
006 /**
007 * Represents the abstract list state. Has a concrete toString () method that
008 * <p/>
009 * uses anynomous inner classes to compute the String representation of the
010 * <p/>
011 * LRStruct owner.
012 *
013 * @author Dung X. Nguyen Copyright 2001 - All rights reserved.
014 * @since 10/09/01
015 */
016
017 abstract class ANode {
018
019 /**
020 * Uses anonymous visitor class to compute a String representation.
021 * <p/>
022 * Anonymous inner classes will be discussed soon!
023 */
024
025 String toString(LRStruct owner) {
026
027 return (String)owner.execute(new IAlgo() {
028
029 public Object emptyCase(LRStruct host, Object inp) {
030
031 return "()";
032
033 }
034
035
036 public Object nonEmptyCase(LRStruct host, Object inp) {
037
038 return "(" + host.getFirst() + host.getRest().execute(new IAlgo() {
039
040 public Object emptyCase(LRStruct h, Object i) {
041
042 return ")";
043
044 }
045
046
047 public Object nonEmptyCase(LRStruct h, Object i) {
048
049 return " " + h.getFirst() + h.getRest().execute(this, null);
050
051 }
052
053 }
054
055 , null);
056
057 }
058
059 }
060
061 , null);
062
063 }
064
065
066 /**
067 * Returns the rest LRStruct of the referencing LRStruct.
068 *
069 * @param owner the LRStruct referencing this ANode.
070 *
071 * @return the rest LRStruct of owner.
072 *
073 * @throws NoSuchElementException if empty.
074 */
075
076 abstract LRStruct getRest(LRStruct owner);
077
078
079 /**
080 * Returns the first data object of the referencing LRStruct.
081 *
082 * @param owner the LRStruct referencing this ANode.
083 *
084 * @return the rest LRStruct of owner.
085 *
086 * @throws NoSuchElementException if empty.
087 */
088
089 abstract Object getFirst(LRStruct owner);
090
091
092 /**
093 * Sets a new rest for the referencing LRStruct.
094 *
095 * @param tail the new rest for the owner LRStruct.
096 * @param owner the LRS referencing this ANode.
097 *
098 * @return <code>LRStruct</code> owner
099 *
100 * @throws NoSuchElementException if empty.
101 */
102
103 abstract LRStruct setRest(LRStruct tail, LRStruct owner);
104
105
106 /**
107 * Sets a new first data object for the referencing LRStruct.
108 *
109 * @param dat the new data object for this ANode.
110 * @param owner the LRS referencing this ANode.
111 *
112 * @return <code>LRStruct</code> owner
113 *
114 * @throws NoSuchElementException if empty.
115 */
116
117 abstract LRStruct setFirst(Object dat, LRStruct owner);
118
119
120 /**
121 * Inserts a data object at the front of the LRStruct owner.
122 *
123 * @param dat the object to be inserted at the front.
124 * @param owner the LRS referencing this ANode.
125 *
126 * @return <code>LRStruct</code> owner
127 */
128
129 abstract LRStruct insertFront(Object dat, LRStruct owner);
130
131
132 /**
133 * Removes and returns the first data object for the referencing LRStruct.
134 *
135 * @param owner the LRS referencing this ANode.
136 *
137 * @return the front data of the LRStruct owner.
138 */
139
140 abstract Object removeFront(LRStruct owner);
141
142
143 /**
144 * Executes a visitor algorithm and returns the output.
145 *
146 * @param algo the visitor algorithm to be executed.
147 * @param inp the input needed by the algorithm.
148 * @param owner the LRStruct referencing this ANode.
149 */
150
151 abstract Object execute(IAlgo algo, Object inp, LRStruct owner);
152
153 }
154
155
156