Package com.yahoo.document.annotation
Class SpanTree
- java.lang.Object
-
- com.yahoo.document.annotation.SpanTree
-
- All Implemented Interfaces:
SpanNodeParent
,java.lang.Comparable<SpanTree>
,java.lang.Iterable<Annotation>
public class SpanTree extends java.lang.Object implements java.lang.Iterable<Annotation>, SpanNodeParent, java.lang.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:
SpanNode
,Annotation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SpanTree.IndexKey
-
Constructor Summary
Constructors Constructor Description SpanTree()
WARNING! Only to be used by deserializers! Creates an empty SpanTree instance.SpanTree(SpanTree otherToCopy)
SpanTree(java.lang.String name)
Creates a new SpanTree with the given name and an empty SpanList as its root node.SpanTree(java.lang.String name, SpanNode root)
Creates a new SpanTree with a given root node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.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.SpanTree
annotate(SpanNode node, AnnotationType type, FieldValue value)
Adds an Annotation.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.void
cleanup()
Ensures consistency of the tree in case SpanNodes have been removed, and there are still Annotations pointing to them.void
clearAnnotations(SpanNode node)
Clears all Annotations for a given SpanNode.void
clearAnnotationsRecursive(SpanNode node)
Clears all Annotations for a given SpanNode and its child nodes.void
clearIndex(SpanTree.IndexKey key)
void
clearIndexes()
int
compareTo(SpanTree spanTree)
void
createIndex(SpanTree.IndexKey key)
boolean
equals(java.lang.Object o)
java.util.Collection<SpanTree.IndexKey>
getCurrentIndexes()
java.lang.String
getName()
Returns the name of this span tree.SpanNode
getRoot()
Returns the root node of this span tree.SpanTree
getSpanTree()
Returns itself. Needed for this class to be able to be a parent of SpanNodes.StringFieldValue
getStringFieldValue()
Returns the StringFieldValue that this SpanTree belongs to.int
hashCode()
java.util.Iterator<Annotation>
iterator()
Returns an Iterator over all annotations in this tree. Note that the iteration order is non-deterministic.java.util.Iterator<Annotation>
iterator(SpanNode node)
Returns an Iterator over all annotations that annotate the given node.java.util.Iterator<Annotation>
iteratorRecursive(SpanNode node)
Returns a recursive Iterator over all annotations that annotate the given node and its subnodes.int
numAnnotations()
Returns the total number of annotations in the tree.boolean
remove(Annotation a)
Removes an Annotation from the internal list of annotations.void
setName(java.lang.String name)
WARNING! Only to be used by deserializers! Sets the name of this SpanTree instance.void
setRoot(SpanNode root)
WARNING! Only to be used by deserializers! Sets the root of this SpanTree instance.void
setStringFieldValue(StringFieldValue stringFieldValue)
Sets the StringFieldValue that this SpanTree belongs to. This is called byStringFieldValue.setSpanTree(SpanTree)
and there is no need for the user to call this except in unit tests.SpanList
spanList()
Convenience shorthand for(SpanList)getRoot()
.java.lang.String
toString()
-
-
-
Constructor Detail
-
SpanTree
public SpanTree()
WARNING! Only to be used by deserializers! Creates an empty SpanTree instance.
-
SpanTree
public SpanTree(java.lang.String name, SpanNode root)
Creates a new SpanTree with a given root node.- Parameters:
name
- the name of the span treeroot
- the root node of the span tree- Throws:
java.lang.IllegalStateException
- if the root node is invalid
-
SpanTree
public SpanTree(java.lang.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 Detail
-
setName
public void setName(java.lang.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 java.lang.String getName()
Returns the name of this span tree.- Returns:
- the name of this span tree.
-
getRoot
public SpanNode getRoot()
Returns the root node of this span tree.- 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:
Annotation
,AnnotationReference
,AnnotationReferenceDataType
-
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 annotateannotation
- the Annotation to add- Returns:
- this, for chaining
- See Also:
Annotation
-
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 annotateannotation
- the Annotation to add- Returns:
- this, for chaining
- See Also:
Annotation
-
annotate
public SpanTree annotate(SpanNode node, AnnotationType type, FieldValue value)
Adds an Annotation. Convenience shorthand forannotate(node,new Annotation(type,value)
- Parameters:
node
- the node to annotatetype
- the type of the Annotation to addvalue
- the value of the Annotation to add- Returns:
- this, for chaining
- See Also:
Annotation
-
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 annotatetype
- the AnnotationType to create an Annotation from- Returns:
- this, for chaining
- See Also:
Annotation
,AnnotationType
-
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 java.util.Iterator<Annotation> iterator()
Returns an Iterator over all annotations in this tree. Note that the iteration order is non-deterministic.- Specified by:
iterator
in interfacejava.lang.Iterable<Annotation>
- Returns:
- an Iterator over all annotations in this tree.
-
iterator
public java.util.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 java.util.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 interfaceSpanNodeParent
- Returns:
- this SpanTree instance.
-
setStringFieldValue
public void setStringFieldValue(StringFieldValue stringFieldValue)
Sets the StringFieldValue that this SpanTree belongs to. This is called byStringFieldValue.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 interfaceSpanNodeParent
- 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 java.util.Collection<SpanTree.IndexKey> getCurrentIndexes()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-