Fixed error computing cumulative frequencies when actual parameter is an Integer. Reported to commons-dev list by Jon Langlois.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141445 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2004-09-19 22:47:27 +00:00
parent 71e95c4c4d
commit 9ffd5866c4
2 changed files with 11 additions and 2 deletions

View File

@ -35,7 +35,7 @@ import java.util.TreeMap;
* The values are ordered using the default (natural order), unless a
* <code>Comparator</code> is supplied in the constructor.
*
* @version $Revision: 1.27 $ $Date: 2004/08/22 01:42:58 $
* @version $Revision: 1.28 $ $Date: 2004/09/19 22:47:27 $
*/
public class Frequency implements Serializable {
@ -149,6 +149,10 @@ public class Frequency implements Serializable {
/**
* Returns an Iterator over the set of values that have been added.
* <p>
* If added values are itegral (i.e., integers, longs, Integers, or Longs),
* they are converted to Longs when they are added, so the objects returned
* by the Iterator will in this case be Longs.
*
* @return values Iterator
*/
@ -289,6 +293,9 @@ public class Frequency implements Serializable {
if (getSumFreq() == 0) {
return 0;
}
if (v instanceof Integer) {
return getCumFreq(((Integer) v).longValue());
}
Comparator c = freqTable.comparator();
if (c == null) {
c = new NaturalComparator();

View File

@ -26,7 +26,7 @@ import junit.framework.TestSuite;
/**
* Test cases for the {@link Frequency} class.
*
* @version $Revision: 1.13 $ $Date: 2004/08/12 15:33:39 $
* @version $Revision: 1.14 $ $Date: 2004/09/19 22:47:27 $
*/
public final class FrequencyTest extends TestCase {
@ -69,6 +69,7 @@ public final class FrequencyTest extends TestCase {
assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));
assertEquals("one cumulative frequency", 3, f.getCumFreq(1));
assertEquals("two cumulative frequency", 4, f.getCumFreq(2));
assertEquals("Integer argument cum freq",4, f.getCumFreq(new Integer(2)));
assertEquals("five cumulative frequency", 4, f.getCumFreq(5));
assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo"));
@ -128,6 +129,7 @@ public final class FrequencyTest extends TestCase {
assertEquals("foo pct",0,f.getPct("foo"),tolerance);
assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance);
assertEquals("two cum pct",0.50,f.getCumPct(new Long(2)),tolerance);
assertEquals("Integer argument",0.50,f.getCumPct(new Integer(2)),tolerance);
assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance);
assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance);
assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance);