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 2011/09/09 23:58:37 UTC
svn commit: r1167371 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/util/Incrementor.java
test/java/org/apache/commons/math/util/IncrementorTest.java
Author: erans
Date: Fri Sep 9 21:58:37 2011
New Revision: 1167371
URL: http://svn.apache.org/viewvc?rev=1167371&view=rev
Log:
New constructor and "canIncrement" method.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java?rev=1167371&r1=1167370&r2=1167371&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java Fri Sep 9 21:58:37 2011
@@ -36,7 +36,28 @@ public class Incrementor {
private int count;
/**
+ * Default constructor.
+ * For the new instance to be useful, the maximal count must be set
+ * by calling {@link #setMaximalCount(int) setMaximalCount}.
+ */
+ public Incrementor() {
+ this(0);
+ }
+
+ /**
+ * Defines a maximal count.
+ *
+ * @param max Maximal count.
+ */
+ public Incrementor(int max) {
+ maximalCount = max;
+ count = 0;
+ }
+
+ /**
* Set the upper limit for the counter.
+ * This does not automatically reset the current count to zero (see
+ * {@link #resetCount()}).
*
* @param max Upper limit of the counter.
*/
@@ -63,6 +84,17 @@ public class Incrementor {
}
/**
+ * Check whether a single increment is allowed.
+ *
+ * @return {@code false} if the next call to {@link #incrementCount(int)
+ * incrementCount} will trigger a {@code MaxCountExceededException},
+ * {@code true} otherwise.
+ */
+ public boolean canIncrement() {
+ return count < maximalCount;
+ }
+
+ /**
* Perform multiple increments.
* See the other {@link #incrementCount() incrementCount} method).
*
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java?rev=1167371&r1=1167370&r2=1167371&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java Fri Sep 9 21:58:37 2011
@@ -21,6 +21,48 @@ import org.junit.Test;
* Test for {@link Incrementor}.
*/
public class IncrementorTest {
+ @Test
+ public void testConstructor1() {
+ final Incrementor i = new Incrementor();
+ Assert.assertEquals(0, i.getMaximalCount());
+ Assert.assertEquals(0, i.getCount());
+ }
+
+ @Test
+ public void testConstructor2() {
+ final Incrementor i = new Incrementor(10);
+ Assert.assertEquals(10, i.getMaximalCount());
+ Assert.assertEquals(0, i.getCount());
+ }
+
+ @Test
+ public void testCanIncrement1() {
+ final Incrementor i = new Incrementor(3);
+ Assert.assertTrue(i.canIncrement());
+ i.incrementCount();
+ Assert.assertTrue(i.canIncrement());
+ i.incrementCount();
+ Assert.assertTrue(i.canIncrement());
+ i.incrementCount();
+ Assert.assertFalse(i.canIncrement());
+ }
+
+ @Test
+ public void testCanIncrement2() {
+ final Incrementor i = new Incrementor(3);
+ while (i.canIncrement()) {
+ i.incrementCount();
+ }
+
+ // Must keep try/catch because the exception must be generated here,
+ // and not in the previous loop.
+ try {
+ i.incrementCount();
+ Assert.fail("MaxCountExceededException expected");
+ } catch (MaxCountExceededException e) {
+ // Expected.
+ }
+ }
@Test
public void testAccessor() {
@@ -43,7 +85,7 @@ public class IncrementorTest {
Assert.assertEquals(3, i.getCount());
}
- @Test(expected = MaxCountExceededException.class)
+ @Test(expected=MaxCountExceededException.class)
public void testAboveMaxCount() {
final Incrementor i = new Incrementor();