generalHelpers
Class ListNode<T extends TreeNode>

java.lang.Object
  extended by generalHelpers.TreeNode
      extended by generalHelpers.ListNode<T>
All Implemented Interfaces:
java.lang.Iterable<T>

public class ListNode<T extends TreeNode>
extends TreeNode
implements java.lang.Iterable<T>

Base class for lists of AST elements.

(See TreeNode for a discussion of AST nodes in general)

List phyla have a distinct set of operations for constructing and accessing lists. For each phylum named X there is a phylum called ListNode .

An empty list is created with new ListNode(lineno). Elements may be appended to the list using either addElement() or appendElement(). appendElement returns the list itself, so calls to it may be chained, as in list.appendElement(Foo).appendElement(Bar).appendElement(Baz).

ListNode implements the interface Iterable to iterate through lists. If you are not familiar with that interface, look it up in the Java API documentation. Here's an example of iterating through a list:

   for (Class_ c : classes) {
   ... do something with c ...
   }
 
If you need to have an integer index:
         int i = -1;
   for (Class_ c : classes) {
   i++;
   ... do something with c and i...
   }
 
To loop through two lists of equal length in parallel:
         Iterator i1, i2;
         Class_ c1, c2;
         i1 = myListNode1.iterator();
         i2 = myListNode2.iterator();
 
         for(; i1.hasNext(); ) {
           c1 = i1.next();
           c2 = i2.next();
     ... do something with c1 and c2...
   }
 


Field Summary
 
Fields inherited from class generalHelpers.TreeNode
lineNumber
 
Constructor Summary
ListNode(int lineNumber)
          Builds a new list node
 
Method Summary
 ListNode<T> appendElement(T element)
          Appends an element to the list.
 ListNode<T> copy()
          no type-safety provided by the copy-method
 java.util.Vector<T> copyElements()
          Creates a deep copy of this list.
 void dump(java.io.Writer out, int n)
          Pretty-prints this list to this output stream.
 int getLength()
          Retreives the length of the list.
 java.util.Iterator<T> iterator()
          Returns an iterator for easy data-access.
 java.lang.String toString()
          Returns a string representation of this list.
 
Methods inherited from class generalHelpers.TreeNode
copyAbstractSymbol, copyBoolean, dumpAbstractSymbol, dumpBoolean, dumpLine, getLineNumber, set
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ListNode

public ListNode(int lineNumber)
Builds a new list node

Parameters:
lineNumber - line in the source file from which this node came.
Method Detail

copyElements

public java.util.Vector<T> copyElements()
Creates a deep copy of this list. None of the elements are shared between the lists, e.g. all elements are duplicated (which is what "deep copy" means).

Returns:
a copy of this elements vector

getLength

public int getLength()
Retreives the length of the list.

Returns:
the length of the list

appendElement

public ListNode<T> appendElement(T element)
Appends an element to the list.

The appendElement() method returns the list modified list of the appropriate type, so that it can be used like this: l.appendElement(i).appendElement(j).appendElement(k);

Parameters:
element - a node to append

copy

public ListNode<T> copy()
no type-safety provided by the copy-method

Specified by:
copy in class TreeNode
Returns:
a copy of this node

dump

public void dump(java.io.Writer out,
                 int n)
          throws java.io.IOException
Pretty-prints this list to this output stream.

Specified by:
dump in class TreeNode
Parameters:
out - the output stream
n - the number of spaces to indent the output
Throws:
java.io.IOException

toString

public java.lang.String toString()
Returns a string representation of this list.

Overrides:
toString in class java.lang.Object
Returns:
a string representation

iterator

public java.util.Iterator<T> iterator()
Returns an iterator for easy data-access.

Specified by:
iterator in interface java.lang.Iterable<T extends TreeNode>
Returns:
an iterator over the elements in the ListNode