GATE
Version 3.1-2270

gate.annotation
Class AnnotationSetImpl

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractSet
          extended by gate.annotation.AnnotationSetImpl
All Implemented Interfaces:
AnnotationSet, SimpleAnnotationSet, Serializable, Cloneable, Iterable, Collection, Set
Direct Known Subclasses:
DatabaseAnnotationSetImpl

public class AnnotationSetImpl
extends AbstractSet
implements AnnotationSet

Implementation of AnnotationSet. Has a number of indices, all bar one of which are null by default and are only constructed when asked for. Has lots of get methods with various selection criteria; these return views into the set, which are nonetheless valid sets in their own right (but will not necesarily be fully indexed). Has a name, which is null by default; clients of Document can request named AnnotationSets if they so desire. Has a reference to the Document it is attached to. Contrary to Collections convention, there is no no-arg constructor, as this would leave the set in an inconsistent state.

There are five indices: annotation by id, annotations by type, annotations by start/end node and nodes by offset. The last three jointly provide positional indexing; construction of these is triggered by indexByStart/EndOffset(), or by calling a get method that selects on offset. The type index is triggered by indexByType(), or calling a get method that selects on type. The id index is always present.

See Also:
Serialized Form

Nested Class Summary
 class AnnotationSetImpl.VerboseHashMap
          This is a HashMap that fires events when elements are removed.
 
Field Summary
protected static AnnotationFactory annFactory
           
protected  Vector annotationSetListeners
           
protected  HashMap annotsById
          Maps annotation ids (Integers) to Annotations
 
Constructor Summary
AnnotationSetImpl(Collection c)
          Construction from Collection (which must be an AnnotationSet)
AnnotationSetImpl(Document doc)
          Construction from Document.
AnnotationSetImpl(Document doc, String name)
          Construction from Document and name.
 
Method Summary
 void add(Integer id, Long start, Long end, String type, FeatureMap features)
          Create and add an annotation from database read data In this case the id is already known being previously fetched from the database
 Integer add(Long start, Long end, String type, FeatureMap features)
          Create and add an annotation and return its id
 Integer add(Node start, Node end, String type, FeatureMap features)
          Create and add an annotation with pre-existing nodes, and return its id
 boolean add(Object o)
          Add an existing annotation.
 boolean addAll(Collection c)
          Adds multiple annotations to this set in one go.
protected  boolean addAllKeepIDs(Collection c)
          Adds multiple annotations to this set in one go.
 void addAnnotationSetListener(AnnotationSetListener l)
           
 void addGateListener(GateListener l)
           
 Object clone()
           
 void edit(Long start, Long end, DocumentContent replacement)
          Propagate changes to the document content.
protected  void fireAnnotationAdded(AnnotationSetEvent e)
           
protected  void fireAnnotationRemoved(AnnotationSetEvent e)
           
protected  void fireGateEvent(GateEvent e)
           
 Node firstNode()
          Get the node with the smallest offset
 AnnotationSet get()
          Get all annotations
 Annotation get(Integer id)
          Find annotations by id
 AnnotationSet get(Long offset)
          Select annotations by offset.
 AnnotationSet get(Long startOffset, Long endOffset)
          Select annotations by offset.
 AnnotationSet get(Set types)
          Select annotations by a set of types.
 AnnotationSet get(String type)
          Select annotations by type
 AnnotationSet get(String type, FeatureMap constraints)
          Select annotations by type and features This will return an annotation set containing just those annotations of a particular type (i.e. with a particular name) and which have features with specific names and values.
 AnnotationSet get(String type, FeatureMap constraints, Long offset)
          Select annotations by type, features and offset
 AnnotationSet get(String neededType, Long startOffset, Long endOffset)
          Select annotations by offset.
 AnnotationSet get(String type, Set featureNames)
          Select annotations by type and feature names
 Set getAllTypes()
          Get a set of java.lang.String objects representing all the annotation types present in this annotation set.
 AnnotationSet getContained(Long startOffset, Long endOffset)
          Select annotations by offset that start at a position between the start and end before the end offset
 Document getDocument()
          Get the document this set is attached to.
 String getName()
          Get the name of this set.
 AnnotationSet getStrict(Long startOffset, Long endOffset)
          Select annotations by offset.
protected  void indexByEndOffset()
          Construct the positional indices for annotation end
protected  void indexByStartOffset()
          Construct the positional indices for annotation start
protected  void indexByType()
          Construct the positional index.
 Iterator iterator()
          Get an iterator for this set
 Node lastNode()
          Get the node with the largest offset
 Node nextNode(Node node)
          Get the first node that is relevant for this annotation set and which has the offset larger than the one of the node provided.
 boolean remove(Object o)
          Remove an element from this set.
 void removeAnnotationSetListener(AnnotationSetListener l)
           
protected  boolean removeFromIdIndex(Annotation a)
          Remove from the ID index.
protected  void removeFromOffsetIndex(Annotation a)
          Remove from the offset indices.
