From 9ffd5866c4dfcf8cb435e03f8bd2e57dbf3221f4 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Sun, 19 Sep 2004 22:47:27 +0000 Subject: [PATCH] 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 --- src/java/org/apache/commons/math/stat/Frequency.java | 9 ++++++++- src/test/org/apache/commons/math/stat/FrequencyTest.java | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/math/stat/Frequency.java b/src/java/org/apache/commons/math/stat/Frequency.java index 331b93525..48ea42000 100644 --- a/src/java/org/apache/commons/math/stat/Frequency.java +++ b/src/java/org/apache/commons/math/stat/Frequency.java @@ -35,7 +35,7 @@ import java.util.TreeMap; * The values are ordered using the default (natural order), unless a * Comparator 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. + *

+ * 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(); diff --git a/src/test/org/apache/commons/math/stat/FrequencyTest.java b/src/test/org/apache/commons/math/stat/FrequencyTest.java index 5156d1d93..806422d92 100644 --- a/src/test/org/apache/commons/math/stat/FrequencyTest.java +++ b/src/test/org/apache/commons/math/stat/FrequencyTest.java @@ -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);