Revision: 18597
          http://sourceforge.net/p/gate/code/18597
Author:   johann_p
Date:     2015-03-19 19:42:36 +0000 (Thu, 19 Mar 2015)
Log Message:
-----------
Make sure that the ImmutableAnnotationSet returned by the 
AnnotationSetImpl methods always has a document, even when 
empty.

Modified Paths:
--------------
    gate/trunk/src/main/gate/annotation/AnnotationSetImpl.java

Modified: gate/trunk/src/main/gate/annotation/AnnotationSetImpl.java
===================================================================
--- gate/trunk/src/main/gate/annotation/AnnotationSetImpl.java  2015-03-19 
19:09:57 UTC (rev 18596)
+++ gate/trunk/src/main/gate/annotation/AnnotationSetImpl.java  2015-03-19 
19:42:36 UTC (rev 18597)
@@ -83,6 +83,12 @@
  * 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.
+ * <P>
+ * NOTE: equality and hashCode of this implementation is exclusively based on 
the annotations
+ * which appear in the set (if any). The document the set comes from, the name 
of the set or
+ * the relations stored in that set are not taken into account for equality or 
hashSet!!
+ *
+ * 
  */
 public class AnnotationSetImpl extends AbstractSet<Annotation> implements
                                                               AnnotationSet {
@@ -130,11 +136,11 @@
   protected RelationSet relations = null;
 
   // Empty AnnotationSet to be returned instead of null
-   public final static AnnotationSet emptyAnnotationSet;
+   //public final static AnnotationSet emptyAS;
 
-   static {
-   emptyAnnotationSet = new ImmutableAnnotationSetImpl(null,null);
-   }
+   //static {
+   //emptyAnnotationSet = new ImmutableAnnotationSetImpl(null,null);
+   //}
 
   /** Construction from Document. */
   public AnnotationSetImpl(Document doc) {
@@ -318,7 +324,7 @@
    */
   @Override
   public AnnotationSet get() {
-    if (annotsById.isEmpty()) return emptyAnnotationSet;
+    if (annotsById.isEmpty()) return emptyAS();
     return new ImmutableAnnotationSetImpl(doc, annotsById.values());
   } // get()
 
@@ -331,7 +337,7 @@
   public AnnotationSet get(String type) {
     if(annotsByType == null) indexByType();
     AnnotationSet byType = annotsByType.get(type);
-    if (byType==null)return emptyAnnotationSet;
+    if (byType==null)return emptyAS();
     // convert the mutable AS into an immutable one
     return byType.get();
   } // get(type)
@@ -356,7 +362,7 @@
         }
       }
     } // while
-    if(annotations.isEmpty()) return emptyAnnotationSet;
+    if(annotations.isEmpty()) return emptyAS();
     return new ImmutableAnnotationSetImpl(doc, annotations);
   } // get(types)
 
@@ -402,7 +408,7 @@
       // (a.getFeatures().entrySet().containsAll(constraints.entrySet()))
       if(a.getFeatures().subsumes(constraints)) annotationsToAdd.add(a);
     } // while
-    if(annotationsToAdd.isEmpty()) return emptyAnnotationSet;
+    if(annotationsToAdd.isEmpty()) return emptyAS();
     return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
   } // get(type, constraints)
 
@@ -430,7 +436,7 @@
       if(a.getFeatures().keySet().containsAll(featureNames))
         annotationsToAdd.add(a);
     } // while
-    if(annotationsToAdd.isEmpty()) return emptyAnnotationSet;
+    if(annotationsToAdd.isEmpty()) return emptyAS();
     return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
   } // get(type, featureNames)
 
@@ -447,14 +453,14 @@
     // there
     Node nextNode = nodesByOffset.getNextOf(offset);
     if(nextNode == null) // no nodes at or beyond this offset
-      return emptyAnnotationSet;
+      return emptyAS();
     Collection<Annotation> annotationsToAdd = getAnnotsByStartNode(nextNode
             .getId());
     // skip all the nodes that have no starting annotations
     while(annotationsToAdd == null) {
       nextNode = nodesByOffset.getNextOf(new Long(nextNode.getOffset()
               .longValue() + 1));
-      if (nextNode==null) return emptyAnnotationSet;
+      if (nextNode==null) return emptyAS();
       annotationsToAdd = getAnnotsByStartNode(nextNode.getId());
     }
     return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
@@ -475,7 +481,7 @@
     if(annotsByStartNode == null) indexByStartOffset();
     Node node = nodesByOffset.get(offset);
     if(node == null) { // no nodes at or beyond this offset
-      return emptyAnnotationSet;
+      return emptyAS();
     }
     return new ImmutableAnnotationSetImpl(doc, 
getAnnotsByStartNode(node.getId()));
   }
@@ -634,13 +640,13 @@
   @Override
   public AnnotationSet getCovering(String neededType, Long startOffset, Long 
endOffset) {
     //check the range
-    if(endOffset < startOffset) return emptyAnnotationSet;
+    if(endOffset < startOffset) return emptyAS();
     //ensure index
     if(annotsByStartNode == null) indexByStartOffset();
     //if the requested range is longer than the longest annotation in this 
set, 
     //then there can be no annotations covering the range
     // so we return an empty set.
-    if(endOffset - startOffset > longestAnnot) return emptyAnnotationSet;
+    if(endOffset - startOffset > longestAnnot) return emptyAS();
     
     List<Annotation> annotationsToAdd = new ArrayList<Annotation>();
     Iterator<Node> nodesIter;
@@ -681,7 +687,7 @@
   public AnnotationSet get(String type, FeatureMap constraints, Long offset) {
     // select by offset
     AnnotationSet nextAnnots = get(offset);
-    if(nextAnnots == null) return emptyAnnotationSet;
+    if(nextAnnots == null) return emptyAS();
     // select by type and constraints from the next annots
     return nextAnnots.get(type, constraints);
   } // get(type, constraints, offset)
@@ -698,7 +704,7 @@
     // start at a position between the start and end before the end
     // offsets
     //check the range
-    if(endOffset < startOffset) return emptyAnnotationSet;
+    if(endOffset < startOffset) return emptyAS();
     //ensure index
     if(annotsByStartNode == null) indexByStartOffset();
     List<Annotation> annotationsToAdd = null;
@@ -1378,4 +1384,14 @@
     }
     return relations;
   }
+  
+  // utility method that replaces the former static singleton member 
ImmutableAnnotationSet(null,null).
+  // We should not give back annotation sets which have a null document, so 
instead we return
+  // as an empty annotation set one that does not have annotations, but points 
to the same document
+  // as the one it was created from. 
+  protected AnnotationSet emptyAS() {
+    return new ImmutableAnnotationSetImpl(doc, null);
+  }
+  
+  
 } // AnnotationSetImpl

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to