org.modeshape.sequencer.ddl.node
Class AstNode

java.lang.Object
  extended by org.modeshape.sequencer.ddl.node.AstNode
All Implemented Interfaces:
Iterable<AstNode>

@NotThreadSafe
public class AstNode
extends Object
implements Iterable<AstNode>

Utility object class designed to facilitate constructing an AST or Abstract Syntax Tree representing nodes and properties that are compatible with ModeShape graph component structure.


Constructor Summary
AstNode(AstNode parent, Name name)
          Construct a node with the supplied name and parent.
AstNode(Name name)
          Construct a node with the supplied name but without a parent.
 
Method Summary
 void addChildren(AstNode first, AstNode second)
          Add the supplied nodes at the end of the list of children.
 void addChildren(AstNode first, AstNode second, AstNode third)
          Add the supplied nodes at the end of the list of children.
 void addChildren(Iterable<AstNode> otherChildren)
          Add the supplied nodes at the end of the list of children.
 void addFirstChild(AstNode child)
          Add the supplied node to the front of the list of children.
 void addLastChild(AstNode child)
          Add the supplied node to the end of the list of children.
 AstNode clone()
           This class returns a new clone of the plan tree rooted at this node.
protected  AstNode cloneWithoutNewParent()
           
 boolean equals(Object obj)
          
 void extractChild(AstNode child)
          Remove the child node from this node, and replace that child with its first child (if there is one).
 void extractFromParent()
          Extract this node from its parent, but replace this node with its child (if there is one).
 AstNode getChild(int index)
          Get the child at the supplied index.
 int getChildCount()
          Get the number of child nodes.
 List<AstNode> getChildren()
          Get the unmodifiable list of child nodes.
 AstNode getFirstChild()
          Get the first child.
 AstNode getLastChild()
          Get the last child.
 Name getName()
          Get the name of the node.
 AstNode getParent()
          Get the parent of this node.
 Path getPath(ExecutionContext context)
          Get the current path of this node, using the supplied context.
 Path getPathRelativeTo(Path rootPath, ExecutionContext context)
          Get the current path of this node, as if the current tree is below the supplied root path, using the supplied context.
 List<Property> getProperties()
          Return the list of properties for this node.
 Property getProperty(Name name)
          Get the property with the supplied name.
 int getSameNameSiblingIndex()
          Get the current same-name-sibling index.
 String getString(ExecutionContext context)
          Get the string representation of this query object.
 void insertAsParent(AstNode newParent)
          Insert the supplied node into the plan node tree immediately above this node.
 boolean isSameAs(AstNode other)
          Determine whether the supplied plan is equivalent to this plan.
 Iterator<AstNode> iterator()
           This iterator is immutable.
 List<AstNode> removeAllChildren()
          Remove all children from this node.
 boolean removeChild(AstNode child)
          Remove the node from this node.
 AstNode removeFromParent()
          Remove this node from its parent, and return the node that used to be the parent of this node.
 Property removeProperty(Name name)
          Remove and return the property with the supplied name.
 boolean replaceChild(AstNode child, AstNode replacement)
          Replace the supplied child with another node.
 void setParent(AstNode parent)
          Set the parent for this node.
 AstNode setProperty(Name name, Object... values)
          Set the property with the given name to the supplied values.
 AstNode setProperty(Name name, Object value)
          Set the property with the given name to the supplied value.
 String toString()
          
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AstNode

public AstNode(Name name)
Construct a node with the supplied name but without a parent.

Parameters:
name - the name of the node; may not be null

AstNode

public AstNode(AstNode parent,
               Name name)
Construct a node with the supplied name and parent.

Parameters:
parent - the parent node; may be null if the new node is to be a root
name - the name of the node; may not be null
Method Detail

getName

public Name getName()
Get the name of the node.

Returns:
the node's name; never null

getSameNameSiblingIndex

public int getSameNameSiblingIndex()
Get the current same-name-sibling index.

Returns:
the SNS index, or 1 if this is the first sibling with the same name

getPath

public Path getPath(ExecutionContext context)
Get the current path of this node, using the supplied context.

Parameters:
context - the context; may not be null
Returns:
the path of this node; never null

getPathRelativeTo

public Path getPathRelativeTo(Path rootPath,
                              ExecutionContext context)
Get the current path of this node, as if the current tree is below the supplied root path, using the supplied context.

Parameters:
rootPath - the path to the root of this AST tree; may not be null
context - the context; may not be null
Returns:
the path of this node; never null

getProperty

public Property getProperty(Name name)
Get the property with the supplied name.

Parameters:
name - the property name; never null
Returns:
the property, or null if no such property exists on the node

setProperty

public AstNode setProperty(Name name,
                           Object value)
Set the property with the given name to the supplied value. Any existing property with the same name will be replaced.

Parameters:
name - the name of the property; may not be null
value - the value of the property; may not be null
Returns:
this node, for method chaining purposes

setProperty

public AstNode setProperty(Name name,
                           Object... values)
Set the property with the given name to the supplied values. If there is at least one value, the new property will replace any existing property with the same name. This method does nothing if zero values are supplied.

Parameters:
name - the name of the property; may not be null
values - the values of the property
Returns:
this node, for method chaining purposes
See Also:
removeProperty(Name)

removeProperty

public Property removeProperty(Name name)
Remove and return the property with the supplied name.

Parameters:
name - the property name; may not be null
Returns:
the property that was removed, or null if there was no such property

