diff --git a/src/java/org/apache/commons/math/stat/Frequency.java b/src/java/org/apache/commons/math/stat/Frequency.java index a5b8d7b60..237c2f60b 100644 --- a/src/java/org/apache/commons/math/stat/Frequency.java +++ b/src/java/org/apache/commons/math/stat/Frequency.java @@ -101,9 +101,26 @@ public class Frequency implements Serializable { *

* * @param v the value to add. - * @throws IllegalArgumentException if v is not comparable. + * @throws IllegalArgumentException if v is not comparable with previous entries + * @throws ClassCastException if v is not Comparable + * @deprecated use {@link #addValue(Comparable)} instead */ + @Deprecated public void addValue(Object v) { + addValue((Comparable) v); + } + + /** + * Adds 1 to the frequency count for v. + *

+ * If other objects have already been added to this Frequency, v must + * be comparable to those that have already been added. + *

+ * + * @param v the value to add. + * @throws IllegalArgumentException if v is not comparable with previous entries + */ + public void addValue(Comparablev){ Object obj = v; if (v instanceof Integer) { obj = Long.valueOf(((Integer) v).longValue()); diff --git a/src/test/org/apache/commons/math/stat/FrequencyTest.java b/src/test/org/apache/commons/math/stat/FrequencyTest.java index cc8fd030e..b2e0b3a2d 100644 --- a/src/test/org/apache/commons/math/stat/FrequencyTest.java +++ b/src/test/org/apache/commons/math/stat/FrequencyTest.java @@ -189,6 +189,22 @@ public final class FrequencyTest extends TestCase { } } + // Check what happens when non-Comparable objects are added + public void testAddNonComparable(){ + try { + f.addValue(new Object()); // This was OK + fail("Expected ClassCastException"); + } catch (ClassCastException expected) { + } + f.clear(); + f.addValue(1); + try { + f.addValue(new Object()); + fail("Expected ClassCastException"); // Previously would have been IllegalArgument + } catch (ClassCastException expected) { + } + } + /** test empty table */ public void testEmptyTable() { assertEquals("freq sum, empty table", 0, f.getSumFreq());