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

This commit is contained in:
Gilles 2018-06-12 15:11:58 +02:00
parent e37de249b1
commit 00a0c6cb86
3 changed files with 36 additions and 3 deletions

View 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>

View 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();
}
}
/**

View 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;