xtc.tree
Class GNode

java.lang.Object
  extended by xtc.tree.Node
      extended by xtc.tree.GNode
All Implemented Interfaces:
Iterable<Object>, Locatable

public abstract class GNode
extends Node

A generic node in an abstract syntax tree.

A note on memory conservation: Generic nodes created through the create(String) or create(String,int) methods can have a variable number of children. While such nodes provide considerable flexibility in creating and managing an abstract syntax tree, their implementation also has a relatively high memory and thus performance overhead. Consequently, this class provides another set of create() methods, which directly take the new node's children as arguments and return nodes specialized for that number of children. After creation, the number of children cannot be changed anymore. Code using generic nodes can test whether a node supports a variable number of children through Node.hasVariable() and convert fixed size nodes into variable sized nodes through ensureVariable(GNode).

Version:
$Revision: 1.48 $

Field Summary
static int MAX_FIXED
          The maximum number of children for generic nodes that are optimized to hold a fixed number of children.
 
Method Summary
static GNode cast(Object o)
          Cast the specified object to a generic node.
static GNode create(GNode node)
          Create a new generic node that is a (shallow) copy of the specified node.
static GNode create(String name)
          Create a new generic node with the specified name.
static GNode create(String name, boolean variable)
          Create a new generic node with the specified name.
static GNode create(String name, int capacity)
          Create a new generic node with the specified name.
static GNode create(String name, Object child)
          Create a new generic node with the specified name and child.
static GNode create(String name, Object c1, Object c2)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3, Object c4)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3, Object c4, Object c5)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3, Object c4, Object c5, Object c6)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3, Object c4, Object c5, Object c6, Object c7)
          Create a new generic node with the specified name and children.
static GNode create(String name, Object c1, Object c2, Object c3, Object c4, Object c5, Object c6, Object c7, Object c8)
          Create a new generic node with the specified name and children.
static GNode createFromPair(String name, Object base, Pair rest)
          Create a new generic node with the specified children.
static GNode createFromPair(String name, Pair p)
          Create a new generic node with the list's nodes as its children.
static GNode ensureVariable(GNode node)
          Ensure that the specified node supports a variable number of children.
 boolean equals(Object o)
          Determine whether this generic node equals the specified object.
 String getName()
          Get the name of this node.
 int hashCode()
          Get this generic node's hash code.
 boolean hasName(String name)
          Determine whether this node's name is the same as the specified name.
 boolean hasTraversal()
          Determine whether this node supports generic traversal of its children.
 boolean isGeneric()
          Determine whether this node is generic.
static boolean test(Object o)
          Test whether the specified object is a generic node, possibly wrapped in annotations.
 
Methods inherited from class xtc.tree.Node
add, add, addAll, addAll, addAll, addAll, addAllTo, addNode, contains, get, getBoolean, getBooleanProperty, getGeneric, getList, getLocation, getNode, getProperty, getString, getStringProperty, getTokenText, hasLocation, hasProperty, hasVariable, indexOf, isAnnotation, isEmpty, isList, isToken, iterator, lastIndexOf, properties, remove, removeProperty, set, setLocation, setLocation, setProperty, size, strip, toAnnotation, toList, toString, toToken, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_FIXED

public static final int MAX_FIXED
The maximum number of children for generic nodes that are optimized to hold a fixed number of children.

See Also:
Constant Field Values
Method Detail

hashCode

public int hashCode()
Get this generic node's hash code.

Overrides:
hashCode in class Object
Returns:
This node's hash code.

equals

public boolean equals(Object o)
Determine whether this generic node equals the specified object. This node equals the object, if both are generic nodes with the same names and the same number of equal children.

Overrides:
equals in class Object
Parameters:
o - The object to compare to.
Returns:
true if this generic node equals the object.

isGeneric

public final boolean isGeneric()
Determine whether this node is generic.

Overrides:
isGeneric in class Node
Returns:
true.
See Also:
GNode

hasTraversal

public final boolean hasTraversal()
Description copied from class: Node
Determine whether this node supports generic traversal of its children. The default implementation returns false.

