Class SpanNode

java.lang.Object
com.yahoo.document.annotation.SpanNode
All Implemented Interfaces:
SpanNodeParent, Comparable<SpanNode>
Direct Known Subclasses:
Span, SpanList

public abstract class SpanNode extends Object implements Comparable<SpanNode>, SpanNodeParent
Base class of nodes in a Span tree.
Author:
Einar M R Rosenvinge
  • Constructor Details

    • SpanNode

      protected SpanNode()
  • Method Details

    • isValid

      public boolean isValid()
      Returns whether this node is valid or not. When a child node from a SpanList, the child is marked as invalid, and the reference to it is removed from the parent SpanList. However, Annotations in the global list kept in SpanTree may still have references to the removed SpanNode. Removing these references is costly, and is only done when calling SpanTree.cleanup().
      Returns:
      true if this node is valid, false otherwise.
    • setScratchId

      public void setScratchId(int id)
    • getScratchId

      public int getScratchId()
    • getParent

      public SpanNodeParent getParent()
      Returns the parent node of this SpanNode, if any.
      Returns:
      the parent node, or null if this is not yet added to a parent SpanList
    • getSpanTree

      public SpanTree getSpanTree()
      Returns the SpanTree that this node belongs to, if any.
      Specified by:
      getSpanTree in interface SpanNodeParent
      Returns:
      the SpanTree that this node belongs to, or null if it is not yet added to a SpanTree.
    • annotate

      public SpanNode annotate(Annotation annotation)
      Convenience method for adding an annotation to this span, same as getSpanTree().spanTree.annotate(this,annotation)
      Parameters:
      annotation - the annotation to add
      Returns:
      this for chaining
      Throws:
      NullPointerException - if this span is not attached to a tree
    • annotate

      public SpanNode annotate(AnnotationType type, FieldValue value)
      Convenience method for adding an annotation to this span, same as getSpanTree().spanTree.annotate(this,type,value)
      Parameters:
      type - the type of the annotation to add
      value - the value of the annotation to add
      Returns:
      this for chaining
      Throws:
      NullPointerException - if this span is not attached to a tree
    • annotate

      public SpanNode annotate(AnnotationType type, String value)
      Convenience method for adding an annotation to this span, same as getSpanTree().spanTree.annotate(this,type,new StringFieldValue(value))
      Parameters:
      type - the type of the annotation to add
      value - the string value of the annotation to add
      Returns:
      this for chaining
      Throws:
      NullPointerException - if this span is not attached to a tree
    • annotate

      public SpanNode annotate(AnnotationType type, Integer value)
      Convenience method for adding an annotation to this span, same as getSpanTree().spanTree.annotate(this,type,new IntegerFieldValue(value))
      Parameters:
      type - the type of the annotation to add
      value - the integer value of the annotation to add
      Returns:
      this for chaining
      Throws:
      NullPointerException - if this span is not attached to a tree
    • annotate

      public SpanNode annotate(AnnotationType type)
      Convenience method for adding an annotation with no value to this span, same as getSpanTree().spanTree.annotate(this,type)
      Parameters:
      type - the type of the annotation to add
      Returns:
      this for chaining
      Throws:
      NullPointerException - if this span is not attached to a tree
    • getStringFieldValue

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

      public abstract boolean isLeafNode()
      Returns true if this node is a leaf node in the tree.
      Returns:
      true if this node is a leaf node in the tree.
    • childIterator

      public abstract ListIterator<SpanNode> childIterator()
      Traverses all immediate children of this SpanNode.
      Returns:
      a ListIterator which traverses all immediate children of this SpanNode
    • childIteratorRecursive

      public abstract ListIterator<SpanNode> childIteratorRecursive()
      Recursively traverses all possible children (not only leaf nodes) of this SpanNode, in a depth-first fashion.
      Returns:
      a ListIterator which recursively traverses all children and their children etc. of this SpanNode.
    • getFrom

      public abstract int getFrom()
      Returns the character index where this SpanNode starts (inclusive).
      Returns:
      the character index where this SpanNode starts (inclusive).
    • getTo

      public abstract int getTo()
      Returns the character index where this SpanNode ends (exclusive).
      Returns:
      the character index where this SpanNode ends (exclusive).
    • getLength

      public abstract int getLength()
      Returns the length of this span, i.e. getFrom() - getTo().
      Returns:
      the length of this span
    • getText

      public abstract CharSequence getText(CharSequence text)
      Returns the text that is covered by this SpanNode.
      Parameters:
      text - the input text
      Returns:
      the text that is covered by this SpanNode.
    • overlaps

      public boolean overlaps(SpanNode o)
      Checks if the text covered by this span overlaps with the text covered by another span.
      Parameters:
      o - the other SpanNode to check
      Returns:
      true if spans are overlapping, false otherwise
    • contains

      public boolean contains(SpanNode o)
      Checks if the text covered by another span is within the text covered by this span.
      Parameters:
      o - the other SpanNode to check.
      Returns:
      true if the text covered by another span is within the text covered by this span, false otherwise.
    • equals

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

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

      public int compareTo(SpanNode o)
      Compares two SpanNodes. Note: this class has a natural ordering that might be inconsistent with equals.

      First, getFrom() is compared, and -1 or 1 is return if our getFrom() is smaller or greater that o.getFrom(), respectively. If and only if getFrom() is equal, getTo() is compared, and -1 or 1 is return if our getTo() is smaller or greater that o.getTo(), respectively. In all other cases, the two SpanNodes are equal both for getFrom() and getTo(), and 0 is returned.

      Note that if a subclass has overridden equals(), which is very likely, but has not overridden compareTo(), then that subclass will have a natural ordering that is inconsistent with equals.

      Specified by:
      compareTo in interface Comparable<SpanNode>
      Parameters:
      o - the SpanNode to compare to
      Returns:
      a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object