-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopNameListNode.java
More file actions
388 lines (359 loc) · 12.3 KB
/
LoopNameListNode.java
File metadata and controls
388 lines (359 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package EDU.bmrb.starlibj;
import java.lang.*;
import java.util.*;
/** Holds the list of tag names that represents one nesting level
* of the loop.
* <P>
* To keep the API familiar to the user, I have tried to mimic the
* methods in java.util.vector as closely as possible.
*/
public class LoopNameListNode extends StarNode implements Cloneable
{
/** empty constructor */
public LoopNameListNode()
{
super();
nameList = new TagsVector();
}
/** copy constructor */
public LoopNameListNode( LoopNameListNode copyMe )
{
super(copyMe);
int i;
nameList = new TagsVector();
for( i = 0 ; i < copyMe.size() ; i++ )
addElement( new DataNameNode(copyMe.elementAt(i)) );
}
/** From interface <TT>Cloneable</TT>. */
public Object clone()
{
return new LoopNameListNode(this);
}
// ---------------- StarVectorLike ----------------------
// ---------------- interface ---------------------------
// TODO - none of these have any sort of double-checking on them
// yet. Need to add the ability to add columns to them when
// a name is added, but I haven't gotten that far yet because I haven't
// got the LoopRowNode and LoopTableNode up and running yet.
/** Just like the Vector method of the same name.
* Makes enough room so that there can be <tt>newSize</tt>
* elements in the node, without having to insert them
* manually one at a time. The nodes start out will
* a null value that can be replaced with <tt>setElementAt</tt>.
* @see VectorCheckType.setSize
*/
public void setSize(int newSize)
{
nameList.setSize(newSize);
}
/** Just like the Vector method of the same name.
* Returns the number of names in this list
* @see java.util.Vector.size
*/
public int size()
{ return nameList.size();
}
/** Just like the Vector method of the same name.
* True if this list has no names in it.
* @see java.util.Vector.isEmpty
*/
public boolean isEmpty()
{ return nameList.isEmpty();
}
/** Just like the Vector method of the same name.
* Gives an enumeration over the names in this block.
* @see java.util.Vector.Enumeration
*/
public Enumeration elements()
{ return nameList.elements();
}
/** Just like the Vector method of the same name.
* True if the node given is in this name list.
* @param obj The string name.
* @see java.util.Vector.contains
*/
public boolean contains(Object obj)
{ return nameList.contains(obj);
}
/** Just like the Vector method of the same name.
* Returns the integer index of the given name
* inside this list.
* @param obj The name to look for.
* @see java.util.Vector.indexOf
*/
public int indexOf(Object obj)
{ return nameList.indexOf(obj);
}
/** Just like the Vector method of the same name.
* Returns the integer index of the next occurrance
* of the given name afterthe given index.
* @param obj The name to look for.
* @param index Start searching at this point in the vector.
* @see java.util.Vector.indexOf
*/
public int indexOf(Object obj,
int index)
{ return nameList.indexOf(obj,index);
}
/** Just like the Vector method of the same name.
* Returns the lastmost integer index of the given name
* @param obj The name to look for.
* @see java.util.Vector.lastIndexOf
*/
public int lastIndexOf(Object obj)
{ return nameList.lastIndexOf(obj);
}
/** Just like the Vector method of the same name.
* Returns the lastmost integer index of the given
* name, but going no higher than the given index.
* @param obj The name to look for.
* @param index Start searching back from this point in
* the vector.
* @see java.util.Vector.lastIndexOf
*/
public int lastIndexOf(Object obj,
int index)
{ return nameList.lastIndexOf(obj,index);
}
/** Just like the Vector method of the same name.
* Returns the name object at the given index.
* @param index The index to return the name for.
* @return The returned object is a string.
* @see java.util.Vector.elementAt
*/
public DataNameNode elementAt(int index)
{ return (DataNameNode) (nameList.elementAt(index) );
}
/** Just like the Vector method of the same name.
* Returns the first name in the list
* @return The returned object is a string.
* @see java.util.Vector.firstElement
*/
public DataNameNode firstElement()
{ return (DataNameNode) ( nameList.firstElement() );
}
/** Just like the Vector method of the same name.
* Returns the last name in the list.
* @return The returned object is a string.
* @see java.util.Vector.lastElement
*/
public DataNameNode lastElement()
{ return (DataNameNode) ( nameList.lastElement() );
}
/** Just like the Vector method of the same name.
* Clobbers the name at the index given with the new name.
* @param obj The name to replace it with.
* @param index the position to replace.
* @see java.util.Vector.setElementAt
*/
public void setElementAt(Object obj,
int index)
throws WrongElementType
{
nameList.setElementAt(obj,index);
((DataNameNode)nameList.elementAt(index)).setParent(this);
}
/** Similar to the Vector method of the same name.
* Deletes the name. If this is contained inside a DataLoopNode,
* then it also removes all the values from the associated loop table
* that are under this name.
* @param index the position to remove.
* @see java.util.Vector.removeElementAt
*/
public void removeElementAt(int index)
{
((DataNameNode)nameList.elementAt(index)).setParent(null);
nameList.removeElementAt(index);
int depth = getDepth();
if( depth >= 0 )
{
StarNode par;
for( par = this ;
par != null && !(par instanceof DataLoopNode ) ;
par = par.getParent() )
{ /*void-body*/ }
if( par != null )
{ ((DataLoopNode)par).getVals().removeColumnAtDepth(
depth, index );
}
}
}
/** Just like the Vector method of the same name.
* Inserts a name just in front of the index given..
* If this is in a DataLoopNode, it also inserts all the
* appropriate columns into the data below so that it matches
* the newly inserted name. The new values will all be
* star nulls (single dot '.' values).
* @param obj The name to insert.
* @param index the position to insert it in from of.
* @see java.util.Vector.insertElementAt
*/
public void insertElementAt(Object obj,
int index)
throws WrongElementType
{
insertElementAt( obj, index, new DataValueNode( "." ) );
}
/** Identical to the version above, except that the
* value to be padded into the loop values is chosen
* by the caller instead of being a dot ('.')
* @param obj The name to insert.
* @param index the position to insert it in from of.
* @param val The value to insert in the columns below.
* @see java.util.Vector.insertElementAt
*/
public void insertElementAt( Object obj,
int index,
DataValueNode val)
throws WrongElementType
{
nameList.insertElementAt(obj,index);
((DataNameNode)nameList.elementAt(index)).setParent(this);
int depth = getDepth();
if( depth >= 0 )
{
StarNode par;
for( par = this ;
par != null && !(par instanceof DataLoopNode ) ;
par = par.getParent() )
{ /*void-body*/ }
if( par != null )
{ ((DataLoopNode)par).getVals().insertColumnAtDepth(
depth, index, val );
}
}
}
/** Just like the Vector method of the same name.
* Adds a name to the end of the list.
* Also adds a default value into the loop in a column to match up
* with the new name if the loop is there. The default value is
* a single nonquoted dot (.).
* @param obj The name to add.
* @see java.util.Vector.addElement
*/
public void addElement(Object obj)
throws WrongElementType
{
insertElementAt( obj, size() );
}
/** Just like the Vector method of the same name.
* Adds a name to the end of the list.
* Also adds new DataValueNodes into the loop in a column to match up
* with the new name where needed. The default value is
* the value passed in the parameter
* @param obj The name to add.
* @param val the new DataValueNode to copy from if need be.
* @see java.util.Vector.addElement
*/
public void addElement( Object obj, DataValueNode val)
throws WrongElementType
{
insertElementAt( obj, size(), val );
}
/** Just like the Vector method of the same name.
* Removes the name matching the one given.
* @param obj (string) The name to remove.
* @see java.util.Vector.removeElement
*/
public void removeElement( Object obj)
{
int idx = indexOf( obj );
removeElementAt( idx );
}
/** Get the depth of this name list in the loop it is in.
* (The depth is the level of nesting. If this is the outermost
* list of names, it is at depth 'zero', if it is the next level in,
* is is depth 1, and so on...)
* <PRE>
* loop_
* _tag1 # --.
* _tag2 # |-- depth 0.
* _tag3 # --'
* loop_
* _tagA # --- depth 1.
* loop_
* _tagX # --.__ depth 2.
* _tagY # --'
* </PRE>
* @return depth - negative number if this is not inside a DataLoopNode.
*/
public int getDepth()
{
StarNode par = getParent();
if( par == null )
return -1;
return ((DataLoopNameListNode)par).indexOf( this );
}
/** Returns the name of the first tag in the list, which is sometimes
* used to refer to the whole loop list.
*/
public String getLabel()
{
if( nameList.size() > 0 )
return ((DataNameNode)nameList.elementAt(0)).getLabel();
else
return null;
}
/** Find the name given in this name list.
* <P>
* The search for names is case-insensitive.
* @param searchFor look for this tag name.
*/
public VectorCheckType searchByName( String searchFor )
{
VectorCheckType retVal = new VectorCheckType();
int i;
try
{
retVal.addType( Class.forName( StarValidity.clsNameStarNode));
retVal.freezeTypes();
for( i = 0 ; i < nameList.size() ; i++ )
if( ( (DataNameNode)(nameList.elementAt(i))
).getLabel().equalsIgnoreCase(searchFor) )
retVal.addElement( nameList.elementAt(i) );
}
catch( ClassNotFoundException exc )
{ System.err.println( "Should never happen exception: " +
exc.getMessage() );
exc.printStackTrace();
return null;
}
return retVal;
}
/** Find the type given in this name list.
* @param searchFor look for this tag name.
*/
public VectorCheckType searchForType( Class type )
{
VectorCheckType retVal = new VectorCheckType();
int i;
try
{
retVal.addType( Class.forName( StarValidity.clsNameStarNode));
retVal.freezeTypes();
// Am I the right type?
if( type.isInstance(this) )
retVal.addElement( this );
if( type == Class.forName( StarValidity.clsNameDataNameNode) )
for( i = 0 ; i < nameList.size() ; i++ )
retVal.addElement( nameList.elementAt(i) );
}
catch( ClassNotFoundException exc )
{ System.err.println( "Should never happen exception: " +
exc.getMessage() );
exc.printStackTrace();
return null;
}
return retVal;
}
/** Unparse prints the contents of the StarNode object out to the
* given stream. This is essentially the inverse of the CS term
* to "parse", hence the name "Unparse". The parameter given is
* the indentation level to print things.
*/
public void Unparse( int indent )
{
// TODO - do when I understand Java printing better.
}
TagsVector nameList;
}