protected  void removeFromTypeIndex(Annotation a)
          Remove from the type index.
 void removeGateListener(GateListener l)
           
static void setAnnotationFactory(AnnotationFactory newFactory)
          Set the annotation factory used to create annotation objects.
 int size()
          The size of this set
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
clear, contains, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
clear, contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Field Detail

annFactory

protected static AnnotationFactory annFactory

annotsById

protected HashMap annotsById
Maps annotation ids (Integers) to Annotations


annotationSetListeners

protected transient Vector annotationSetListeners
Constructor Detail

AnnotationSetImpl

public AnnotationSetImpl(Document doc)
Construction from Document.


AnnotationSetImpl

public AnnotationSetImpl(Document doc,
                         String name)
Construction from Document and name.


AnnotationSetImpl

public AnnotationSetImpl(Collection c)
                  throws ClassCastException
Construction from Collection (which must be an AnnotationSet)

Throws:
ClassCastException
Method Detail

iterator

public Iterator iterator()
Get an iterator for this set

Specified by:
iterator in interface SimpleAnnotationSet
Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface Set
Specified by:
iterator in class AbstractCollection

remove

public boolean remove(Object o)
               throws ClassCastException
Remove an element from this set.

Specified by:
remove in interface SimpleAnnotationSet
Specified by:
remove in interface Collection
Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection
Throws:
ClassCastException

removeFromIdIndex

protected boolean removeFromIdIndex(Annotation a)
Remove from the ID index.


removeFromTypeIndex

protected void removeFromTypeIndex(Annotation a)
Remove from the type index.


removeFromOffsetIndex

protected void removeFromOffsetIndex(Annotation a)
Remove from the offset indices.


size

public int size()
The size of this set

Specified by:
size in interface SimpleAnnotationSet
Specified by:
size in interface Collection
Specified by:
size in interface Set
Specified by:
size in class AbstractCollection

get

public Annotation get(Integer id)
Find annotations by id

Specified by:
get in interface SimpleAnnotationSet

get

public AnnotationSet get()
Get all annotations

Specified by:
get in interface SimpleAnnotationSet

get

public AnnotationSet get(String type)
Select annotations by type

Specified by:
get in interface SimpleAnnotationSet

get

public AnnotationSet get(Set types)
                  throws ClassCastException
Select annotations by a set of types. Expects a Set of String.

Specified by:
get in interface SimpleAnnotationSet
Throws:
ClassCastException

get

public AnnotationSet get(String type,
                         FeatureMap constraints)
Select annotations by type and features This will return an annotation set containing just those annotations of a particular type (i.e. with a particular name) and which have features with specific names and values. (It will also return annotations that have features besides those specified, but it will not return any annotations that do not have all the specified feature-value pairs.) However, if constraints contains a feature whose value is equal to gate.creole.ANNIEConstants.LOOKUP_CLASS_FEATURE_NAME (which is normally "class"), then GATE will attempt to match that feature using an ontology which it will try to retreive from a feature on the both the annotation and in constraints. If these do not return identical ontologies, or if either the annotation or constraints does not contain an ontology, then matching will fail, and the annotation will not be added. In summary, this method will not work normally for features with the name "class".

Specified by:
get in interface AnnotationSet
Parameters:
type - The name of the annotations to return.
constraints - A feature map containing all of the feature value pairs that the annotation must have in order for them to be returned.
Returns:
An annotation set containing only those annotations with the given name and which have the specified set of feature-value pairs.

get

public AnnotationSet get(String type,
                         Set featureNames)
Select annotations by type and feature names

Specified by:
get in interface AnnotationSet

get

public AnnotationSet get(Long offset)
Select annotations by offset. This returns the set of annotations whose start node is the least such that it is less than or equal to offset. If a positional index doesn't exist it is created. If there are no nodes at or beyond the offset param then it will return null.

Specified by:
get in interface AnnotationSet

get

public AnnotationSet get(Long startOffset,
                         Long endOffset)
Select annotations by offset. This returns the set of annotations that overlap totaly or partially with the interval defined by the two provided offsets.The result will include all the annotations that either:

getStrict

public AnnotationSet getStrict(Long startOffset,
                               Long endOffset)
Select annotations by offset. This returns the set of annotations that overlap strictly with the interval defined by the two provided offsets.The result will include all the annotations that start at the start offset and end strictly at the end offset


get

public AnnotationSet get(String neededType,
                         Long startOffset,
                         Long endOffset)
Select annotations by offset. This returns the set of annotations of the given type that overlap totaly or partially with the interval defined by the two provided offsets.The result will include all the annotations that either:

get

public AnnotationSet get(String type,
                         FeatureMap constraints,
                         Long offset)
Select annotations by type, features and offset

Specified by:
get in interface AnnotationSet

getContained

public AnnotationSet getContained(Long startOffset,
                                  Long endOffset)
Select annotations by offset that start at a position between the start and end before the end offset

