Fixed two errors reported on commons-user / commons-dev:
1. addValue(object) and getXxx methods failing or returning incorrect results for Integer arguments when the freq table is not empty 2. getXxx methods failing / returning inconsistent values when invoked on an empty table. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141420 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
225cfc7ac2
commit
61c175d0ef
|
@ -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.25 $ $Date: 2004/07/02 05:27:41 $
|
||||
* @version $Revision: 1.26 $ $Date: 2004/08/12 15:33:39 $
|
||||
*/
|
||||
public class Frequency implements Serializable {
|
||||
|
||||
|
@ -114,6 +114,15 @@ public class Frequency implements Serializable {
|
|||
public void addValue(int v) {
|
||||
addValue(new Long(v));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 1 to the frequency count for v.
|
||||
*
|
||||
* @param v the value to add.
|
||||
*/
|
||||
public void addValue(Integer v) {
|
||||
addValue(new Long(v.longValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 1 to the frequency count for v.
|
||||
|
@ -170,6 +179,9 @@ public class Frequency implements Serializable {
|
|||
* @return the frequency of v.
|
||||
*/
|
||||
public long getCount(Object v) {
|
||||
if (v instanceof Integer) {
|
||||
return getCount(((Integer) v).longValue());
|
||||
}
|
||||
long result = 0;
|
||||
try {
|
||||
Long count = (Long) freqTable.get(v);
|
||||
|
@ -217,11 +229,16 @@ public class Frequency implements Serializable {
|
|||
/**
|
||||
* Returns the percentage of values that are equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns <code>Double.NaN</code> if no values have been added.
|
||||
*
|
||||
* @param v the value to lookup
|
||||
* @return the proportion of values equal to v
|
||||
*/
|
||||
public double getPct(Object v) {
|
||||
if (getSumFreq() == 0) {
|
||||
return Double.NaN;
|
||||
}
|
||||
return (double) getCount(v) / (double) getSumFreq();
|
||||
}
|
||||
|
||||
|
@ -269,6 +286,9 @@ public class Frequency implements Serializable {
|
|||
* @return the proportion of values equal to v
|
||||
*/
|
||||
public long getCumFreq(Object v) {
|
||||
if (getSumFreq() == 0) {
|
||||
return 0;
|
||||
}
|
||||
Comparator c = freqTable.comparator();
|
||||
if (c == null) {
|
||||
c = new NaturalComparator();
|
||||
|
@ -346,12 +366,17 @@ public class Frequency implements Serializable {
|
|||
* Returns the cumulative percentage of values less than or equal to v
|
||||
* (as a proportion between 0 and 1).
|
||||
* <p>
|
||||
* Returns 0 if v is not comparable to the values set.
|
||||
* Returns <code>Double.NaN</code> if no values have been added.
|
||||
* Returns 0 if at least one value has been added, but v is not comparable
|
||||
* to the values set.
|
||||
*
|
||||
* @param v the value to lookup
|
||||
* @return the proportion of values less than or equal to v
|
||||
*/
|
||||
public double getCumPct(Object v) {
|
||||
if (getSumFreq() == 0) {
|
||||
return Double.NaN;
|
||||
}
|
||||
return (double) getCumFreq(v) / (double) getSumFreq();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Test cases for the {@link Frequency} class.
|
||||
*
|
||||
* @version $Revision: 1.12 $ $Date: 2004/03/07 00:57:11 $
|
||||
* @version $Revision: 1.13 $ $Date: 2004/08/12 15:33:39 $
|
||||
*/
|
||||
|
||||
public final class FrequencyTest extends TestCase {
|
||||
|
@ -86,6 +86,20 @@ public final class FrequencyTest extends TestCase {
|
|||
assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance);
|
||||
f.clear();
|
||||
|
||||
f = null;
|
||||
Frequency f = new Frequency();
|
||||
f.addValue(1);
|
||||
f.addValue(new Integer(1));
|
||||
f.addValue(new Long(1));
|
||||
f.addValue(2);
|
||||
f.addValue(new Integer(-1));
|
||||
assertEquals("1 count", 3, f.getCount(1));
|
||||
assertEquals("1 count", 3, f.getCount(new Integer(1)));
|
||||
assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);
|
||||
assertEquals("1 pct", 0.6, f.getPct(new Integer(1)), tolerance);
|
||||
assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);
|
||||
assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);
|
||||
|
||||
f = null;
|
||||
f = new Frequency(String.CASE_INSENSITIVE_ORDER);
|
||||
f.addValue("one");
|
||||
|
@ -139,6 +153,19 @@ public final class FrequencyTest extends TestCase {
|
|||
assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance);
|
||||
}
|
||||
|
||||
/** test empty table */
|
||||
public void testEmptyTable() {
|
||||
assertEquals("freq sum, empty table", 0, f.getSumFreq());
|
||||
assertEquals("count, empty table", 0, f.getCount(0));
|
||||
assertEquals("count, empty table",0, f.getCount(new Integer(0)));
|
||||
assertEquals("cum freq, empty table", 0, f.getCumFreq(0));
|
||||
assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));
|
||||
assertTrue("pct, empty table", Double.isNaN(f.getPct(0)));
|
||||
assertTrue("pct, empty table", Double.isNaN(f.getPct(new Integer(0))));
|
||||
assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0)));
|
||||
assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(new Integer(0))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests toString()
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue