[UIMA-5427] indexCovered should not return reference annotation
- Fix indexCovered to no longer return reference annotation
- Improved JavaDoc
- Added unit test


git-svn-id: https://svn.apache.org/repos/asf/uima/uimafit/trunk@1795203 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java b/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java
index 531f2e6..bd2e3f9 100644
--- a/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java
+++ b/uimafit-core/src/main/java/org/apache/uima/fit/util/CasUtil.java
@@ -743,6 +743,9 @@
    * offsets are equal to the begin/end of the given annotation or where given 'covered' annotation
    * is properly contained within the span of the 'covering' annotation. Partially overlapping
    * annotations are not returned.
+   * <p>
+   * When querying for the annotations covering a given annotation, the given annotation itself
+   * is never returned, even if it is of the queried type.   
    * 
    * @param cas
    *          a CAS.
@@ -791,7 +794,9 @@
    * The method only returns properly covered annotations, that is annotations where the begin/end
    * offsets are equal to the 'covering' annotation or where both the begin/end are included in
    * the span of the 'covering' annotation. Partially overlapping annotations are not returned.
-   * 
+   * <p>
+   * When querying for the annotations covered by a given annotation, the given annotation itself
+   * is never returned, even if it is of the queried type.   * 
    * @param cas
    *          a CAS.
    * @param type
@@ -828,7 +833,7 @@
     
     // Keeps currently "open" annotations in a sorted order
     Deque<AnnotationFS> memory = new ArrayDeque<>();
-    Deque<AnnotationFS> memory2 = new ArrayDeque<>();
+    //Deque<AnnotationFS> memory2 = new ArrayDeque<>();
     
     // Array cursors
     int o = 0;
@@ -872,7 +877,9 @@
               c = new LinkedList<AnnotationFS>();
               index.put(covering, c);
             }
-            c.add(iFS);
+            if (iFS != covering) {
+              c.add(iFS);
+            }
           }
         }
         
diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java b/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java
index 1fb6de2..97aae8d 100644
--- a/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java
+++ b/uimafit-core/src/main/java/org/apache/uima/fit/util/JCasUtil.java
@@ -483,6 +483,9 @@
    * offsets are equal to the begin/end of the given annotation or where given 'covered' annotation
    * is properly contained within the span of the 'covering' annotation. Partially overlapping
    * annotations are not returned.
+   * <p>
+   * When querying for the annotations covering a given annotation, the given annotation itself
+   * is never returned, even if it is of the queried type.   
    * 
    * @param <T>
    *          the covered JCAs type.
@@ -513,6 +516,9 @@
    * The method only returns properly covered annotations, that is annotations where the begin/end
    * offsets are equal to the 'covering' annotation or where both the begin/end are included in
    * the span of the 'covering' annotation. Partially overlapping annotations are not returned.
+   * <p>
+   * When querying for the annotations covered by a given annotation, the given annotation itself
+   * is never returned, even if it is of the queried type.
    * 
    * @param <T>
    *          the covering JCas type.
diff --git a/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java b/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java
index be5beee..becbd21 100644
--- a/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java
+++ b/uimafit-core/src/test/java/org/apache/uima/fit/util/JCasUtilTest.java
@@ -789,6 +789,37 @@
     index = indexCovering(jCas, Token.class, Sentence.class);
     // Check the first token is not contained in any sentence
     assertFalse(!index.get(tokens.get(0)).isEmpty());
+    
+    // Check if the reference annotation itself is returned
+    Token extra = new Token(jCas, tokens.get(3).getBegin(), tokens.get(3).getEnd());
+    extra.addToIndexes();
+    Map<Token, Collection<Token>> index2 = indexCovering(jCas, Token.class, Token.class);
+    assertEquals(0, index2.get(0).size());
+    assertEquals(1, index2.get(extra).size());
+    assertEquals(tokens.get(3), index2.get(extra).iterator().next());
+  }
+
+  @Test
+  public void testIndexCovered() throws Exception {
+    String text = "Will you come home today ? \n No , tomorrow !";
+    tokenBuilder.buildTokens(jCas, text);
+
+    List<Sentence> sentences = new ArrayList<Sentence>(select(jCas, Sentence.class));
+    List<Token> tokens = new ArrayList<Token>(select(jCas, Token.class));
+    
+    Map<Sentence, Collection<Token>> index = indexCovered(jCas, Sentence.class, Token.class);
+    
+    // Check covered annotations are found
+    assertEquals(tokens.subList(0, 6), index.get(sentences.get(0)));
+    assertEquals(tokens.subList(6, 10), index.get(sentences.get(1)));
+    
+    // Check if the reference annotation itself is returned
+    Token extra = new Token(jCas, tokens.get(3).getBegin(), tokens.get(3).getEnd());
+    extra.addToIndexes();
+    Map<Token, Collection<Token>> index2 = indexCovered(jCas, Token.class, Token.class);
+    assertEquals(0, index2.get(0).size());
+    assertEquals(1, index2.get(extra).size());
+    assertEquals(tokens.get(3), index2.get(extra).iterator().next());
   }
   
   @Test