You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/10/03 21:09:34 UTC
svn commit: r1763212 - in
/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src:
main/java/org/apache/uima/cas/impl/FsIterator_limited.java
test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java
Author: schor
Date: Mon Oct 3 21:09:34 2016
New Revision: 1763212
URL: http://svn.apache.org/viewvc?rev=1763212&view=rev
Log:
[UIMA-5115] limit() fixes, incl test case.
Modified:
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java?rev=1763212&r1=1763211&r2=1763212&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/cas/impl/FsIterator_limited.java Mon Oct 3 21:09:34 2016
@@ -25,7 +25,7 @@ import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.FeatureStructure;
/**
- * Wraps FSIterator<T>, limits results to n
+ * Wraps FSIterator<T>, limits results to n gets
*/
class FsIterator_limited<T extends FeatureStructure>
implements LowLevelIterator<T> {
@@ -39,63 +39,60 @@ class FsIterator_limited<T extends Featu
this.limit = limit;
}
- private void makeInvalid() {
- iterator.moveToFirst();
- iterator.moveToPrevious();
+ private void maybeMakeInvalid() {
+ if (count == limit) {
+ iterator.moveToFirst();
+ iterator.moveToPrevious();
+ }
}
public T get() throws NoSuchElementException {
- if (count == limit) {
- makeInvalid();
- throw new NoSuchElementException();
- }
+ maybeMakeInvalid();
T r = iterator.get();
- count++;
- if (count == limit) {
- makeInvalid();
- }
+ count++;
return r;
}
public T getNvc() {
- if (count == limit) {
- makeInvalid();
- throw new NoSuchElementException();
- }
+ maybeMakeInvalid();
T r = iterator.getNvc();
count++;
- if (count == limit) {
- makeInvalid();
- }
return r;
}
public void moveToNext() {
+ maybeMakeInvalid();
iterator.moveToNext();
}
public void moveToNextNvc() {
+ maybeMakeInvalid();
iterator.moveToNextNvc();
}
public void moveToPrevious() {
+ maybeMakeInvalid();
iterator.moveToPrevious();
}
public void moveToPreviousNvc() {
+ maybeMakeInvalid();
iterator.moveToPreviousNvc();
}
public void moveToFirst() {
iterator.moveToFirst();
+ maybeMakeInvalid();
}
public void moveToLast() {
iterator.moveToLast();
+ maybeMakeInvalid();
}
public void moveTo(FeatureStructure fs) {
iterator.moveTo(fs);
+ maybeMakeInvalid();
}
public FSIterator<T> copy() {
@@ -103,6 +100,7 @@ class FsIterator_limited<T extends Featu
}
public boolean isValid() {
+ maybeMakeInvalid();
return iterator.isValid();
}
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java?rev=1763212&r1=1763211&r2=1763212&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/AnnotationIteratorTest.java Mon Oct 3 21:09:34 2016
@@ -299,6 +299,10 @@ public class AnnotationIteratorTest exte
assertCount("Subiterator over annot with big bound, strict", 33, it);
select_it = cas.<AnnotationFS>select().coveredBy((Annotation) bigBound).endWithinBounds().fsIterator();
assertCount("Subiterator select over annot with big bound, strict", 33, select_it);
+
+ select_it = cas.<AnnotationFS>select().coveredBy((Annotation) bigBound).limit(7).endWithinBounds().fsIterator();
+ assertCountLimit("Subiterator select limit 7 over annot with big bound, strict", 7, select_it);
+
it = annotIndex.subiterator(bigBound, false, true); // unambiguous, strict
assertCount("Subiterator over annot unambiguous strict", 3, it);
@@ -340,30 +344,10 @@ public class AnnotationIteratorTest exte
private String flatStateMsg(String s) {
return s + (isSave ? "" : " with flattened index");
}
-
private void assertCount(String msg, int expected, FSIterator<? extends AnnotationFS> it) {
+ int fssStart = assertCountCmn(msg, expected, it);
msg = flatStateMsg(msg);
- int count = 0;
- callCount ++;
- int fssStart;
- if (isSave) {
- fssStarts.add(fssStart = fss.size());
- } else {
- fssStart = fssStarts.get(callCount);
- }
- while (it.isValid()) {
- ++count;
- AnnotationFS fs = it.next();
- if (showFSs) {
- System.out.format("%d " + msg + " fs begin: %d end: %d type: %s%n", count, fs.getBegin(), fs.getEnd(), fs.getType().getName() );
- }
- if (isSave) {
- fss.add(fs);
- } else {
- assertEquals(msg, fss.get(fssStart + count -1).hashCode(), fs.hashCode());
- }
- }
- assertEquals(msg, expected, count);
+ int count = expected;
if (count > 0) {
// test moveTo(fs) in middle, first, and last
AnnotationFS posFs = fss.get(fssStart + (count >> 1));
@@ -401,6 +385,41 @@ public class AnnotationIteratorTest exte
assertEquals(msg, expected, count);
}
+ private int assertCountCmn(String msg, int expected, FSIterator<? extends AnnotationFS> it) {
+ msg = flatStateMsg(msg);
+ int count = 0;
+ callCount ++;
+ int fssStart;
+ if (isSave) {
+ fssStarts.add(fssStart = fss.size());
+ } else {
+ fssStart = fssStarts.get(callCount);
+ }
+ while (it.isValid()) {
+ ++count;
+ AnnotationFS fs = it.next();
+ if (showFSs) {
+ System.out.format("%d " + msg + " fs begin: %d end: %d type: %s%n", count, fs.getBegin(), fs.getEnd(), fs.getType().getName() );
+ }
+ if (isSave) {
+ fss.add(fs);
+ } else {
+ assertEquals(msg, fss.get(fssStart + count -1).hashCode(), fs.hashCode());
+ }
+ }
+ assertEquals(msg, expected, count);
+ return fssStart;
+ }
+
+
+ private void assertCountLimit(String msg, int expected, FSIterator<? extends AnnotationFS> it) {
+ assertCountCmn(msg, expected, it);
+ it.moveToFirst();
+ assertFalse(it.isValid());
+ }
+
+
+
public void testIncorrectIndexTypeException() {
boolean caughtException = false;
try {