Author: joern
Date: Wed Apr 27 08:35:05 2016
New Revision: 1741161

URL: http://svn.apache.org/viewvc?rev=1741161&view=rev
Log:
OPENNLP-835 Fix early termination, reset behavior and minor memory leak

The class SequenceStreamEventStream has a few bugs.
(1) It truncates the stream early if any sequence is empty.
(2) After reset, it will emit the remaining elements from the underlying 
sequence that was being iterated over before the reset, and then start over 
from the beginning.
(3) It leaks memory by not discarding references to objects it doesn't need 
anymore.

Thanks to Steven Taschuk for providing a patch. 

Modified:
    
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/SequenceStreamEventStream.java

Modified: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/SequenceStreamEventStream.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/SequenceStreamEventStream.java?rev=1741161&r1=1741160&r2=1741161&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/SequenceStreamEventStream.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/SequenceStreamEventStream.java
 Wed Apr 27 08:35:05 2016
@@ -28,7 +28,6 @@ import opennlp.tools.util.ObjectStream;
 
 /**
  * Class which turns a sequence stream into an event stream.
- *
  */
 public class SequenceStreamEventStream implements ObjectStream<Event> {
 
@@ -42,32 +41,25 @@ public class SequenceStreamEventStream i
 
   @Override
   public Event read() throws IOException {
-
-    if (eventIt.hasNext()) {
-      return eventIt.next();
-    }
-    else {
+    while (!eventIt.hasNext()) {
       Sequence<?> sequence = sequenceStream.read();
-
-      if (sequence != null) {
-        eventIt = Arrays.asList(sequence.getEvents()).iterator();
-      }
-
-      if (eventIt.hasNext()) {
-        return read();
+      if (sequence == null) {
+        return null;
       }
+      eventIt = Arrays.asList(sequence.getEvents()).iterator();
     }
-
-    return null;
+    return eventIt.next();
   }
 
   @Override
   public void reset() throws IOException, UnsupportedOperationException {
+    eventIt = Collections.emptyListIterator();
     sequenceStream.reset();
   }
 
   @Override
   public void close() throws IOException {
+    eventIt = Collections.emptyListIterator();
     sequenceStream.close();
   }
 }


Reply via email to