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 2017/07/20 14:02:20 UTC

svn commit: r1802488 - /uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java

Author: schor
Date: Thu Jul 20 14:02:20 2017
New Revision: 1802488

URL: http://svn.apache.org/viewvc?rev=1802488&view=rev
Log:
[UIMA-5504] implement common approach to iterator validity checking in one spot, shared by all classes.

Modified:
    uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java

Modified: uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java
URL: http://svn.apache.org/viewvc/uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java?rev=1802488&r1=1802487&r2=1802488&view=diff
==============================================================================
--- uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java (original)
+++ uima/uv3/uimaj-v3/trunk/uimaj-core/src/main/java/org/apache/uima/cas/FSIterator.java Thu Jul 20 14:02:20 2017
@@ -87,42 +87,51 @@ public interface FSIterator<T extends Fe
    * @exception NoSuchElementException
    *              If the iterator is not valid.
    */
-  T get() throws NoSuchElementException;
+  default T get() throws NoSuchElementException {
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    return getNvc();
+  }
 
   /**
    * Get the structure the iterator is pointing at.
    * Throws various unchecked exceptions, if the iterator is not valid
    * @return The structure the iterator is pointing at.
    */
-  default T getNvc() {
-    return get();
-  }
+  T getNvc();
   
   /**
    * Advance the iterator. This may invalidate the iterator.
    * @exception ConcurrentModificationException if the underlying indexes being iterated over were modified
    */
-  void moveToNext();
+  default void moveToNext() {
+    if (!isValid()) {
+      return;
+    }
+    moveToNextNvc();
+  }
 
   /**
    * version of moveToNext which bypasses the isValid check - call only if you've just done this check yourself
    */
-  default void moveToNextNvc() {
-    moveToNext();
-  }
+  void moveToNextNvc();
   
   /**
    * Move the iterator one element back. This may invalidate the iterator.
    * @exception ConcurrentModificationException if the underlying indexes being iterated over were modified
    */
-  void moveToPrevious();
+  default void moveToPrevious() {
+    if (!isValid()) {
+      return;
+    }
+    moveToPreviousNvc();
+  }
 
   /**
    * version of moveToPrevious which bypasses the isValid check - call only if you've just done this check yourself
    */
-  default void moveToPreviousNvc() {
-    moveToPrevious();
-  }
+  void moveToPreviousNvc();
 
   /**
    * Move the iterator to the first element. The iterator will be valid iff the underlying
@@ -147,8 +156,9 @@ public interface FSIterator<T extends Fe
    * If the fs is greater than all of the entries in the index, the moveTo cannot set the iterator to an insertion point
    * where the current feature structure is greater than fs, so it marks the iterator "invalid".
    * <p>
-   * If the underlying index is a bag index, no ordering is present, and the moveTo operation moves to the
-   * fs which is the same identical fs as the key. If no such fs is in the index, the iterator is marked 
+   * If the underlying index is a set or bag index, no ordering is present, and the moveTo operation moves to the
+   * fs which is the same identical fs as the key (for a bag), or to the element which matches equal
+   * according to the index's comparator (for a set). If no such fs is in the index, the iterator is marked 
    * invalid.
    * 
    * @param fs
@@ -194,9 +204,10 @@ public interface FSIterator<T extends Fe
    */
   @Override
   default T next() {
-    T result = get();
-    moveToNext();
-    return result;
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    return nextNvc();
   }
   
   default T nextNvc() {
@@ -222,12 +233,19 @@ public interface FSIterator<T extends Fe
 
   @Override
   default T previous() {
-    T result = get();
-    moveToPrevious();
+    if (!isValid()) {
+      throw new NoSuchElementException();
+    }
+    return previousNvc();
+  }
+  
+  default T previousNvc() {
+    T result = getNvc();
+    moveToPreviousNvc();
     return result;
-
   }
 
+
   @Override
   default int nextIndex() {
     throw new UnsupportedOperationException();