dfh.treepath
Class Index<N>

java.lang.Object
  extended by dfh.treepath.Index<N>
Type Parameters:
N - a type of tree node
Direct Known Subclasses:
ParentIndex

public class Index<N>
extends Object

A class that caches information pertaining to a particular tree, if necessary. For some sorts of tree, this class will do little more than provide a reference back to the Forester and root node. For others the index supplements the tree with information not accessible from particular nodes.

Author:
David F. Houghton - Apr 28, 2012

Field Summary
 Forester<N> f
          Forester capable of interpreting the tree, generally the one that created the index.
protected  Map<String,N> identifiedNodes
          Map from ids to nodes, if this is appropriate for the tree type and any identified nodes exist.
protected  boolean indexed
          Whether the index has been initialized.
 N root
          root node in tree
 
Constructor Summary
Index(N root, Forester<N> f)
          Constructs an index for the tree with the specified root.
 
Method Summary
 String id(N n)
          Returns the unique identifier, if any, that identifies this node.
protected  void index()
          Walks tree performing indexing.
protected  void index(N n)
          Record any unique identifier of this node.
protected  void index(N n, N c)
          Do relevant indexing for a parent and child pair.
protected  boolean indexed()
          Returns whether index() has been called.
 boolean isRoot(N n)
          Indicates whether the given node is the root.
protected  void walk(N n)
          Walk the tree, indexing all nodes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

public final N root
root node in tree


f

public final Forester<N> f
Forester capable of interpreting the tree, generally the one that created the index.


identifiedNodes

protected Map<String,N> identifiedNodes
Map from ids to nodes, if this is appropriate for the tree type and any identified nodes exist.


indexed

protected boolean indexed
Whether the index has been initialized.

Constructor Detail

Index

public Index(N root,
             Forester<N> f)
Constructs an index for the tree with the specified root.

Parameters:
root - the root of a tree
f - a Forester that can query nodes in the tree
Method Detail

index

protected void index()
Walks tree performing indexing.


indexed

protected boolean indexed()
Returns whether index() has been called. This is used to prevent redundant tree walking.

Returns:
whether index() has been called

walk

protected void walk(N n)
Walk the tree, indexing all nodes.

Parameters:
n - current node

index

protected void index(N n)
Record any unique identifier of this node.

Parameters:
n - node

index

protected void index(N n,
                     N c)
Do relevant indexing for a parent and child pair. Unless overridden this method does nothing.

Parameters:
n - parent node
c - child node

isRoot

public boolean isRoot(N n)
Indicates whether the given node is the root.

Parameters:
n -
Returns:
whether the given node is the root of the tree

id

public String id(N n)
Returns the unique identifier, if any, that identifies this node.

Parameters:
n - node
Returns:
a unique string identifier of this node, if any