This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch feature/UIMA-5929-Add-exists-method-to-CasUtil
in repository https://gitbox.apache.org/repos/asf/uima-uimafit.git

commit 531cd005d78b1ed517b0b35685ffb3b62eff2489
Author: Richard Eckart de Castilho <r...@apache.org>
AuthorDate: Wed Dec 5 09:36:43 2018 +0100

    [UIMA-5929] Add exists() method to CasUtil
    
    - Added CasUtil.exists() method
    - Added corresponding unit test
---
 .../main/java/org/apache/uima/fit/util/CasUtil.java   | 16 ++++++++++++++++
 .../java/org/apache/uima/fit/util/CasUtilTest.java    | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+)

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 869faee..dc97d5e 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
@@ -46,6 +46,7 @@ import org.apache.uima.cas.TypeSystem;
 import org.apache.uima.cas.impl.Subiterator;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.jcas.tcas.Annotation;
 
 /**
@@ -1187,6 +1188,21 @@ public final class CasUtil {
   }
 
   /**
+   * Test if a CAS contains an annotation of the given type.
+   * 
+   * @param <T>
+   *          the annotation type.
+   * @param aCas
+   *          a CAS.
+   * @param aType
+   *          a annotation type.
+   * @return {@code true} if there is at least one annotation of the given 
type in the CAS.
+   */
+  public static <T extends TOP> boolean exists(CAS aCas, Type aType) {
+    return CasUtil.iterator(aCas, aType).hasNext();
+  }
+  
+  /**
    * Convenience method to get the specified view or a default view if the 
requested view does not
    * exist. The default can also be {@code null}.
    * 
diff --git 
a/uimafit-core/src/test/java/org/apache/uima/fit/util/CasUtilTest.java 
b/uimafit-core/src/test/java/org/apache/uima/fit/util/CasUtilTest.java
index f1a1595..67d0ae7 100644
--- a/uimafit-core/src/test/java/org/apache/uima/fit/util/CasUtilTest.java
+++ b/uimafit-core/src/test/java/org/apache/uima/fit/util/CasUtilTest.java
@@ -19,6 +19,7 @@
 package org.apache.uima.fit.util;
 
 import static java.util.Arrays.asList;
+import static 
org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription;
 import static org.apache.uima.fit.util.CasUtil.getAnnotationType;
 import static org.apache.uima.fit.util.CasUtil.getType;
 import static org.apache.uima.fit.util.CasUtil.iterator;
@@ -27,12 +28,16 @@ import static org.apache.uima.fit.util.CasUtil.select;
 import static org.apache.uima.fit.util.CasUtil.selectByIndex;
 import static org.apache.uima.fit.util.CasUtil.selectFS;
 import static org.apache.uima.fit.util.CasUtil.toText;
+import static org.apache.uima.fit.util.CasUtil.exists;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.util.Collection;
 import java.util.Iterator;
 
+import org.apache.uima.UIMAException;
 import org.apache.uima.cas.ArrayFS;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.FeatureStructure;
@@ -42,6 +47,7 @@ import org.apache.uima.fit.ComponentTestBase;
 import org.apache.uima.fit.type.Token;
 import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.util.CasCreationUtils;
 import org.junit.Test;
 
 /**
@@ -186,4 +192,17 @@ public class CasUtilTest extends ComponentTestBase {
     assertEquals(asList("Rot", "wood", "cheeses", "dew?"),
             toText((Iterable<AnnotationFS>) (Iterable) selectFS(cas, 
getType(cas, Token.class))));
   }
+  
+  @Test
+  public void testExists() throws UIMAException {
+    CAS cas = CasCreationUtils.createCas(createTypeSystemDescription(), null, 
null);
+
+    Type tokenType = CasUtil.getAnnotationType(cas, Token.class);
+    
+    assertFalse(exists(cas, tokenType));
+
+    cas.addFsToIndexes(cas.createAnnotation(tokenType, 0, 1));
+
+    assertTrue(exists(cas, tokenType));
+  }
 }

Reply via email to