Author: tn
Date: Tue Sep  3 19:51:26 2013
New Revision: 1519813

URL: http://svn.apache.org/r1519813
Log:
[MATH-1001] Added overloaded methods for Frequency#incrementValue, thanks to 
sebb.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1519813&r1=1519812&r2=1519813&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Tue Sep  3 19:51:26 2013
@@ -51,6 +51,10 @@ If the output is not quite correct, chec
   </properties>
   <body>
     <release version="x.y" date="TBD" description="TBD">
+      <action dev="tn" type="add" issue="MATH-1001" due-to="sebb">
+        Added overloaded methods for "Frequency#incrementValue(Comparable, 
long)" with
+        int, long and char primitive arguments.
+      </action>
       <action dev="tn" type="add" issue="MATH-1030" due-to="Thorsten Schäfer">
         Added a section to the userguide for the new package o.a.c.m.ml with an
         overview of available clustering algorithms and a code example.

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java?rev=1519813&r1=1519812&r2=1519813&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/Frequency.java
 Tue Sep  3 19:51:26 2013
@@ -85,8 +85,7 @@ public class Frequency implements Serial
     }
 
     /**
-     * Return a string representation of this frequency
-     * distribution.
+     * Return a string representation of this frequency distribution.
      *
      * @return a string representation.
      */
@@ -125,6 +124,39 @@ public class Frequency implements Serial
     }
 
     /**
+     * Adds 1 to the frequency count for v.
+     *
+     * @param v the value to add.
+     * @throws MathIllegalArgumentException if the table contains entries not
+     * comparable to Long
+     */
+    public void addValue(int v) throws MathIllegalArgumentException {
+        addValue(Long.valueOf(v));
+    }
+
+    /**
+     * Adds 1 to the frequency count for v.
+     *
+     * @param v the value to add.
+     * @throws MathIllegalArgumentException if the table contains entries not
+     * comparable to Long
+     */
+    public void addValue(long v) throws MathIllegalArgumentException {
+        addValue(Long.valueOf(v));
+    }
+
+    /**
+     * Adds 1 to the frequency count for v.
+     *
+     * @param v the value to add.
+     * @throws MathIllegalArgumentException if the table contains entries not
+     * comparable to Char
+     */
+    public void addValue(char v) throws MathIllegalArgumentException {
+        addValue(Character.valueOf(v));
+    }
+
+    /**
      * Increments the frequency count for v.
      * <p>
      * If other objects have already been added to this Frequency, v must
@@ -133,10 +165,10 @@ public class Frequency implements Serial
      *
      * @param v the value to add.
      * @param increment the amount by which the value should be incremented
-     * @throws IllegalArgumentException if <code>v</code> is not comparable 
with previous entries
+     * @throws MathIllegalArgumentException if <code>v</code> is not 
comparable with previous entries
      * @since 3.1
      */
