MATH-1463: Abide by class "Iterator" contract.
authorGilles <erans@apache.org>
Tue, 12 Jun 2018 13:11:58 +0000 (15:11 +0200)
committerGilles <erans@apache.org>
Tue, 12 Jun 2018 13:11:58 +0000 (15:11 +0200)
src/changes/changes.xml
src/main/java/org/apache/commons/math4/util/IntegerSequence.java
src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java

index 7ce6920..fa75a72 100644 (file)
@@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="fix" issue="MATH-1463">
+        "IntegerSequence.incrementor": Throw "NoSuchElementException" from "next" method.
+      </action>
       <action dev="erans" type="add" issue="MATH-1459" due-to="Adrian Porter">
         Create a way to automatically calculate a Jacobian matrix using a differentiator.
       </action>
index 56dd745..b4db78c 100644 (file)
@@ -17,6 +17,7 @@
 package org.apache.commons.math4.util;
 
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 import org.apache.commons.math4.exception.MaxCountExceededException;
 import org.apache.commons.math4.exception.NullArgumentException;
 import org.apache.commons.math4.exception.MathUnsupportedOperationException;
@@ -352,9 +353,14 @@ public class IntegerSequence {
         /** {@inheritDoc} */
         @Override
         public Integer next() {
-            final int value = count;
-            count += increment;
-            return value;
+            if (canIncrement(0)) {
+                final int value = count;
+                count += increment;
+                return value;
+            } else {
+                // Contract for "Iterator".
+                throw new NoSuchElementException();
+            }
         }
 
         /**
index 6023f77..857188b 100644 (file)
@@ -15,6 +15,7 @@ package org.apache.commons.math4.util;
 
 import java.util.List;
 import java.util.ArrayList;
+import java.util.NoSuchElementException;
 import org.apache.commons.math4.exception.MaxCountExceededException;
 import org.apache.commons.math4.exception.TooManyEvaluationsException;
 import org.apache.commons.math4.exception.NotStrictlyPositiveException;
@@ -274,6 +275,29 @@ public class IntegerSequenceTest {
         }
     }
 
+    @Test
+    public void testIteratorNext() {
+        final int start = 1;
+        final int max = 2;
+        final int step = 1;
+
+        final IntegerSequence.Incrementor inc
+            = IntegerSequence.Incrementor.create()
+            .withStart(start)
+            .withMaximalCount(max)
+            .withIncrement(step);
+
+        Assert.assertTrue(inc.hasNext());
+        Assert.assertEquals(1, inc.next().intValue());
+        Assert.assertFalse(inc.hasNext());
+        try {
+            inc.next();
+            Assert.fail("exception expected");
+        } catch (NoSuchElementException e) {
+            // Expected.
+        }
+    }
+
     @Test(expected=TooManyEvaluationsException.class)
     public void testIncrementorAlternateException() {
         final int start = 1;