Class LayerTraversal<R>


  • public class LayerTraversal<R>
    extends Object
    Traverses Annotations in a Graph using the Layer hierarchy (i.e. Annotations are nodes, parent/child linkes are edges).

    This base class handles the traversal. The pre-order and post-order operations (the actual work being done) can be implemented by subclassing to implement pre(Annotation) and post(Annotation).

    For example, to print a list of annotations in hierarchy order:

     LayerTraversal<StringBuffer> t = new LayerTraversal<StringBuffer>(new StringBuffer(), graph)
     {
       protected void pre(Annotation annotation)
       {
         result.append(annotation.getLabel() + "\n");
       }
     };
     System.out.println(t.getResult().toString());
     

    The main traversal uses only the layer hierarchy defined in the graph. This means that if the graph contains annotations for which no layer is defined, those annotations will not be visited by this traversal. After traversing the layer hierarchy, the annotations that have not been visited are processed by except(Annotation) - provide an implementation for this method if these annotations must also be processed.

    Author:
    Robert Fromont robert@fromont.net.nz
    • Constructor Detail

      • LayerTraversal

        public LayerTraversal()
        Default constructor.
      • LayerTraversal

        public LayerTraversal​(R result)
        Constructor with starting result.
        Parameters:
        result - The result of the traversal.
      • LayerTraversal

        public LayerTraversal​(R result,
                              Graph graph)
        Constructor with starting result and graph to immediately traverse. Traversal will be depth-first.
        Parameters:
        result - The result of the traversal.
        graph - The graph to be traversed.
      • LayerTraversal

        public LayerTraversal​(R result,
                              Annotation annotation)
        Constructor with starting result and annotation to immediately traverse. Traversal will be depth-first.
        Parameters:
        result - The result of the traversal.
        annotation - The annotation to be traversed.
      • LayerTraversal

        public LayerTraversal​(R result,
                              Graph graph,
                              boolean breadthFirst)
        Constructor with starting result and graph to immediately traverse.
        Parameters:
        result - The result of the traversal.
        graph - The graph to be traversed.
        breadthFirst - Whether the traversal is breadth-first (true) or depth-first (false).
    • Method Detail

      • getGraph

        public Graph getGraph()
        Getter for graph: The graph to be traversed.
        Returns:
        The graph to be traversed.
      • setGraph

        public LayerTraversal<R> setGraph​(Graph newGraph)
        Setter for graph: The graph to be traversed.
        Parameters:
        newGraph - The graph to be traversed.
      • getResult

        public R getResult()
        Getter for result: The result of the traversal, if required.
        Returns:
        The result of the traversal, if required.
      • setResult

        public LayerTraversal<R> setResult​(R newResult)
        Setter for result: The result of the traversal, if required.
        Parameters:
        newResult - The result of the traversal, if required.
      • getBreadthFirst

        public boolean getBreadthFirst()
        Getter for breadthFirst: Whether the traversal is breadth-first (true) or depth-first (false - the default).
        Returns:
        Whether the traversal is breadth-first (true) or depth-first (false - the default).
      • setBreadthFirst

        public LayerTraversal<R> setBreadthFirst​(boolean newBreadthFirst)
        Setter for breadthFirst: Whether the traversal is breadth-first (true) or depth-first (false - the default).
        Parameters:
        newBreadthFirst - Whether the traversal is breadth-first (true) or depth-first (false - the default).
      • traverseGraph

        public R traverseGraph​(Graph graph)
        Traverses the given graph.
        Parameters:
        graph - The graph to traverse.
        Returns:
        Some result of the traversal, if required.
      • traverseAnnotation

        public R traverseAnnotation​(Annotation annotation)
        Depth-first recursive method that traverses the layer annotations using the layer hierarchy, calling pre(Annotation), then calling itself for all children, then called post(Annotation), before returning.
        Parameters:
        annotation - The annotation to traverse through.
        Returns:
        The results - i.e. getResult()
        See Also:
        breadthFirst