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

rec pushed a commit to branch 
bugfix/#368-select(AnnotationBaseFs-type).count()-seems-to-return-MAX_LONG
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 0f3ed2b5a2f3df0fb76e07e61c1782b6af4bcbca
Author: Richard Eckart de Castilho <[email protected]>
AuthorDate: Fri Aug 16 18:19:59 2024 +0200

    Issue #368: select(AnnotationBaseFs-type).count() seems to return MAX_LONG
    
    - If no index is available, fall back to the altSourceIterator or the 
fsIterator
    - Added test
---
 .../org/apache/uima/cas/impl/SelectFSs_impl.java   | 25 ++++++++++++--------
 .../org/apache/uima/cas/impl/SelectFsTest.java     | 27 +++++++++++++++++-----
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git 
a/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java 
b/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
index 69d793e81..478d9a1b9 100644
--- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
+++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/SelectFSs_impl.java
@@ -927,16 +927,15 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
       filtered = filteredItems
               .toArray((T[]) Array.newInstance(FeatureStructure.class, 
filteredItems.size()));
     } else {
-
       // skip filtering if nullOK and no subsumption test needed because type 
= TOP or higher
       boolean noTypeFilter = ti == view.getTypeSystemImpl().topType;
       if (!isNullOK && noTypeFilter) {
         return new FsIterator_subtypes_snapshot<>((T[]) sourceFSArray, null, 
IS_UNORDERED, null);
       }
 
-      List<T> filteredItems = new ArrayList<>();
+      var filteredItems = new ArrayList<T>();
       boolean noNullsWereFiltered = true;
-      for (FeatureStructure item : sourceFSArray) {
+      for (var item : sourceFSArray) {
         if (!isNullOK && null == item) {
           noNullsWereFiltered = false;
           continue; // null items may be skipped
@@ -951,11 +950,11 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
         return new FsIterator_subtypes_snapshot<>((T[]) sourceFSArray, null, 
IS_UNORDERED, null);
       }
 
-      filtered = filteredItems
-              .toArray((T[]) Array.newInstance(FeatureStructure.class, 
filteredItems.size()));
+      filtered = (T[]) filteredItems.toArray(FeatureStructure[]::new);
     }
-    return new FsIterator_subtypes_snapshot<>(filtered, null, IS_UNORDERED, 
null); // items not
-                                                                               
    // sorted
+
+    // items not sorted
+    return new FsIterator_subtypes_snapshot<>(filtered, null, IS_UNORDERED, 
null);
   }
 
   @Override
@@ -1094,9 +1093,15 @@ public class SelectFSs_impl<T extends FeatureStructure> 
implements SelectFSs<T>
 
       @Override
       public long estimateSize() {
-        return ((characteristics & Spliterator.SIZED) == Spliterator.SIZED && 
localIndex != null)
-                ? localIndex.size()
-                : Long.MAX_VALUE;
+        if ((characteristics & Spliterator.SIZED) == Spliterator.SIZED && 
localIndex != null) {
+          return localIndex.size();
+        }
+
+        if (isAltSource) {
+          return altSourceIterator().size();
+        }
+
+        return fsIterator().size();
       }
 
       @Override
diff --git 
a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java 
b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
index 45b729c9d..80625bfa4 100644
--- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SelectFsTest.java
@@ -46,8 +46,6 @@ import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.cas.TOP;
 import org.apache.uima.jcas.tcas.Annotation;
-import org.apache.uima.resource.metadata.TypePriorities;
-import org.apache.uima.resource.metadata.TypePriorityList;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.test.junit_extension.JUnitExtension;
 import org.apache.uima.util.CasCreationUtils;
@@ -90,8 +88,8 @@ public class SelectFsTest {
     typeSystemDescription = UIMAFramework.getXMLParser()
             .parseTypeSystemDescription(new XMLInputSource(typeSystemFile1));
 
-    TypePriorities prios = 
getResourceSpecifierFactory().createTypePriorities();
-    TypePriorityList typePrioList = prios.addPriorityList();
+    var prios = getResourceSpecifierFactory().createTypePriorities();
+    var typePrioList = prios.addPriorityList();
     Arrays.stream(aPrioTypeNames).forEachOrdered(typePrioList::addType);
 
     cas = (CASImpl) CasCreationUtils.createCas(typeSystemDescription, prios, 
null);
@@ -302,10 +300,10 @@ public class SelectFsTest {
           throws Exception {
     setup(aMode, aPrioTypeNames);
 
-    TypeSystemDescription tsd = 
getResourceSpecifierFactory().createTypeSystemDescription();
+    var tsd = getResourceSpecifierFactory().createTypeSystemDescription();
     tsd.addType("test.Type1", "", "uima.tcas.Annotation");
 
-    CAS cas = CasCreationUtils.createCas(tsd, null, null, null);
+    var cas = CasCreationUtils.createCas(tsd, null, null, null);
 
     Type type1 = cas.getTypeSystem().getType("test.Type1");
 
@@ -1817,6 +1815,23 @@ public class SelectFsTest {
 
   }
 
+  @Test
+  void thatSelectAnnotationBaseFsCountWorks() throws Exception {
+    var tsd = getResourceSpecifierFactory().createTypeSystemDescription();
+    tsd.addType("test.Type1", "", CAS.TYPE_NAME_ANNOTATION_BASE);
+
+    var cas = CasCreationUtils.createCas(tsd, null, null, null);
+
+    var type1 = cas.getTypeSystem().getType("test.Type1");
+
+    assertThat(cas.select(type1).count()).isEqualTo(0);
+
+    var y = cas.createFS(type1);
+    cas.addFsToIndexes(y);
+
+    assertThat(cas.select(type1).count()).isEqualTo(1);
+  }
+
   @SuppressWarnings("unchecked")
   private static <T extends AnnotationFS, R extends AnnotationFS> List<R> 
toListBackwards(
           SelectFSs<T> select) {

Reply via email to