You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/06/12 13:12:52 UTC

[math] MATH-1463: Abide by class "Iterator" contract.

Repository: commons-math
Updated Branches:
  refs/heads/master e37de249b -> 00a0c6cb8


MATH-1463: Abide by class "Iterator" contract.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/00a0c6cb
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/00a0c6cb
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/00a0c6cb

Branch: refs/heads/master
Commit: 00a0c6cb866b01c0d48debae0faf884038279144
Parents: e37de24
Author: Gilles <er...@apache.org>
Authored: Tue Jun 12 15:11:58 2018 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue Jun 12 15:11:58 2018 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 +++
 .../commons/math4/util/IntegerSequence.java     | 12 +++++++---
 .../commons/math4/util/IntegerSequenceTest.java | 24 ++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/00a0c6cb/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7ce6920..fa75a72 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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>

http://git-wip-us.apache.org/repos/asf/commons-math/blob/00a0c6cb/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/IntegerSequence.java b/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
index 56dd745..b4db78c 100644
--- a/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
+++ b/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
@@ -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();
+            }
         }
 
         /**

http://git-wip-us.apache.org/repos/asf/commons-math/blob/00a0c6cb/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java b/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
index 6023f77..857188b 100644
--- a/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
+++ b/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
@@ -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;