Class SpanTree

java.lang.Object
com.yahoo.document.annotation.SpanTree
All Implemented Interfaces:
SpanNodeParent, Comparable<SpanTree>, Iterable<Annotation>

public class SpanTree extends Object implements Iterable<Annotation>, SpanNodeParent, Comparable<SpanTree>
A SpanTree holds a root node of a tree of SpanNodes, and a List of Annotations pointing to these nodes or each other. It also has a name.
Author:
Einar M R Rosenvinge
See Also:
  • Constructor Details

    • SpanTree

      public SpanTree()
      WARNING! Only to be used by deserializers! Creates an empty SpanTree instance.
    • SpanTree

      public SpanTree(String name, SpanNode root)
      Creates a new SpanTree with a given root node.
      Parameters:
      name - the name of the span tree
      root - the root node of the span tree
      Throws:
      IllegalStateException - if the root node is invalid
    • SpanTree

      public SpanTree(String name)
      Creates a new SpanTree with the given name and an empty SpanList as its root node.
      Parameters:
      name - the name of the span tree
    • SpanTree

      public SpanTree(SpanTree otherToCopy)
  • Method Details

    • setName

      public void setName(String name)
      WARNING! Only to be used by deserializers! Sets the name of this SpanTree instance.
      Parameters:
      name - the name to set for this SpanTree instance.
    • setRoot

      public void setRoot(SpanNode root)
      WARNING! Only to be used by deserializers! Sets the root of this SpanTree instance.
      Parameters:
      root - the root to set for this SpanTree instance.
    • getName

      public String getName()
      Returns the name of this span tree.
    • getRoot

      public SpanNode getRoot()
      Returns the root node of this span tree.
    • spanList

      public SpanList spanList()
      Convenience shorthand for (SpanList)getRoot(). This must of course only be used when it is known that the root in this tree actually is a SpanList.
    • cleanup

      public void cleanup()
      Ensures consistency of the tree in case SpanNodes have been removed, and there are still Annotations pointing to them. This method has a maximum upper bound of O(3nm), where n is the total number of Annotations, and m is the number of SpanNodes that had been removed from the tree. The lower bound is Omega(n), if no SpanNodes had been removed from the tree.
    • annotate

      public SpanTree annotate(Annotation a)
      Adds an Annotation to the internal list of annotations for this SpanTree. Use this when adding an Annotation that uses an AnnotationReference, and does not annotate a SpanNode.
      Parameters:
      a - the Annotation to add
      Returns:
      this, for chaining
      See Also:
    • annotate

      public SpanTree annotate(SpanNode node, Annotation annotation)
      Adds an Annotation to the internal list of annotations for this SpanTree. Use this when adding an Annotation that shall annotate a SpanNode. Upon return, Annotation.getSpanNode() returns the given node.
      Parameters:
      node - the node to annotate
      annotation - the Annotation to add
      Returns:
      this, for chaining
      See Also:
    • annotateFast

      public final SpanTree annotateFast(SpanNode node, Annotation annotation)
      Adds an Annotation to the internal list of annotations for this SpanTree. Use this when adding an Annotation that shall annotate a SpanNode. Upon return, Annotation.getSpanNode() returns the given node. This one is unchecked and assumes that the SpanNode is valid and has already been attached to the Annotation.
      Parameters:
      node - the node to annotate
      annotation - the Annotation to add
      Returns:
      this, for chaining
      See Also:
    • annotate

      public SpanTree annotate(SpanNode node, AnnotationType type, FieldValue value)
      Adds an Annotation. Convenience shorthand for annotate(node,new Annotation(type,value)
      Parameters:
      node - the node to annotate
      type - the type of the Annotation to add
      value - the value of the Annotation to add
      Returns:
      this, for chaining
      See Also:
    • annotate

      public SpanTree annotate(SpanNode node, AnnotationType type)
      Creates an Annotation based on the given AnnotationType, and adds it to the internal list of annotations for this SpanTree (convenience method). Use this when adding an Annotation (that does not have a FieldValue) that shall annotate a SpanNode. Upon return, Annotation.getSpanNode() returns the given node.
      Parameters:
      node - the node to annotate
      type - the AnnotationType to create an Annotation from
      Returns:
      this, for chaining
      See Also:
    • remove

      public boolean remove(Annotation a)
      Removes an Annotation from the internal list of annotations.
      Parameters:
      a - the annotation to remove
      Returns:
      true if the Annotation was successfully removed, false otherwise
    • numAnnotations

      public int numAnnotations()
      Returns the total number of annotations in the tree.
      Returns:
      the total number of annotations in the tree.
    • clearAnnotations

      public void clearAnnotations(SpanNode node)
      Clears all Annotations for a given SpanNode.
      Parameters:
      node - the SpanNode to clear all Annotations for.
    • clearAnnotationsRecursive

      public void clearAnnotationsRecursive(SpanNode node)
      Clears all Annotations for a given SpanNode and its child nodes.
      Parameters:
      node - the SpanNode to clear all Annotations for.
    • iterator

      public Iterator<Annotation> iterator()
      Returns an Iterator over all annotations in this tree. Note that the iteration order is non-deterministic.
      Specified by:
      iterator in interface Iterable<Annotation>
    • iterator

      public Iterator<Annotation> iterator(SpanNode node)
      Returns an Iterator over all annotations that annotate the given node.
      Parameters:
      node - the node to return annotations for.
      Returns:
      an Iterator over all annotations that annotate the given node.
    • iteratorRecursive

      public Iterator<Annotation> iteratorRecursive(SpanNode node)
      Returns a recursive Iterator over all annotations that annotate the given node and its subnodes.
      Parameters:
      node - the node to recursively return annotations for.
      Returns:
      a recursive Iterator over all annotations that annotate the given node and its subnodes.
    • getSpanTree

      public SpanTree getSpanTree()
      Returns itself. Needed for this class to be able to be a parent of SpanNodes.
      Specified by:
      getSpanTree in interface SpanNodeParent
      Returns:
      this SpanTree instance.
    • setStringFieldValue

      public void setStringFieldValue(StringFieldValue stringFieldValue)
      Sets the StringFieldValue that this SpanTree belongs to. This is called by StringFieldValue.setSpanTree(SpanTree) and there is no need for the user to call this except in unit tests.
      Parameters:
      stringFieldValue - the StringFieldValue that this SpanTree should belong to (might be null to clear the current value)
    • getStringFieldValue

      public StringFieldValue getStringFieldValue()
      Returns the StringFieldValue that this SpanTree belongs to.
      Specified by:
      getStringFieldValue in interface SpanNodeParent
      Returns:
      the StringFieldValue that this SpanTree belongs to, if any, otherwise null.
    • createIndex

      public void createIndex(SpanTree.IndexKey key)
    • clearIndex

      public void clearIndex(SpanTree.IndexKey key)
    • clearIndexes

      public void clearIndexes()
    • getCurrentIndexes

      public Collection<SpanTree.IndexKey> getCurrentIndexes()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(SpanTree spanTree)
      Specified by:
      compareTo in interface Comparable<SpanTree>