MATH-397
New "Incrementor" utility class to encapsulate the counter of function evaluations. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@990743 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
79c6cb129d
commit
320194b3cb
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue