Author: sebb Date: Fri Apr 17 13:34:59 2009 New Revision: 765996 URL: http://svn.apache.org/viewvc?rev=765996&view=rev Log: MATH-259 - check for Comparable when adding values
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java commons/proper/math/trunk/src/test/org/apache/commons/math/stat/FrequencyTest.java Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java?rev=765996&r1=765995&r2=765996&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java Fri Apr 17 13:34:59 2009 @@ -101,9 +101,26 @@ * </p> * * @param v the value to add. - * @throws IllegalArgumentException if <code>v</code> is not comparable. + * @throws IllegalArgumentException if <code>v</code> is not comparable with previous entries + * @throws ClassCastException if <code>v</code> 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. + * <p> + * If other objects have already been added to this Frequency, v must + * be comparable to those that have already been added. + * </p> + * + * @param v the value to add. + * @throws IllegalArgumentException if <code>v</code> is not comparable with previous entries + */ + public void addValue(Comparable<?>v){ Object obj = v; if (v instanceof Integer) { obj = Long.valueOf(((Integer) v).longValue()); Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/stat/FrequencyTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/stat/FrequencyTest.java?rev=765996&r1=765995&r2=765996&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/org/apache/commons/math/stat/FrequencyTest.java (original) +++ commons/proper/math/trunk/src/test/org/apache/commons/math/stat/FrequencyTest.java Fri Apr 17 13:34:59 2009 @@ -189,6 +189,22 @@ } } + // 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());