Overrides:
hasTraversal in class Node
Returns:
true if this node supports generic traversal of its children.

getName

public final String getName()
Description copied from class: Node
Get the name of this node. For strongly typed nodes, the name is implicitly specified by the node's class. For generic nodes, the name is the generic node's explicit name. The default implementation returns the node's class name.

User-specified classes must not override this method.

Overrides:
getName in class Node
Returns:
The name.

hasName

public final boolean hasName(String name)
Description copied from class: Node
Determine whether this node's name is the same as the specified name.

User-specified classes must not override this method.

Overrides:
hasName in class Node
Parameters:
name - The name.
Returns:
true if this node's name equals the specified name.

create

public static GNode create(String name)
Create a new generic node with the specified name. The new node supports a variable number of children and has a default capacity.

Parameters:
name - The name.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           int capacity)
Create a new generic node with the specified name. The new node supports a variable number of children and has the specified capacity.

Parameters:
name - The name.
capacity - The initial capacity.
Returns:
The corresponding generic node.
Throws:
IllegalArgumentException - Signals that the capacity is negative.

create

public static GNode create(String name,
                           boolean variable)
Create a new generic node with the specified name. Invoking this method with a true variable flag is equivalent to invoking create(String). Invoking this method with a false variabel flag results in a generic node with no children.

Parameters:
name - The name.
variable - Flag for whether the new node supports a variable number of children.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object child)
Create a new generic node with the specified name and child. The new node does not support a variable number of children.

Parameters:
name - The name.
child - The child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3,
                           Object c4)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
c4 - The fourth child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3,
                           Object c4,
                           Object c5)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
c4 - The fourth child.
c5 - The fifth child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3,
                           Object c4,
                           Object c5,
                           Object c6)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
c4 - The fourth child.
c5 - The fifth child.
c6 - The sixth child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3,
                           Object c4,
                           Object c5,
                           Object c6,
                           Object c7)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
c4 - The fourth child.
c5 - The fifth child.
c6 - The sixth child.
c7 - The seventh child.
Returns:
The corresponding generic node.

create

public static GNode create(String name,
                           Object c1,
                           Object c2,
                           Object c3,
                           Object c4,
                           Object c5,
                           Object c6,
                           Object c7,
                           Object c8)
Create a new generic node with the specified name and children. The new node does not support a variable number of children.

Parameters:
name - The name.
c1 - The first child.
c2 - The second child.
c3 - The third child.
c4 - The fourth child.
c5 - The fifth child.
c6 - The sixth child.
c7 - The seventh child.
c8 - The eigth child.
Returns:
The corresponding generic node.

createFromPair

public static GNode createFromPair(String name,
                                   Pair p)
Create a new generic node with the list's nodes as its children. If possible, this method returns a fixed size node.

Parameters:
name - The name.
p - The list of children.
Returns:
The corresponding generic node.

createFromPair

public static GNode createFromPair(String name,
                                   Object base,
                                   Pair rest)
Create a new generic node with the specified children. If possible, this method returns a fixed size node.

Parameters:
name - The name.
base - The first child.
rest - The rest of the children.
Returns:
The corresponding generic node.

create

public static GNode create(GNode node)
Create a new generic node that is a (shallow) copy of the specified node.

Parameters:
node - The node to copy.
Returns:
The copy.

ensureVariable

public static GNode ensureVariable(GNode node)
Ensure that the specified node supports a variable number of children.

Parameters:
node - The generic node.
Returns:
A shallow copy of the specified node if it does not support a variable number of children; otherwise, the specified node.

test

public static final boolean test(Object o)
Test whether the specified object is a generic node, possibly wrapped in annotations.

Parameters:
o - The object.
Returns:
true if the object is a possibly annotated generic node.

cast

public static final GNode cast(Object o)
Cast the specified object to a generic node. If the specified object has any annotations, they are stripped before returning the object as a generic node.

Parameters:
o - The object.
Returns:
The object as a stripped generic node.
See Also:
test(Object)


(C) Copyright 2004-2007 Robert Grimm, New York University, IBM, and Princeton