-    public void incrementValue(Comparable<?> v, long increment){
+    public void incrementValue(Comparable<?> v, long increment) throws 
MathIllegalArgumentException {
         Comparable<?> obj = v;
         if (v instanceof Integer) {
             obj = Long.valueOf(((Integer) v).longValue());
@@ -157,36 +189,54 @@ public class Frequency implements Serial
     }
 
     /**
-     * Adds 1 to the frequency count for v.
+     * Increments 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.
+     * @param increment the amount by which the value should be incremented
      * @throws MathIllegalArgumentException if the table contains entries not
-     * comparable to Integer
+     * comparable to Long
+     * @since 3.3
      */
-    public void addValue(int v) throws MathIllegalArgumentException {
-        addValue(Long.valueOf(v));
+    public void incrementValue(int v, long increment) throws 
MathIllegalArgumentException {
+        incrementValue(Long.valueOf(v), increment);
     }
 
     /**
-     * Adds 1 to the frequency count for v.
+     * Increments 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.
+     * @param increment the amount by which the value should be incremented
      * @throws MathIllegalArgumentException if the table contains entries not
      * comparable to Long
+     * @since 3.3
      */
-    public void addValue(long v) throws MathIllegalArgumentException {
-        addValue(Long.valueOf(v));
+    public void incrementValue(long v, long increment) throws 
MathIllegalArgumentException {
+        incrementValue(Long.valueOf(v), increment);
     }
 
     /**
-     * Adds 1 to the frequency count for v.
+     * Increments 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.
+     * @param increment the amount by which the value should be incremented
      * @throws MathIllegalArgumentException if the table contains entries not
      * comparable to Char
+     * @since 3.3
      */
-    public void addValue(char v) throws MathIllegalArgumentException {
-        addValue(Character.valueOf(v));
+    public void incrementValue(char v, long increment) throws 
MathIllegalArgumentException {
+        incrementValue(Character.valueOf(v), increment);
     }
 
     /** Clears the frequency table */

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java?rev=1519813&r1=1519812&r2=1519813&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java
 Tue Sep  3 19:51:26 2013
@@ -34,15 +34,15 @@ import org.junit.Test;
  *
  * @version $Id$
  */
-
 public final class FrequencyTest {
     private static final long ONE_LONG = 1L;
     private static final long TWO_LONG = 2L;
     private static final long THREE_LONG = 3L;
     private static final int ONE = 1;
     private static final int TWO = 2;
-    private static final int THREEE = 3 ;
+    private static final int THREE = 3 ;
     private static final double TOLERANCE = 10E-15d;
+    private static final char CHAR_A = 'a';
 
     private Frequency f = null;
 
@@ -135,7 +135,7 @@ public final class FrequencyTest {
         f.addValue(THREE_LONG);
         f.addValue(THREE_LONG);
         f.addValue(3);
-        f.addValue(THREEE);
+        f.addValue(THREE);
         Assert.assertEquals("one pct",0.25,f.getPct(1),TOLERANCE);
         Assert.assertEquals("two 
pct",0.25,f.getPct(Long.valueOf(2)),TOLERANCE);
         Assert.assertEquals("three pct",0.5,f.getPct(THREE_LONG),TOLERANCE);
@@ -255,10 +255,17 @@ public final class FrequencyTest {
         Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), 
TOLERANCE);
         Assert.assertEquals("Integer 1 cumPct", 0.5, 
f.getCumPct(Long.valueOf(1)), TOLERANCE);
         Assert.assertEquals("Integer 1 cumPct", 0.5, 
f.getCumPct(Integer.valueOf(1)), TOLERANCE);
+        
+        f.incrementValue(ONE, -2);
+        f.incrementValue(THREE, 5);
+
+        Assert.assertEquals("Integer 1 count", 0, f.getCount(1));
+        Assert.assertEquals("Integer 3 count", 5, f.getCount(3));
+
         Iterator<?> it = f.valuesIterator();
         while (it.hasNext()) {
             Assert.assertTrue(it.next() instanceof Long);
-        }
+        }        
     }
 
     @Test
@@ -292,6 +299,29 @@ public final class FrequencyTest {
         
         f.incrementValue(ONE_LONG, -5);
         Assert.assertEquals(0, f.getCount(ONE_LONG));
+        
+        try {
+            f.incrementValue(CHAR_A, 1);
+            Assert.fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+
+        f = new Frequency();
+        f.incrementValue(CHAR_A, 2);
+        
+        Assert.assertEquals(2, f.getCount(CHAR_A));
+
+        try {
+            f.incrementValue(ONE, 1);
+            Assert.fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        
+        f.incrementValue(CHAR_A, 3);
+        Assert.assertEquals(5, f.getCount(CHAR_A));
+
     }
     
     @Test
@@ -309,18 +339,18 @@ public final class FrequencyTest {
         Frequency g = new Frequency();
         g.addValue(ONE_LONG);
         g.addValue(THREE_LONG);
-        g.addValue(THREEE);
+        g.addValue(THREE);
 
         Assert.assertEquals(2, g.getUniqueCount());
         Assert.assertEquals(1, g.getCount(ONE));
-        Assert.assertEquals(2, g.getCount(THREEE));
+        Assert.assertEquals(2, g.getCount(THREE));
 
         f.merge(g);
         
         Assert.assertEquals(3, f.getUniqueCount());
         Assert.assertEquals(3, f.getCount(ONE));
         Assert.assertEquals(2, f.getCount(TWO));
-        Assert.assertEquals(2, f.getCount(THREEE));        
+        Assert.assertEquals(2, f.getCount(THREE));        
     }
     
     @Test
@@ -346,7 +376,7 @@ public final class FrequencyTest {
         Assert.assertEquals(3, f.getUniqueCount());
         Assert.assertEquals(1, f.getCount(ONE));
         Assert.assertEquals(1, f.getCount(TWO));
-        Assert.assertEquals(1, f.getCount(THREEE));        
+        Assert.assertEquals(1, f.getCount(THREE));        
     }
     
     @Test


Reply via email to