getProperties

public List<Property> getProperties()
Return the list of properties for this node.

Returns:
the list of properties for this node.

getParent

public AstNode getParent()
Get the parent of this node.

Returns:
the parent node, or null if this node has no parent

setParent

public void setParent(AstNode parent)
Set the parent for this node. If this node already has a parent, this method will remove this node from the current parent. If the supplied parent is not null, then this node will be added to the supplied parent's children.

Parameters:
parent - the new parent, or null if this node is to have no parent

insertAsParent

public void insertAsParent(AstNode newParent)
Insert the supplied node into the plan node tree immediately above this node. If this node has a parent when this method is called, the new parent essentially takes the place of this node within the list of children of the old parent. This method does nothing if the supplied new parent is null.

For example, consider a plan node tree before this method is called:

        A
      / | \
     /  |  \
    B   C   D
 
Then after this method is called with c.insertAsParent(e), the resulting plan node tree will be:
        A
      / | \
     /  |  \
    B   E   D
        |
        |
        C
 

Also note that the node on which this method is called ('C' in the example above) will always be added as the last child to the new parent. This allows the new parent to already have children before this method is called.

Parameters:
newParent - the new parent; method does nothing if this is null

removeFromParent

public AstNode removeFromParent()
Remove this node from its parent, and return the node that used to be the parent of this node. Note that this method removes the entire subgraph under this node.

Returns:
the node that was the parent of this node, or null if this node had no parent
See Also:
extractChild(AstNode), extractFromParent()

replaceChild

public boolean replaceChild(AstNode child,
                            AstNode replacement)
Replace the supplied child with another node. If the replacement is already a child of this node, this method effectively swaps the position of the child and replacement nodes.

Parameters:
child - the node that is already a child and that is to be replaced; may not be null and must be a child
replacement - the node that is to replace the 'child' node; may not be null
Returns:
true if the child was successfully replaced

getChildCount

public int getChildCount()
Get the number of child nodes.

Returns:
the number of children; never negative

getFirstChild

public AstNode getFirstChild()
Get the first child.

Returns:
the first child, or null if there are no children

getLastChild

public AstNode getLastChild()
Get the last child.

Returns:
the last child, or null if there are no children

getChild

public AstNode getChild(int index)
Get the child at the supplied index.

Parameters:
index - the index
Returns:
the child, or null if there are no children
Throws:
IndexOutOfBoundsException - if the index is not valid given the number of children

addFirstChild

public void addFirstChild(AstNode child)
Add the supplied node to the front of the list of children.

Parameters:
child - the node that should be added as the first child; may not be null

addLastChild

public void addLastChild(AstNode child)
Add the supplied node to the end of the list of children.

Parameters:
child - the node that should be added as the last child; may not be null

addChildren

public void addChildren(Iterable<AstNode> otherChildren)
Add the supplied nodes at the end of the list of children.

Parameters:
otherChildren - the children to add; may not be null

addChildren

public void addChildren(AstNode first,
                        AstNode second)
Add the supplied nodes at the end of the list of children.

Parameters:
first - the first child to add
second - the second child to add

addChildren

public void addChildren(AstNode first,
                        AstNode second,
                        AstNode third)
Add the supplied nodes at the end of the list of children.

Parameters:
first - the first child to add
second - the second child to add
third - the third child to add

removeChild

public boolean removeChild(AstNode child)
Remove the node from this node.

Parameters:
child - the child node; may not be null
Returns:
true if the child was removed from this node, or false if the supplied node was not a child of this node

extractChild

public void extractChild(AstNode child)
Remove the child node from this node, and replace that child with its first child (if there is one).

Parameters:
child - the child to be extracted; may not be null and must have at most 1 child
See Also:
extractFromParent()

extractFromParent

public void extractFromParent()
Extract this node from its parent, but replace this node with its child (if there is one).

See Also:
extractChild(AstNode)

getChildren

public List<AstNode> getChildren()
Get the unmodifiable list of child nodes. This list will immediately reflect any changes made to the children (via other methods), but this list cannot be used to add or remove children.

Returns:
the list of children, which immediately reflects changes but which cannot be modified directly; never null

iterator

public Iterator<AstNode> iterator()

This iterator is immutable.

Specified by:
iterator in interface Iterable<AstNode>
See Also:
Iterable.iterator()

removeAllChildren

public List<AstNode> removeAllChildren()
Remove all children from this node. All nodes immediately become orphaned. The resulting list will be mutable.

Returns:
a copy of all the of the children that were removed (and which have no parent); never null

toString

public String toString()

Overrides:
toString in class Object
See Also:
Object.toString()

equals

public final boolean equals(Object obj)

Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)

clone

public AstNode clone()

This class returns a new clone of the plan tree rooted at this node. However, the top node of the resulting plan tree (that is, the node returned from this method) has no parent.

Overrides:
clone in class Object
See Also:
Object.clone()

cloneWithoutNewParent

protected AstNode cloneWithoutNewParent()

isSameAs

public boolean isSameAs(AstNode other)
Determine whether the supplied plan is equivalent to this plan.

Parameters:
other - the other plan to compare with this instance
Returns:
true if the two plans are equivalent, or false otherwise

getString

public String getString(ExecutionContext context)
Get the string representation of this query object.

Parameters:
context - the execution context in which the conversion is to take place
Returns:
the string representation; never null


Copyright © 2008-2010 JBoss, a division of Red Hat. All Rights Reserved.