You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/04/17 15:07:26 UTC

svn commit: r765978 - /commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.java

Author: sebb
Date: Fri Apr 17 13:07:26 2009
New Revision: 765978

URL: http://svn.apache.org/viewvc?rev=765978&view=rev
Log:
Make HashMap final as it is only set in the ctors
Document behaviour of getCount(Object v) if v is not comparable
Save value of getSumFreq() from DIV/0 check so we don't calculate it twice

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/stat/Frequency.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=765978&r1=765977&r2=765978&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:07:26 2009
@@ -49,7 +49,7 @@
     private static final long serialVersionUID = -3845586908418844111L;
 
     /** underlying collection */
-    private TreeMap freqTable = null;
+    private final TreeMap freqTable;
 
     /**
      * Default constructor.
@@ -193,6 +193,7 @@
 
     /**
      * Returns the number of values = v.
+     * Returns 0 if the value is not comparable.
      * 
      * @param v the value to lookup.
      * @return the frequency of v.
@@ -255,10 +256,11 @@
      * @return the proportion of values equal to v
      */
     public double getPct(Object v) {
-        if (getSumFreq() == 0) {
+        final long sumFreq = getSumFreq();
+        if (sumFreq == 0) {
             return Double.NaN;
         }
-        return (double) getCount(v) / (double) getSumFreq();        
+        return (double) getCount(v) / (double) sumFreq;        
     }
     
     /**
@@ -396,10 +398,11 @@
      * @return the proportion of values less than or equal to v
      */
     public double getCumPct(Object v) {
-        if (getSumFreq() == 0) {
+        final long sumFreq = getSumFreq();
+        if (sumFreq == 0) {
             return Double.NaN;
         }
-        return (double) getCumFreq(v) / (double) getSumFreq();        
+        return (double) getCumFreq(v) / (double) sumFreq;        
     }
     
     /**