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 2010/08/30 11:38:39 UTC

svn commit: r990743 - 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: Mon Aug 30 09:38:39 2010
New Revision: 990743

URL: http://svn.apache.org/viewvc?rev=990743&view=rev
Log:
MATH-397
New "Incrementor" utility class to encapsulate the counter of function
evaluations.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java   (with props)

Added: 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=990743&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java Mon Aug 30 09:38:39 2010
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.util;
+
+import org.apache.commons.math.exception.MaxCountExceededException;
+
+/**
+ * Utility that increments a counter until a maximum is reached, at which
+ * point it will throw an exception.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class Incrementor {
+    /**
+     * Upper limit for the counter.
+     */
+    private int maximalCount;
+    /**
+     * Current count.
+     */
+    private int count;
+
+    /**
+     * Set the upper limit for the counter.
+     *
+     * @param count Upper limit of the counter.
+     */
+    public void setMaximalCount(int count) {
+        maximalCount = count;
+    }
+
+    /**
+     * Get the upper limit of the counter.
+     *
+     * @return the counter upper limit.
+     */
+    public int getMaximalCount() {
+        return maximalCount;
+    }
+
+    /**
+     * Get the current count.
+     *
+     * @return the current count.
+     */
+    public int getCount() {
+        return count;
+    }
+
+    /**
+     * Perform multiple increments.
+     * See the other {@link #incrementCount() incrementCount} method).
+     *
+     * @param value Number of increments.
+     * @throws MaxCountExceededException at counter exhaustion.
+     */
+    public void incrementCount(int value) {
+        for (int i = 0; i < value; i++) {
+            incrementCount();
+        }
+    }
+
+    /**
+     * Add one to the current iteration count.
+     *
+     * @throws MaxCountExceededException at counter exhaustion.
+     */
+    public void incrementCount() {
+        if (++count > maximalCount) {
+            throw new MaxCountExceededException(maximalCount);
+        }
+    }
+
+    /**
+     * Reset the counter to 0.
+     */
+    public void resetCount() {
+        count = 0;
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Incrementor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=990743&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java Mon Aug 30 09:38:39 2010
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
+ * or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.apache.commons.math.util;
+
+import org.apache.commons.math.exception.MaxCountExceededException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for {@link Incrementor}.
+ */
+public class IncrementorTest {
+
+    @Test
+    public void testAccessor() {
+        final Incrementor i = new Incrementor();
+
+        i.setMaximalCount(10);
+        Assert.assertEquals(10, i.getMaximalCount());
+        Assert.assertEquals(0, i.getCount());
+    }
+
+    @Test
+    public void testBelowMaxCount() {
+        final Incrementor i = new Incrementor();
+
+        i.setMaximalCount(3);
+        i.incrementCount();
+        i.incrementCount();
+        i.incrementCount();
+
+        Assert.assertEquals(3, i.getCount());
+    }
+
+    @Test(expected = MaxCountExceededException.class)
+    public void testAboveMaxCount() {
+        final Incrementor i = new Incrementor();
+
+        i.setMaximalCount(3);
+        i.incrementCount();
+        i.incrementCount();
+        i.incrementCount();
+        i.incrementCount();
+    }
+
+    @Test
+    public void testReset() {
+        final Incrementor i = new Incrementor();
+
+        i.setMaximalCount(3);
+        i.incrementCount();
+        i.incrementCount();
+        i.incrementCount();
+        Assert.assertEquals(3, i.getCount());
+        i.resetCount();
+        Assert.assertEquals(0, i.getCount());
+    }
+
+    @Test
+    public void testBulkIncrement() {
+        final Incrementor i = new Incrementor();
+
+        i.setMaximalCount(3);
+        i.incrementCount(2);
+        Assert.assertEquals(2, i.getCount());
+        i.incrementCount(1);
+        Assert.assertEquals(3, i.getCount());
+    }
+}
\ No newline at end of file

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/IncrementorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native