Specified by:
getContained in interface AnnotationSet

firstNode

public Node firstNode()
Get the node with the smallest offset

Specified by:
firstNode in interface AnnotationSet

lastNode

public Node lastNode()
Get the node with the largest offset

Specified by:
lastNode in interface AnnotationSet

nextNode

public Node nextNode(Node node)
Get the first node that is relevant for this annotation set and which has the offset larger than the one of the node provided.

Specified by:
nextNode in interface AnnotationSet

setAnnotationFactory

public static void setAnnotationFactory(AnnotationFactory newFactory)
Set the annotation factory used to create annotation objects. The default factory is DefaultAnnotationFactory.


add

public Integer add(Node start,
                   Node end,
                   String type,
                   FeatureMap features)
Create and add an annotation with pre-existing nodes, and return its id

Specified by:
add in interface SimpleAnnotationSet

add

public boolean add(Object o)
            throws ClassCastException
Add an existing annotation. Returns true when the set is modified.

Specified by:
add in interface SimpleAnnotationSet
Specified by:
add in interface Collection
Specified by:
add in interface Set
Overrides:
add in class AbstractCollection
Throws:
ClassCastException

addAll

public boolean addAll(Collection c)
Adds multiple annotations to this set in one go. All the objects in the provided collection should be of Annotation type, otherwise a ClassCastException will be thrown. The provided annotations will be used to create new annotations using the appropriate add() methods from this set. The new annotations will have different IDs from the old ones (which is required in order to preserve the uniqueness of IDs inside an annotation set).

Specified by:
addAll in interface Collection
Specified by:
addAll in interface Set
Overrides:
addAll in class AbstractCollection
Parameters:
c - a collection of annotations
Returns:
true if the set has been modified as a result of this call.

addAllKeepIDs

protected boolean addAllKeepIDs(Collection c)
Adds multiple annotations to this set in one go. All the objects in the provided collection should be of Annotation type, otherwise a ClassCastException will be thrown. This method does not create copies of the annotations like addAll() does but simply adds the new annotations to the set. It is intended to be used solely by annotation sets in order to construct the results for various get(...) methods.

Parameters:
c - a collection of annotations
Returns:
true if the set has been modified as a result of this call.

add

public Integer add(Long start,
                   Long end,
                   String type,
                   FeatureMap features)
            throws InvalidOffsetException
Create and add an annotation and return its id

Specified by:
add in interface SimpleAnnotationSet
Throws:
InvalidOffsetException

add

public void add(Integer id,
                Long start,
                Long end,
                String type,
                FeatureMap features)
         throws InvalidOffsetException
Create and add an annotation from database read data In this case the id is already known being previously fetched from the database

Specified by:
add in interface AnnotationSet
Throws:
InvalidOffsetException

indexByType

protected void indexByType()
Construct the positional index.


indexByStartOffset

protected void indexByStartOffset()
Construct the positional indices for annotation start


indexByEndOffset

protected void indexByEndOffset()
Construct the positional indices for annotation end


edit

public void edit(Long start,
                 Long end,
                 DocumentContent replacement)
Propagate changes to the document content. Has, unfortunately, to be public, to allow DocumentImpls to get at it. Oh for a "friend" declaration. Doesn't throw InvalidOffsetException as DocumentImpl is the only client, and that checks the offsets before calling this method.


getName

public String getName()
Get the name of this set.

Specified by:
getName in interface SimpleAnnotationSet

getDocument

public Document getDocument()
Get the document this set is attached to.

Specified by:
getDocument in interface SimpleAnnotationSet

getAllTypes

public Set getAllTypes()
Get a set of java.lang.String objects representing all the annotation types present in this annotation set.

Specified by:
getAllTypes in interface SimpleAnnotationSet

clone

public Object clone()
             throws CloneNotSupportedException
Overrides:
clone in class Object
Returns:
a clone of this set.
Throws:
CloneNotSupportedException

removeAnnotationSetListener

public void removeAnnotationSetListener(AnnotationSetListener l)
Specified by:
removeAnnotationSetListener in interface AnnotationSet
Parameters:
l -

addAnnotationSetListener

public void addAnnotationSetListener(AnnotationSetListener l)
Specified by:
addAnnotationSetListener in interface AnnotationSet
Parameters:
l -

fireAnnotationAdded

protected void fireAnnotationAdded(AnnotationSetEvent e)
Parameters:
e -

fireAnnotationRemoved

protected void fireAnnotationRemoved(AnnotationSetEvent e)
Parameters:
e -

removeGateListener

public void removeGateListener(GateListener l)
Specified by:
removeGateListener in interface AnnotationSet
Parameters:
l -

addGateListener

public void addGateListener(GateListener l)
Specified by:
addGateListener in interface AnnotationSet
Parameters:
l -

fireGateEvent

protected void fireGateEvent(GateEvent e)
Parameters:
e -

GATE
Version 3.1-2270