public interface Node
Node
represents a node in a graph.
A tree is a graph, so it may represent the root of a tree, a node corresponding to a proper subtree of the same tree, or simply a leaf node therein.
In this API, you can traverse a Node
in at least two different ways. The first using an implementation of the interface NodeVisitor
, which uses a design pattern called Visitor. The second using an
implementation of the interface NodeHierarchicalVisitor
, which uses a design pattern called Hierarchical Visitor.
Modifier and Type | Method and Description |
---|---|
default boolean |
accept(NodeHierarchicalVisitor nodeHierarchicalVisitor)
Accepts a
NodeHierarchicalVisitor . |
default void |
accept(NodeVisitor nodeVisitor)
Accepts a
NodeVisitor . |
default boolean accept(NodeHierarchicalVisitor nodeHierarchicalVisitor)
NodeHierarchicalVisitor
.
Returns the result of nodeHierarchicalVisitor.visitLeave(this)
.
If nodeHierarchicalVisitor
is null
, a NullPointerException
may be thrown. But no guarantees can be made for all implementations.
If a RuntimeException
is thrown by the current NodeHierarchicalVisitor
, a NodeTraversalException
may be thrown with the RuntimeException
wrapped. But no guarantees can be made for all
implementations.
To implement this method, consider the following example:
if(nodeHierarchicalVisitor.visitEnter(this)) {
for(Node childNode : childNodes) {
if(!childNode.accept(nodeHierarchicalVisitor)) {
break;
}
}
}
return nodeHierarchicalVisitor.visitLeave(this);
The default implementation will:
NullPointerException
if nodeHierarchicalVisitor
is null
.NodeTraversalException
if nodeHierarchicalVisitor
throws a RuntimeException
.Node
s, because it does not have any.nodeHierarchicalVisitor
- the NodeHierarchicalVisitor
to acceptnodeHierarchicalVisitor.visitLeave(this)
NodeTraversalException
- thrown if, and only if, a RuntimeException
is thrown by the current NodeHierarchicalVisitor
java.lang.NullPointerException
- thrown if, and only if, nodeHierarchicalVisitor
is null
default void accept(NodeVisitor nodeVisitor)
NodeVisitor
.
If nodeVisitor
is null
, a NullPointerException
may be thrown. But no guarantees can be made for all implementations.
If a RuntimeException
is thrown by the current NodeVisitor
, a NodeTraversalException
may be thrown with the RuntimeException
wrapped. But no guarantees can be made for all
implementations.
To implement this method, consider the following example:
nodeVisitor.accept(this);
The default implementation will:
NullPointerException
if nodeVisitor
is null
.NodeTraversalException
if nodeVisitor
throws a RuntimeException
.nodeVisitor
- the NodeVisitor
to acceptNodeTraversalException
- thrown if, and only if, a RuntimeException
is thrown by the current NodeVisitor
java.lang.NullPointerException
- thrown if, and only if, nodeVisitor
is null