fr.umlv.tatoo.runtime.ast
Class CompositeNode<N extends Node,P extends Node>

java.lang.Object
  extended by fr.umlv.tatoo.runtime.ast.CompositeNode<N,P>
Type Parameters:
N - type of child nodes.
P - type of parent node.
All Implemented Interfaces:
Node, Serializable

public abstract class CompositeNode<N extends Node,P extends Node>
extends Object

Represents a node with a variable number of nodes.

Author:
Remi
See Also:
FlatNode, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface fr.umlv.tatoo.runtime.ast.Node
Node.NoParent, Node.NoValue
 
Constructor Summary
protected CompositeNode()
          Only subclass can call this constructor
 
Method Summary
protected  V attributeAt(int index)
          Returns the value of the attribute at the index position.
protected  V attributeAt(int index, V value)
          Changes the value of the attribute at the index position and returns the old value.
protected  int attributeCount()
          Returns the number of attributes.
 List<V> attributeList()
          A read-write list of all attribute values of the current node.
 Map<String,V> attributeMap()
          A read-write map of couples attribute name/attribute value.
protected  String attributeNameAt(int index)
          Returns the name of the attribute at the index position.
protected  Class<? extends V> attributeTypeAt(int index)
          Returns the real type of the attribute at the index position.
 boolean equals(Object o)
           
protected abstract  Class<N> getElementType()
          Returns the real type of the child node.
 P getParent()
          Returns the parent of the current node.
 int hashCode()
           
protected  int indexOfAttributeName(String name)
          Returns the index of an attribute by its name.
 List<N> nodeList()
          Returns a read-write list of all sub nodes of the current node.
protected
<T extends N>
T
reparent(T oldNode, T newNode)
          This method changes the parent of the current node.
 void replaceAllNodes(Collection<? extends N> nodes)
          Replaces all sub-nodes of the current node by new ones taken as parameter.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fr.umlv.tatoo.runtime.ast.Node
accept, getName
 

Constructor Detail

CompositeNode

protected CompositeNode()
Only subclass can call this constructor

Method Detail

nodeList

public List<N> nodeList()
Returns a read-write list of all sub nodes of the current node. This list provides access in constant time to each of its element. This list checks at runtime if the type of an inserted node is a subtype of the declared children type. The method List.add(int, Object) or List.set(int, Object) raises an IllegalStateException if the node taken as argument has already a parent. Futhermore, this list checks at runtime if the type of an inserted node is a subtype of the declared children type.

Specified by:
nodeList in interface Node
Returns:
a list of all sub nodes.
See Also:
Collections.checkedList(List, Class)

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

getParent

public P getParent()
Returns the parent of the current node.

Specified by:
getParent in interface Node
Returns:
the parent of the current node or null if the node has no parent.

reparent

protected <T extends N> T reparent(T oldNode,
                                   T newNode)
This method changes the parent of the current node. This method must not be called directly, but used by nodeList().set(), nodeList().add(), nodeList().remove() instead.

Type Parameters:
T - a subtype of N
Parameters:
oldNode - the old child node.
newNode - the new child node.
Returns:
the new child node.

replaceAllNodes

public void replaceAllNodes(Collection<? extends N> nodes)
Replaces all sub-nodes of the current node by new ones taken as parameter. This method is roughly equivalent this code :
   nodeList().clear();
       nodeList().addAll(nodes);
  
Each node in nodes must have no parent. Each newly added node will have the current node has parent. Each removed node will have no parent.

Parameters:
nodes - a collection of nodes that will be used as new subnodes,
Throws:
IllegalArgumentException - if the nodeList() is a fixed size list and if its number of nodes is not the same that the number of nodes of the collection taken as parameter.
IllegalStateException - if one node of nodes has a parent.
See Also:
nodeList()

getElementType

protected abstract Class<N> getElementType()
Returns the real type of the child node. Service method implemented by concrete node, this method if used by XML deserialization.

Returns:
the real type of the children node.

attributeList

public List<V> attributeList()
A read-write list of all attribute values of the current node. This list is fixed size and provides access in constant time to each of its element.

Specified by:
attributeList in interface Node
Returns:
a list of attribute values.
See Also:
Node.attributeMap()

attributeCount

protected int attributeCount()
Returns the number of attributes. Service method implemented by concrete node, this method must not be called directly, to obtain the number of attributes use attributeList().size() or attributeMap().size() instead.

Returns:
the number of attributes.

attributeAt

protected V attributeAt(int index)
                 throws IndexOutOfBoundsException
Returns the value of the attribute at the index position. Service method implemented by concrete node, this method must not be called directly, to obtain an attribute value by its index use attributeList().get(index) instead.

Parameters:
index - the index of the attribute.
Returns:
the value of the attribute at index index.
Throws:
IndexOutOfBoundsException - if the index is not between 0 and the attribute list size.

attributeAt

protected V attributeAt(int index,
                        V value)
                 throws IndexOutOfBoundsException
Changes the value of the attribute at the index position and returns the old value. Service method implemented by concrete node, this method must not be called directly, to change an attribute value by its index use attributeList().set(index) instead.

Parameters:
index - the index of the attribute.
value - the new value of the attribute.
Returns:
the old value of the attribute at index index.
Throws:
IndexOutOfBoundsException - if the index is not between 0 ant the attribute list size.

attributeNameAt

protected String attributeNameAt(int index)
                          throws IndexOutOfBoundsException
Returns the name of the attribute at the index position. Service method implemented by concrete node, this method must not be called directly.

Parameters:
index - index the index of the attribute.
Returns:
the name of the attribute at index index.
Throws:
IndexOutOfBoundsException - if the index is not between 0 ant the attribute list size.

attributeTypeAt

protected Class<? extends V> attributeTypeAt(int index)
                                      throws IndexOutOfBoundsException
Returns the real type of the attribute at the index position. Service method implemented by concrete node and used by deserialization mecanism, this method must not be called directly.

Parameters:
index - index the index of the attribute.
Returns:
the real type of the attribute at index index.
Throws:
IndexOutOfBoundsException - if the index is not between 0 ant the attribute list size.

indexOfAttributeName

protected int indexOfAttributeName(String name)
Returns the index of an attribute by its name. Service method implemented by concrete node and used by deserialization mecanism, this method must not be called directly.

Parameters:
name - name of the attribute.
Returns:
the real type of the attribute at index index.
Throws:
IllegalArgumentException - if the name of the attribute is not known.

attributeMap

public Map<String,V> attributeMap()
A read-write map of couples attribute name/attribute value. This map is fixed size and access time could be a function of the number of attribute. Futhermore, this map checks the runtime time of the attribute value before insertion.

Specified by:
attributeMap in interface Node
Returns:
a map of attribute name/value.