MATH-1088
Ensure that bad usage will raise an exception. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1558833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2c9438da23
commit
63d88c74b7
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.commons.math3.util;
|
package org.apache.commons.math3.util;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||||
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
|
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
|
||||||
import org.apache.commons.math3.exception.OutOfRangeException;
|
import org.apache.commons.math3.exception.OutOfRangeException;
|
||||||
|
@ -77,6 +78,10 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
||||||
* Unidimensional counter.
|
* Unidimensional counter.
|
||||||
*/
|
*/
|
||||||
private int count = -1;
|
private int count = -1;
|
||||||
|
/**
|
||||||
|
* Maximum value for {@link #count}.
|
||||||
|
*/
|
||||||
|
private final int maxCount = totalSize - 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an iterator
|
* Create an iterator
|
||||||
|
@ -90,19 +95,20 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
for (int i = 0; i < dimension; i++) {
|
return count < maxCount;
|
||||||
if (counter[i] != size[i] - 1) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the unidimensional count after the counter has been
|
* @return the unidimensional count after the counter has been
|
||||||
* incremented by {@code 1}.
|
* incremented by {@code 1}.
|
||||||
|
* @throws NoSuchElementException if {@link #hasNext()} would have
|
||||||
|
* returned {@code false}.
|
||||||
*/
|
*/
|
||||||
public Integer next() {
|
public Integer next() {
|
||||||
|
if (!hasNext()) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = last; i >= 0; i--) {
|
for (int i = last; i >= 0; i--) {
|
||||||
if (counter[i] == size[i] - 1) {
|
if (counter[i] == size[i] - 1) {
|
||||||
counter[i] = 0;
|
counter[i] = 0;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.commons.math3.util;
|
package org.apache.commons.math3.util;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||||
import org.apache.commons.math3.exception.OutOfRangeException;
|
import org.apache.commons.math3.exception.OutOfRangeException;
|
||||||
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
|
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
|
||||||
|
@ -24,7 +25,9 @@ import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Test cases for the {@link MultidimensionalCounter} class.
|
||||||
*
|
*
|
||||||
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class MultidimensionalCounterTest {
|
public class MultidimensionalCounterTest {
|
||||||
@Test
|
@Test
|
||||||
|
@ -100,6 +103,38 @@ public class MultidimensionalCounterTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIterator() {
|
||||||
|
final int dim1 = 3;
|
||||||
|
final int dim2 = 4;
|
||||||
|
|
||||||
|
final MultidimensionalCounter.Iterator iter
|
||||||
|
= new MultidimensionalCounter(dim1, dim2).iterator();
|
||||||
|
|
||||||
|
final int max = dim1 * dim2;
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
Assert.assertTrue(iter.hasNext());
|
||||||
|
|
||||||
|
// Should not throw.
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=NoSuchElementException.class)
|
||||||
|
public void testIteratorNoMoreElements() {
|
||||||
|
final MultidimensionalCounter.Iterator iter
|
||||||
|
= new MultidimensionalCounter(4, 2).iterator();
|
||||||
|
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No more elements: should throw.
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMulti2UniConversion() {
|
public void testMulti2UniConversion() {
|
||||||
final MultidimensionalCounter c = new MultidimensionalCounter(2, 4, 5);
|
final MultidimensionalCounter c = new MultidimensionalCounter(2, 4, 5);
|
||||||
|
@ -150,6 +185,8 @@ public class MultidimensionalCounterTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
final int totalSize = c.getSize();
|
final int totalSize = c.getSize();
|
||||||
|
Assert.assertEquals(expected.length, totalSize);
|
||||||
|
|
||||||
final int nDim = c.getDimension();
|
final int nDim = c.getDimension();
|
||||||
final MultidimensionalCounter.Iterator iter = c.iterator();
|
final MultidimensionalCounter.Iterator iter = c.iterator();
|
||||||
for (int i = 0; i < totalSize; i++) {
|
for (int i = 0; i < totalSize; i++) {
|
||||||
|
|
Loading…
Reference in New Issue